我想用wcf测试protobuf。 我使用protobuf attirbutes配置我的服务和客户端,并在app-config中添加了扩展,并告诉服务和客户端现在使用protobufBehavior我想检查客户端发送给服务的内容(List ob Class Person)。 如果没有protobuf,客户端会将xml样式对象列表发送到服务。 但是protobuf更紧凑。 问题是,当我调查它(使用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>
为什么会这样? 我没有线索。 我添加了protobut并设置了行为。 我做错了什么,有人有个主意吗? 提前感谢您的帮助。 顺便说一句:这就是我用来获得想法的例子。 https://whinery.wordpress.com/2014/10/28/wcf-with-protobuf-serialization/
这就是我托管服务的方式:
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();
}
这就是我使用我服务的方式:
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>
答案 0 :(得分:0)
您根本没有使用配置文件中的设置。您需要在客户端上使用ChannelFactory构造函数的不同重载作为开头:
// first argument must match with a name in the app.config file
var factory = new ChannelFactory<ISelfHostTest>("BasicHttpBinding_ISelfHostTest");
因为这在你的配置中:
<endpoint address="http://localhost:8080/SelfHost"
binding="netNamedPipeBinding"
bindingConfiguration="BasicHttpBinding_ISelfHostTest"
contract="TestInterface.ISelfHostTest"
name="BasicHttpBinding_ISelfHostTest" <!-- This goes into ChannelFactory -->
behaviorConfiguration="protoEndpointBehavior">
</endpoint>
您的服务存在类似的问题。我没有看到配置,所以我假设你想以编程方式添加行为。您可以通过将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();