[英]C# Protobuf Wcf
I want to test protobuf with wcf. 我想用wcf测试protobuf。 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).
我用protobuf属性配置了我的服务和客户端,并在app-config中添加了扩展名,并告诉服务和客户端使用protobufBehavior,现在我想检查客户端向服务发送的内容(列出ob Class Person)。 Without protobuf the client would send xml style object list to the service.
如果没有protobuf,客户端将向服务发送xml样式对象列表。 But wiht protobuf its more compact.
但是用protobuf使其更加紧凑。 problem is, when i look into it (with fiddler/wireshark) it still looks like xml example:
问题是,当我使用fiddler / wireshark进行查看时,它仍然看起来像xml示例:
<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.
我添加了protobut并设置了行为。 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/
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:
首先,您需要在客户端上对ChannelFactory构造函数进行其他重载:
// 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:
您可以通过将EndpointBehavior添加到在ServiceHost实例上找到的ServiceDescription端点来实现:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.