How Do You Create a Arrayofstring for a Wcf Web Service?
I Have a List Object That Needs to Be Passed to a Wcf Web Service. the Object Type That the Web Service Helper Class Wants Is an Arrayofstring Object. for the...
I have a List<string> object that needs to be passed to a WCF web service. The object type that the web service helper class wants is an ArrayOfString object. For the life of me, I cannot find a way to convert my List<string> to an ArrayOfString object.
How can I do this?
NOTE
I have tried the following lines of code. None of these conversions work.
List<string> strList = new List<string>();
...
ArrayOfString strs = strList;
ArrayOfString strs = strList.ToArray();
ArrayOfString strs = strList.AsEnumerable();
What else can I try?
4 Answers
Create an instance of ArrayOfList, then copy your list into it like this:
arrString.AddRange(strList);
I Created a web service with this method:
public List<decimal> GetData(string[] ConnectionInfo)
The WSDL translation of those types were ArrayOfDecimal and ArrayOfString
So to receive the correct types in a client program, this was not sufficient:
decimalArray = GetServiceClient.GetData(connectionArray);
To convert to ArrayOfDecimal and ArrayOfString, I found and instantiated the types through the service refence like so
GetWebService.ArrayOfString myStrArr = new GetWebService.ArrayOfString();
GetWebService.ArrayOfDecimal myDecArr = new GetWebService.ArrayOfDecimal();
Added the values to the new type:
myStrArr.AddRange(listOfConnections);
and succesfully called the method like so
myDecArr = GetServiceClient.GetData(myStrArr);
I created a web client using Jaxb and Wsdl file. The list parameters are typeof ArrayOfstring. There is no constructor for creating ArrayOfstring from List. So I get the string and add list elements. Here is my solution:
List<String> myArrayList = new ArrayList<String>();
myArrayList.add("test1");
myArrayList.add("test2");
ArrayOfsring arrayOfstring = objectFactory.createArrayOfstring();
arrayOfstring.getString().addAll(myArrayList);
Not sure if it works for C# but in Java you can create it in the following way:
ArrayOfString myArrayOfString = new ArrayOfString();
myArrayOfString.getString().addAll(Arrays.asList("your", "list", "of", "strings");