I'm playing with protobuf-net and WCF. Here is code I created:
public class MobileServiceV2
{
[WebGet(UriTemplate = "/some-data")]
[Description("returns test data")]
public Stream GetSomeData()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-protobuf";
var ms = new MemoryStream();
ProtoBuf.Serializer.Serialize(ms, new MyResponse { SomeData = "Test data here" });
return ms;
}
}
[DataContract]
public class MyResponse
{
[DataMember(Order = 1)]
public string SomeData { get; set; }
}
When I look in Fiddler - I can see proper outgoing content-type and all looks good, but I get empty response. IE prompts to download file and this file is empty. Is serializer not working? Or I just don't do it right?
EDIT:
I added following code to method and yes, it serializes correctly. Something wrong with how I return stream from WCF..
using (var file = File.Create("C:\\test.bin"))
{
Serializer.Serialize(file, new MyResponse { SomeData = "Test data here" });
}
If I understand the question correctly, you're trying to use a streaming WCF binding. In that case, you could try splitting the data into chunks that are serialized individually and deserialized in the same way on the client. The only caveat is the WCF supplied Stream implementation on the receiving side - you'll need to wrap it and manage read on your own. The following is a class I use to facilitate this:
To use it simply call a WCF interface method like this:
And read it on the receiving end like this:
This implementation is still in testing, so I'll appreciate any input.
Just write to a MemoryStream, and rewind it. Do not Dispose() it in this case:
This does, however, mean that it buffers in memory. I could try and come up with some voodoo to avoid that, but it would be very complex.
Please try this way