[英]C# Protobuf Wcf
我想用wcf測試protobuf。 我用protobuf屬性配置了我的服務和客戶端,並在app-config中添加了擴展名,並告訴服務和客戶端使用protobufBehavior,現在我想檢查客戶端向服務發送的內容(列出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>
您根本沒有使用配置文件中的設置。 首先,您需要在客戶端上對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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.