ncupper / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Sample WCF client using protobuf #125

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Could you please profile a small sample how to create a WCF client that uses 
generated interface for the service and which serializes data into binary form? 
Here is the code that is generated by protoc.exe

[global::System.ServiceModel.ServiceContract(Name = @"DaemonService")]
public interface IDaemonService
{
    [global::System.ServiceModel.OperationContract(Name = @"DoHello")]
    [global::ProtoBuf.ServiceModel.ProtoBehavior]BaseResponse DoHello(HelloRequest request);
}

I wrote generic method that creates WCF channel for given interface:

public static T CreateWebService<T>(string host, string servicePath) where T : 
class
{
    bool secureMode = host.StartsWith("https");
    var securityMode = (secureMode ? WebHttpSecurityMode.Transport : WebHttpSecurityMode.None);
    var myBinding = new WebHttpBinding(securityMode) { MaxReceivedMessageSize = (5242880) };
    myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    var factory = new WebChannelFactory<T>(myBinding, new Uri(host + servicePath));
    GenericEndpointBehavior.AddClientMessageInspector(factory.Endpoint, new MyInspector());

    T service = factory.CreateChannel();
    return service;
}

then I used it like this:

var service = CreateWebService<ICirrusDaemonService>("http://85.202.240.71:80", 
"/service");
var helloRequest = new HelloRequest {VersionTrayApp = 1, VersionWinService = 2, 
Message = "Test Message"};
service.DoHello(helloRequest);

This code makes http request where data is serialized into XML form instead of 
binary form of protobuf.

What do I need to do in order to make it serialize data into binary form?

Thank you.

Original issue reported on code.google.com by rusl...@gmail.com on 30 Jul 2010 at 5:50

GoogleCodeExporter commented 9 years ago
After two hours of dealing with WCF I think I should clarify what I'm exactly 
trying to implement.

My goal is to have .Net client that talks with Java App Server. I want .Net 
client to use WCF in a way of talking to the server via HTTP/HTTPS calls as it 
does with default WCF XmlSerializer but instead of having XML in body of HTTP 
POST request to have protobuf binary in it.

Is it possible? So far I can't achieve it :( Moreover even when I use code 
shown above WCF client never calls ProtoOperationBehavior.CreateSerializer. It 
uses DataContractSerializerOperationBehavior anyway.

Original comment by rusl...@gmail.com on 30 Jul 2010 at 7:38

GoogleCodeExporter commented 9 years ago
Is that using WCF in REST mode? If so, then it might be a pain. I'm not 100% 
what API your java server is using, but I wonder if it is simpler (for REST) to 
handle the serialization externally for now, for example via `WebClient` to get 
bytes. Just a thought.

Original comment by marc.gravell on 30 Jul 2010 at 8:09

GoogleCodeExporter commented 9 years ago
Hi Marc. I created C# client that uses WCF and talks via REST calls to 
Java-based application server (Google App Engine in my case). If you wish I can 
write wiki page here with sample code that other people can use.

Original comment by rusl...@gmail.com on 4 Aug 2010 at 7:04

GoogleCodeExporter commented 9 years ago
Hi rusl

is it possible to use silverlight enabled WCF service along with a silverlight 
application in the Google Apps Engine ? 

i you have any sample code that uses WCF and talks to GAE, Please do write in 
wiki with the same and send the link to me. 

Original comment by yoosufsh...@gmail.com on 26 May 2011 at 9:46

GoogleCodeExporter commented 9 years ago
@rusl - GAE would presumably need a very specific format (of which I am not 
aware) - which may or may not involve protobufs (you don't state). I'm not sure 
this is a protobuf query, is it? ultimately though, I doubt GAE is going to 
want to talk WCF.

Original comment by marc.gravell on 26 May 2011 at 10:05

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Wow, it's been almost a year :) Marc, if you want I can post working sample of 
integration with GAE on your wiki.

Original comment by rusl...@gmail.com on 26 May 2011 at 5:34

GoogleCodeExporter commented 9 years ago
@rusl - sorry, I realise I copied the wrong name - I meant to reply to 
"yoosufsh..." - sorry about that. But if you *have* such an example I'd be 
overjoyed. But I genuinely never intended to put you to any trouble.

Somehow this must have slipped me by - I had a bit of a crazy/busy time late 
last year, hence why protobuf-net took a bit of a pause.

Original comment by marc.gravell on 26 May 2011 at 8:17