1

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

这是我的 2 个具体错误:

错误一:

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; }
   }
}
4

2 回答 2

2

感谢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

        }
    }
于 2012-08-19T19:31:24.030 回答
0

你的lstNewItemsasp代码里有什么?

要工作,它应该是一个Item[]. 即使您List<Item>在 WCF 代码中使用 a,WCF 生成的代理代码的默认设置是使用数组进行集合。

编辑

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

错误信息显示FilesToControl 代理方法接受一个数组 ( Item[])

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

你传入一个List<Item>

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

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

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

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

ControlFiles_Result = Client.FilesToControl(lstNewItems.ToArray());
于 2012-08-18T15:40:12.983 回答