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?
Best Answer
Create an instance of ArrayOfList, then copy your list into it like this:arrString.AddRange(strList);
Creating an ArrayOfString for a WCF web service involves several steps and considerations. First, you need to define the data type in your service contract. This can be done using the [DataContract]
attribute. Next, you'll need to create a class that represents the array of strings. This class should be marked with the [CollectionDataContract]
attribute.
Once you have defined the ArrayOfString data type, you can use it as a parameter or return type in your service operations. To pass an ArrayOfString as a parameter, simply include it in the method signature. To return an ArrayOfString from a service operation, specify it as the return type.
When working with ArrayOfString, it's important to remember that WCF uses XML serialization to transmit data. This means that the array will be converted to XML before being sent over the network. Make sure that your ArrayOfString class is serializable and can be properly deserialized by the client.
In conclusion, creating an ArrayOfString for a WCF web service is a straightforward process. By following the steps outlined in this guide, you'll be able to effectively use arrays of strings in your WCF service operations.
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");