简体   繁体   中英

C# Protobuf Wcf

I want to test protobuf with wcf. I configured my service and client with the protobuf attirbutes and in the app-config added the extension and told the service and client to use the protobufBehavior now I want to check what the client sends to the service (List ob Class Person). Without protobuf the client would send xml style object list to the service. But wiht protobuf its more compact. problem is, when i look into it (with fiddler/wireshark) it still looks like xml example:

<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <SavePersons
            xmlns="http://tempuri.org/">
            <persons
                xmlns:a="http://schemas.datacontract.org/2004/07/TestInterface"
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:Person>
                    <a:Id>0</a:Id>
                    <a:Name>4RAZIW</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4356535+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>1</a:Id>
                    <a:Name>NZORLT</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4366538+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>2</a:Id>
                    <a:Name>WP0IYX</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4366538+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>3</a:Id>
                    <a:Name>ZRJ0X7</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4376546+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>4</a:Id>
                    <a:Name>G1X55H</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4376546+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>5</a:Id>
                    <a:Name>EVV90Q</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4386542+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>6</a:Id>
                    <a:Name>OCERM0</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4386542+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>7</a:Id>
                    <a:Name>4A4XBA</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4396545+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>8</a:Id>
                    <a:Name>P60Y28</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4396545+02:00</a:nextGift>
                </a:Person>
                <a:Person>
                    <a:Id>9</a:Id>
                    <a:Name>3XNBLI</a:Name>
                    <a:aDouble>0.61364615364635655</a:aDouble>
                    <a:addressCollection>
                        <a:Address/>
                    </a:addressCollection>
                    <a:addressList>
                        <a:Address/>
                    </a:addressList>
                    <a:bytearray i:nil="true"/>
                    <a:dataTable i:nil="true"/>
                    <a:nextGift>2017-07-05T13:23:57.4396545+02:00</a:nextGift>
                </a:Person>
            </persons>
        </SavePersons>
    </s:Body>
</s:Envelope>

Any ideas why that is? I have no clue. I addded protobut and setup the behavior. What am i doing wrong, anyone got an idea? Thanks for help in advance. BTW: this is what i used as example to get an idea. https://whinery.wordpress.com/2014/10/28/wcf-with-protobuf-serialization/

This is how i host my service:

try
    {
        //RuntimeTypeModel.Default.Add(typeof(Person), false).Add(1,  nameof(Person.Id)).Add(2, "Name").Add(3, "addressList").Add(4, "nextGift").Add(5, "aDouble").Add(6, "bytearray").Add(7, "addressCollection");

        string address = "http://localhost:8080/SelfHost";

        BasicHttpBinding binding = new BasicHttpBinding();
        ServiceHost host = new ServiceHost(typeof(SelfHost));

        host.AddServiceEndpoint(typeof(ISelfHostTest), binding, address);
        host.Open();
        Console.WriteLine("Host is {0}.\n{1} \nPress enter to close.", host.State, address);
        if (!File.Exists("person.bin"))
            File.Create("person.bin").Dispose();
        if (!File.Exists("Person.bin"))
            File.Create("Person.bin").Dispose();
        Console.ReadLine();

        host.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("???" + ex.Message + "???");
        Console.ReadLine();
    }

This is how i consume my service:

        private ISelfHostTest Connect()
    {
        string address = serviceAdress;//"http://localhost:8080/SelfHost";
        BasicHttpBinding binding = new BasicHttpBinding();            
        ChannelFactory<ISelfHostTest> factory = new ChannelFactory<ISelfHostTest>(binding, address);

        ISelfHostTest client = factory.CreateChannel();
        return client;
    }

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="protoEndpointBehavior">
          <protobuf/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISelfHostTest" receiveTimeout="00:10:00" sendTimeout="0:10:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8080/SelfHost"
          binding="netNamedPipeBinding" bindingConfiguration="BasicHttpBinding_ISelfHostTest"
          contract="TestInterface.ISelfHostTest" name="BasicHttpBinding_ISelfHostTest" behaviorConfiguration="protoEndpointBehavior">
      </endpoint>
    </client>

    <extensions>
      <behaviorExtensions>
        <add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net"/>
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

You're not using the settings from the config files at all. You'll need a different overload of the ChannelFactory constructor on your client for a start:

// first argument must match with a name in the app.config file
var factory = new ChannelFactory<ISelfHostTest>("BasicHttpBinding_ISelfHostTest");

because this is in your config:

<endpoint address="http://localhost:8080/SelfHost"
          binding="netNamedPipeBinding" 
          bindingConfiguration="BasicHttpBinding_ISelfHostTest"
          contract="TestInterface.ISelfHostTest" 
          name="BasicHttpBinding_ISelfHostTest" <!-- This goes into ChannelFactory -->
          behaviorConfiguration="protoEndpointBehavior">
  </endpoint>

A similar issue exist for your service. I don't see a config there so I assume you want to programmatically add the behavior. You can do so by adding the EndpointBehavior to the ServiceDescription Endpoints found on the instance of your ServiceHost:

var host = new ServiceHost(typeof(SelfHost));

var address = "http://localhost:87/SelfHost";
var binding = new BasicHttpBinding();

host.AddServiceEndpoint(typeof(ISelfHostTest), binding, address);

// add EndpointBehavior
host.Description.Endpoints[0].EndpointBehaviors.Add(
     new ProtoBuf.ServiceModel.ProtoEndpointBehavior());

host.Open();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM