views:

60

answers:

1

I have a WCF service hosted that is returning a byte[] of protobuf-net serialized data. Originally this was hosted over the default webHttpBinding settings (SOAP) and everything was working properly. I recently found out that I need to call HTTP GETs and POSTs directly from our client so I thought it'd be easier to switch to use a RESTful service. I switched to use the WebGet attribute and the WCF REST Template.

I attempted to create a simple web site to test the client and I'm having trouble deserializing the data. This is an example of how I'm calling the service:

using (WebClient client = new WebClient())
{
    result = client.DownloadString(url);
}    
// Deserialize 
BinaryVehicles binVehs;
using (var ms = new MemoryStream(StrToByteArray(result)))
{
    binVehs = Serializer.Deserialize<BinaryVehicles>(ms);
}

An example of what is returned in "result": < base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ChsKCzEyMy00NTYtNzg5EgU0NDAwMBoFQmxhY2sKHAoLOTYzLTg1Mi03NDESBTIzMDAwGgZTaWx2ZXI=&lt; /base64Binary>

I also attempted to deserialize the data between the < base64Binary > tags with no results. Does anyone have any idea on how I should go about sending the binary protobuf-net data from an WebGet method and how I should deserialize the data? Thanks.

+1  A: 

protobuf-net primarily handles just the serialization aspects (the protobuf spec by Google doesn't define any further than this). So it really comes down to: how are you serializing it?

I must admit that the WCF GET approach is not something I've looked at hugely, so there is no special handling there. One simple approach may be to look at just returning a string, and handling the base-64 encoding yourself, but to be honest if you are doing HTTP GET, then WCF itself seems overkill.

I blogged here about using ASP.NET MVC at the server for protobuf via HTTP GET. The linked sample code also includes a wire-compatible ASP.NET implementation.

If there is something appropriate we can do to make WCF GET easier, I'm all ears...

Marc Gravell
Thanks Marc. Should I be encoding the protobuf-net data after I serialize it and then use the same encoding and deserialize it on the receiving end? Quickly, I attempted to do this using these simple methods: http://www.chilkatsoft.com/faq/dotnetstrtobytes.html and couldn't get it to work properly. I'm getting a ProtoBuf.ProtoException when it tries to deserialize it. Let me know if you have any hints. Thanks!
Sean
@Sean - that article describes starting from a `string`, encoding **to** a `byte[]`, then back to a `string`. However, going in the **other** direction works very differently (since the binary is **not** ASCII etc): see http://marcgravell.blogspot.com/2010/03/binary-data-and-strings.html, which notes to use `Convert.ToBase64String` and `Convert.FromBase64String`.
Marc Gravell
@Marc Gravell - thanks, that did the trick!
Sean