开发者

Return more than a single value from a WebService

开发者 https://www.devze.com 2022-12-14 14:50 出处:网络
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

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?

tha开发者_JAVA技巧nk you :)


any serializable class can be returned from a web service


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号