Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wcf passing list as method parameter

I have a wcf service which has a method as follow:

 public void SyncClient(List<Client> client)
        {
         }

then i call this in a lib solution which has reference to this service as follow:

WCF.SyncClient wcfSer = new WCF.SyncClient();
 List<Client> clientModified = secondservice.Get_Clients(ModifiedDate, modifiedUnt).ToList();
wcfSer.SyncClient(clientModified);

but I keep on getting the following error :

The best overloaded method match for 'SyncClient(SyncLib.ClientWCF.Client[])' has some invalid arguments    
cannot convert from 'System.Collections.Generic.List<SyncLib.Client>' to 'SyncLib.WCF.Client[]'

The only thing I am doing is getting the list populated with another web service.

what is the problem here as the argument that I am passing is list and the method accepts list, can some one please point me to right direction.

like image 697
Zaki Avatar asked Oct 22 '12 15:10

Zaki


2 Answers

This is down to how you added the service reference.

You can either amend your code to send client[]

WCF.SyncClient wcfSer = new WCF.SyncClient();
 var clientModified = secondservice.Get_Clients(ModifiedDate,
    modifiedUnt).ToArray();
wcfSer.SyncClient(clientModified);

or re-add the reference and select the option to use System.Collection.Generic.List as below;

Select Add Reference
Select URL
Select Advanced when you have found the service.
In Datatype, you will see these options.

Select Collection Type as System.Collection.Generic.List

like image 112
ChrisBint Avatar answered Sep 22 '22 01:09

ChrisBint


The issue is, when you create a service reference in WCF, by default, WCF will use an array for all (non-dictionary) collection types. That will cause a method defined in a service like so:

public void SyncClient(List<Client> client)
{

To map through in the service reference as:

public void SyncClient(Client[] client)

This is a configurable option - in the "Create Service Reference" dialog, you can have WCF use a List<T> instead of an array, in which case your code will work.

Alternatively, you can just convert the object to an array (via ToArray()) when calling the service reference, if you prefer.

like image 41
Reed Copsey Avatar answered Sep 24 '22 01:09

Reed Copsey