Service Links and List / Array Types

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

Assumptions:

  • Both programs and services executed in VS2008
  • I have complete control over the Program and Service.
  • I have a program that passes a "list" of information 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 doesn't think that the string [] I create in the service is the same String array. It will convert it to the # namespace type. # Servicename.ArrayOfString

this code does not currently compile. I have looked at alternative ways of transferring the list or general, and I continue to encounter roadblocks.

0


source to share


2 answers


Consider changing your code to use List<string>

, not IList<string>

(I know what you are actually using, but declare your variable as such). The class List<T>

provides an explicit function ToArray()

that will be faster than the extension method Enumerable.ToArray<T>

that you use in your code. This one might clear up your problems, but I'm not sure about that.

Also, you don't need

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

      

Just declare it as



string[] arrayToSend;

      

You have already assigned it as a call ToArray()

; no need to create another in advance.

Edit . Why not create and populate a class ArrayOfString

created in your service reference namespace? It will work the same way List<string>

(and, in fact, I believe it inherits from it). Use this as a type for XMLList

and just pass the variable XMLList

directly to the function.

+1


source


If you changed WebMethod

to accept List<string>

and not string[]

pass the appropriate type, it should work. ASP.NET services seem to prefer this in my experience.

Also, remember to make sure that your service references are up to date so that the service wrapper types are properly created for your code to interact.



Update

Have you tried actually creating the type ArrayOfString

and populating it rather than using it string[]

when calling your WebMethod?

0


source







All Articles