I am trying to pass an object via WCF. I can pass strings, ints etc no problem, but this list of objects will not work.
Here are my 2 specific errors:
Error 1:
The best overloaded method match for
'test.ServiceReference1.JobsClient.FilesToControl(test.ServiceReference1.Item[])'
has some invalid arguments
Error 2:
Argument 1: cannot convert from 'System.Collections.Generic.List<test.Site1.Item>'
to 'test.ServiceReference1.Item[]'
This is the function I am trying to call within 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 (the object I am trying to pass) is defined as the following code:
List<Item> lstNewItems = new List<Item>(); // Control Items
Item is defined as:
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; }
}
}
Now in the WCF service I have the following function which accepts a list of items:
//Files Manager
public ReturnClass FilesToControl(List<Item> ItemsToControl)
{
return new ReturnClass(1, String.Empty, String.Empty, null, null, null);
}
The service contract is defined as:
[ServiceContract]
public interface IJobs
{
//Files Manager
[OperationContract]
ReturnClass FilesToControl(List<Item> ItemsToControl);
}
The items class is defined in WCF as follows:
[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; }
}
}
What’s
lstNewItemsin your asp code?To work it should be an
Item[]. Even though you take aList<Item>in the WCF code, the default settings for proxy code generated by WCF is to use arrays for collections.Edit
With the latest edit in place, the problem is possible to find:
The error message shows that the
FilesToControlproxy method accepts an array (Item[])You pass in a
List<Item>:There are three ways to fix this.
lstNewItemsto be of typeItem[]instead.List<T>as the default collection type.The latter can be done through a call to
ToArray: