Tutorial :Service Reference and List/Array Types



Question:

I have a problem that I really need solved in any way (changing data types is an option)

Assumptions:

  1. Both Program and Service done in VS2008
  2. I have complete control over both Program and Service
  3. I have a program that is passing a "list" of info to the Service Reference

Example:

Program snippet:

IList<string> XMLList = null;  XMLList = new List<string>();    // fill my list up here...  mostly just an "array" of xml-based strings    string[] arrayToSend = new string[XMLList.Count];  arrayToSend = XMLList.ToArray<string>();    string results = DataFeedService.ProcessXMLBatch(arrayToSend);  

Service Snippet:

[WebMethod]  public string ProcessXMLBatch(string[] XMLBatch)  {         // process the xml here...         return "done";  }  

Problem: It does not consider the string[] that I create in the service to be the same kind of string array. It converts it to a type of #namespace.#servicename.ArrayOfString

this code will currently not compile. I have looked around for alternate ways to pass a list or a generic and I keep hitting roadblocks.


Solution:1

Consider changing your code to use a List<string> rather than an IList<string> (I know that's what you're actually using, but declare your variable as such). The List<T> class provides an explicit ToArray() function that will be faster than the Enumerable.ToArray<T> extension method you're employing in your code. This might clear up your issues too, but I'm not certain about that.

Also, you do not need

string[] arrayToSend = new string[XMLList.Count];  

Just declare it as

string[] arrayToSend;  

You're already assigning it with the ToArray() call; no need to create another one beforehand.

Edit Why not instantiate and populate the ArrayOfString class that's created in your service reference namespace? It will work just like a List<string> (and, in fact, I believe it inherits from it). Use this as the type for your XMLList, and just send the XMLList variable directly to the function.


Solution:2

If you change your WebMethod to accept a List<string> rather than string[] and pass an appropriate type, it should work. ASP.NET services seem to prefer this in my experience.

Also, don't forget to make sure your Service References are up-to-date so that the service wrapper types are correctly created for your code to interact with.

Update

Have you tried actually creating the ArrayOfString type and populating it, rather than using string[] when calling your WebMethod?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »