views:

108

answers:

5

I have to return a lot of values back to my windows application from my webService. But how would I return more than just a single string/int/boolean from my WebService to my Application. How would I return a collection, keyValuePair, or even better, a DataSet? Or is this just imposible?

thank you :)

+1  A: 

If your webservice is written in ASP.NET it's as simple as setting the return type from your webservice to be the more complex type. Most serializable types are supported.

Andrew Koester
+2  A: 

any serializable class can be returned from a web service

Steven A. Lowe
+1  A: 

The best method I've used with webservices is to return XML as a string. This way there are no compatability issues and parsing the XML is easy enough.

SLoret
A: 

Thank you all for your perfect and quick answers, it really helped me out

For anyone else: I simply used the tutorial, and got everything working

Jacob Kofoed
Please mark the question as answered if you've solved your problem.
Andrew Koester
A: 

Along the same lines as returning XML, if you're returning to javascript code, you may want to consider returning your complex type as serialized in JSON.

Here is an extension method which makes this really easy...

<Extension()> Public Function ToJSON(Of T As Class)(ByVal target As T) As String
    Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(T))
    Using ms As MemoryStream = New MemoryStream()
        serializer.WriteObject(ms, target)
        ms.Flush()

        Dim bytes As Byte() = ms.GetBuffer()

        Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim(Chr(0))

        Return json
    End Using
End Function

Then in your data service, you can simply call

Return MyObject.ToJSON
eidylon