【问题标题】:WCF Call Function from asp.net来自 asp.net 的 WCF 调用函数
【发布时间】:2012-08-18 15:28:43
【问题描述】:

我正在尝试通过 WCF 传递一个对象。我可以通过stringsints 等没问题,但是这个对象列表不起作用。

这是我的 2 个具体错误:

错误 1:

The best overloaded method match for
 'test.ServiceReference1.JobsClient.FilesToControl(test.ServiceReference1.Item[])'
 has some invalid arguments

错误 2:

Argument 1: cannot convert from 'System.Collections.Generic.List<test.Site1.Item>'
 to 'test.ServiceReference1.Item[]'

这是我试图在 asp.net 中调用的函数:

// Commit the files
ServiceReference1.JobsClient Client = new ServiceReference1.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://" +   DropDownListSystems.SelectedValue + ":8732/FactoryAudit/");
test.ServiceReference1.ReturnClass ControlFiles_Result;
ControlFiles_Result = Client.FilesToControl(lstNewItems);

lstNewItems(我要传递的对象)定义为如下代码:

List<Item> lstNewItems = new List<Item>(); // Control Items  

Item定义为:

public class Item
{
    public Item(string Paramater, string Type)
    {
        _Paramater = Paramater;
        _Type = Type;

    }

    private string _Paramater;

    public string Paramater
    {
        get { return _Paramater; }
        set { _Paramater = value; }
    }

    private string _Type;

    public string Type
    {
        get { return _Type; }
        set { _Type = value; }
    }
}

现在在 WCF 服务中,我有以下接受项目列表的函数:

//Files Manager
public  ReturnClass FilesToControl(List<Item> ItemsToControl)

{ return new ReturnClass(1, String.Empty, String.Empty, null, null, null); }

服务合同定义为:

[ServiceContract]
public interface IJobs
{
     //Files Manager
     [OperationContract]
     ReturnClass FilesToControl(List<Item> ItemsToControl);      
}

项目类在 WCF 中定义如下:

[DataContract]
[KnownType(typeof(List<Item>))]
[KnownType(typeof(Item))] 
public class Item
{
   public Item(string Paramater, string Type)
   {
       _Paramater = Paramater;
       _Type = Type;

   }
   [DataMember]
   private string _Paramater;
   [DataMember]
   public string Paramater
   {
       get { return _Paramater; }
       set { _Paramater = value; }
   }
   [DataMember]
   private string _Type;
   [DataMember]
   public string Type
   {
       get { return _Type; }
       set { _Type = value; }
   }
}

【问题讨论】:

  • 我现在会更好地格式化它。谢谢你的帮助。希望我可以附上一个邮政编码示例。当代码在不同的项目和类中时,很难解释。
  • 我已经更新了我的答案并删除了所有现在已经过时的 cmets。

标签: c# asp.net wcf


【解决方案1】:

多亏了 Abel,我才得以弄清楚。下面是解决方案。我希望这对其他人有帮助。

请注意,您还必须右键单击 asp.net 中的服务引用,然后单击“配置服务引用”并将“集合类型”设置为 Systems.Collections.Generic.List

这是我在 asp.net 中调用函数的方式

Item NewItem = new Item();
        foreach (GridViewRow PendingItemUnderControl in GridViewPendingList.Rows)
        {
            NewItem.Paramater = PendingItemUnderControl.Cells[0].Text.ToLower();
            NewItem.Type = (String)Session["BrowseType"];
            lstNewItems.Add(NewItem);
        }
       ControlFiles_Result = Client.FilesToControl(lstNewItems);

asp.net中lstItems的定义如下

  List<Item> lstNewItems = new List<Item>(); // Control Items  

Abel 说我需要在我的 asp 项目顶部添加一个引用是正确的。另请注意,项目是在 WCF 应用程序中声明的,因此无需在 asp.net 中声明它,因为我们在下面的行中引用了它,如下所示:

using test.ServiceReference1;

现在在 WCF 中输入 IJobs.cs 中的代码。请注意私有和公共测试,我将所有这些都公之于众,这造成了严重破坏。

using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Collections;
namespace WCFJobsLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IJobs" in both code and config file together.
    [ServiceContract]
    public interface IJobs
    {
         //Directoy Manager
         [OperationContract]
        ReturnClass FindDrives();
         //Directoy Manager
         [OperationContract]
         ReturnClass FindSubfolders(String Folder_To_Search);
         //Directoy Manager
         [OperationContract]
         ReturnClass FindSubFiles(String Folder_To_Search);
         //Directoy Manager
         [OperationContract]
         ReturnClass FilesToControl(List<Item> ItemsToControl);        

    }


        [DataContract]
      //  [KnownType(typeof(List<Item>))]
        [KnownType(typeof(Item))] 
           public class Item
       {
           public Item(string Paramater, string Type)
           {         
               _Paramater = Paramater;
               _Type = Type;
           }
           private string _Paramater;
            [DataMember]
           public string Paramater
           {
               get { return _Paramater; }
               set { _Paramater = value; }
           }
            private string _Type;
            [DataMember]
           public string Type
           {
               get { return _Type; }
               set { _Type = value; }
           }

       }




}

还有 Jobs.cs - 通过一个简单的测试来确保它正常工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Collections;
using System.Web;
using Microsoft.Win32;
namespace WCFJobsLibrary
{

        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Jobs" in both code and config file together.
       public class Jobs : IJobs
    {


