Wcf transfer list as method parameter

I have a wcf service that has the following method:

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

then I call it in the lib solution, which refers to this service as follows:

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

but I keep 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 do is get a list populated by another web service.

What is the problem here, since the argument I'm passing in is a list, and the method accepts the list if anyone can point me in the right direction.

+4
source share
6 answers

This is due to how you added the link to the service.

You can change your code to be sent by the client []

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

or re-add the link and select the option to use System.Collection.Generic.List, as shown below:

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

Select a collection type as System.Collection.Generic.List

+3
source

The problem is that when you create a service reference in WCF, by default WCF will use an array for all (non-dictionary) collection types. This will call the method defined in such a service:

 public void SyncClient(List<Client> client) { 

To display in the service link as:

 public void SyncClient(Client[] client) 

This is a custom parameter - in the Create Service Link dialog box, you can use WCF instead of List<T> instead of an array, in which case your code will work.

Alternatively, you can simply convert the object to an array (via ToArray() ) when calling the help service, if you want.

+3
source

Can you try this:

 WCF.SyncClient wcfSer = new WCF.SyncClient(); Client[] clientModified = secondservice.Get_Clients(ModifiedDate, modifiedUnt).ToArray(); wcfSer.SyncClient(clientModified); 
+2
source

This can happen if you added a service link in visual studio to create a proxy. The "Data type" section in the help desk settings has a drop-down list for the "Collection" type. Check if System.Collections.Generic.List or System.Array .

In addition to this, if you have a reference to the assembly that defines the contract, you can also check the Reuse types in referenced assemblies , which the generated proxy will have to use the provided assembly instead of creating a new type.

+1
source

My suggestion ... do not use Visual Studio for a cross proxy class. If you have all your objects and your interface in the WCF library (without actually implementing the service, because it should be different anyway), why not just drop the client and access it directly. Here you have everything you need to do ...

 public class MyWCFClient:ClientBase<IMyWCFServiceContract>, IMyWCFServiceContract { //Implement the interface and call the channel public void SyncClient(List<Client> clients) { this.Channel.SyncClient(clients); } } 

Then bam! you have your proxy (client), and you reuse all of your DataContract classes that you created to include any methods that they do so that you can take advantage! Read this CodeProject to learn a little more about it. This not only solved a very similar problem that I had, but also allowed me to be more flexible in my DataContract objects, so I could use them not only for data transfer.

This method can save you the trouble of running multiple services, since the gererator of the VS service likes to create some namespaces associated with your current namespace. If you have a library go crazy.

If you do not have access to these libraries, do not completely ignore it, since you can use the code using the method described above CodeProject Article and also custimize the code (including namespaces).

+1
source

Try the following:

 StreamReader reader = new StreamReader(JSONdataLogin); string JSONdata = reader.ReadToEnd(); JavaScriptSerializer jss = new JavaScriptSerializer(); List<WsShoeClass> OBJ = jss.Deserialize<List<WsShoeClass>>(JSONdata); 

 foreach (var Images in OBJ) { SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["conn"].ConnectionString); SqlCommand cmd = new SqlCommand("sp_UploadShoeImage", con); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@UserId", Images.UserId)); cmd.Parameters.Add(new SqlParameter("@ShoeId", Images.ShoeId)); cmd.Parameters.Add(new SqlParameter("@ShoeImage", Images.ShoeImage)); con.Open(); SqlDataAdapter ad = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); ad.Fill(ds); } 
0
source

Source: https://habr.com/ru/post/1441322/


All Articles