       #region IJobs Members            



        //Files Manager
        public ReturnClass FilesToControl(List<Item> lstNewItems)
       {
           try
           {
               String ThisisAnItemToControl = "";
               String ThisIsItsType = "";

               for (int i = 0; i < lstNewItems.Count; i++) // Loop through List with for
               {
                   ThisisAnItemToControl = lstNewItems[i].Paramater;
                   ThisIsItsType = lstNewItems[i].Type;

               }

               return new ReturnClass(1, ThisisAnItemToControl, String.Empty, null, null, null);

           }

           catch (Exception ex)
           {

               return new ReturnClass(-1, ex.Message.ToString(), ex.InnerException.ToString(), null, null, null);

           }           


       }

        }
       #endregion

终于在ReturnClass.cs类中定义了ReturnClass

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ComponentModel;
    using System.Data;
    using System.IO;
    using System.Collections;
    using System.Web;
    using Microsoft.Win32;

    namespace WCFJobsLibrary
    {
        public class ReturnClass
        {
            //private members
            private int _errorCode;
            private string _errorMessage;
            private string _exMessage;
            private ArrayList _drives;
            private string[] _folders;
            private string[] _filePaths;


            #region Constructors

            //constructor 1
            public ReturnClass()
            {

            }

            //constructor 2
            public ReturnClass(int iErr, string sErrMsg, string ExMsg, ArrayList arrDrives, string[] sfolders, string[] sFilePaths)
            {
                ErrorCode = iErr;
                ErrorMessage = sErrMsg;
                ExMessage = ExMsg;
                Drives = arrDrives;
                Folders = sfolders;
                FilePaths = sFilePaths;
            }

            #endregion

            #region methods

            //Error Code
            public int ErrorCode
            {
                get { return this._errorCode; }
                set { this._errorCode = value; }
            }

            //error message
            public string ErrorMessage
            {
                get { return this._errorMessage; }
                set { this._errorMessage = value; }
            }

            //exception message
            public string ExMessage
            {
                get { return this._exMessage; }
                set { this._exMessage = value; }
            }

            //drives
            public ArrayList Drives
            {
                get { return this._drives; }
                set { this._drives = value; }
            }

            //folders
            public string[] Folders
            {
                get { return this._folders; }
                set { this._folders = value; }
            }

            //File Paths
            public string[] FilePaths
            {
                get { return this._filePaths; }
                set { this._filePaths = value; }
            }

            #endregion

        }
    }

【讨论】:

    【解决方案2】:

    您的 asp 代码中的lstNewItems 是什么?

    要工作,它应该是Item[]。即使您在 WCF 代码中使用 List&lt;Item&gt;,WCF 生成的代理代码的默认设置是使用数组进行集合。

    编辑

    有了最新的编辑,问题就可以找到:

    错误信息显示FilesToControlproxy方法接受一个数组(Item[]

    错误 1 ​​最好的重载方法匹配 'test.ServiceReference1.JobsClient.FilesToControl(test.ServiceReference1.Item[])' 有一些无效的参数

    你传入一个List&lt;Item&gt;

    List<Item> lstNewItems = new List<Item>();
    //....
    ControlFiles_Result = Client.FilesToControl(lstNewItems);
    

    有三种方法可以解决此问题。

    • lstNewItems 改为Item[] 类型。
    • 更改服务引用的设置(您已添加它以便能够调用您的 WCF)以使用 List&lt;T&gt; 作为默认集合类型。
    • 更改调用以转换为数组。

    后者可以通过调用ToArray来完成:

    ControlFiles_Result = Client.FilesToControl(lstNewItems.ToArray());
    

    【讨论】:

    • 嗨 public class Item { public Item(string Paramater, string Type) { _Paramater = Paramater; _Type = 类型; } 私有字符串 _Paramater;公共字符串参数 { 获取 { 返回 _Paramater; } 设置 { _参数 = 值; } } 私有字符串 _Type;公共字符串类型 { 获取 { 返回 _Type; } 设置 { _Type = 值; } } }
    • 嗨 abel,你是说我不能创建自定义类型并通过 wcf 传递它,但必须传递一个简单的数组? - 如果不能,请提供示例代码来修复我的代码。谢谢达摩
    • 嗨 Abel,选项 1 -“将 lstNewItems 更改为 Item[] 类型。” - 这个选项我不想使用。选项 2 -“更改服务引用的设置(您已添加它以便能够调用您的 WCF)以使用 List 作为默认集合类型。”我今天在研究错误时尝试了所有的服务集合,但这不起作用,因为它仍然会产生编译错误。选项 3 -“更改调用以转换为数组。” - 根据其他研究,我今天已经尝试过了,但它给出了编译错误 - 这将是我的首选解决方案。
    • Abel,这是我按照您的建议将 lstnewitems 转换为数组时出现的错误。错误 1 ​​'test.ServiceReference1.JobsClient.FilesToControl(test.ServiceReference1.Item[])' 的最佳重载方法匹配有一些无效参数错误 2 参数 1:无法从 'test.Site1.Item[]' 转换为 'test .ServiceReference1.Item[]'
    • 您正在使用 test.Site1 命名空间中的另一个类 Item,而不是 WCF 在 test.ServiceReference1 中生成的 Item 类。它们是两个不同的类,WCF 对test.Site1 中的类一无所知。如果类相同,只需更改代码隐藏文件中的using 指令即可修复。
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    相关资源
    最近更新 更多