开篇:
最近工作中使用WCF比较多,有了一点收获,也有了一点浅薄的想法,在此写出,希望能给大家带来一些收获。
本章说明:
实际开发中可能会出现,一个服务端需应付多个不同协议客户端,下文就是这种场景的一个解决方案。
Binding所对应的协议版本:
Binding Class Name |
Transport |
Message Encoding |
Message Version |
Security Mode |
Reliable Messaging |
Transaction Flow (disabled by default) |
Example Application? |
BasicHttpBinding |
HTTP |
Text |
SOAP 1.1 |
None |
Not Supported |
Not Supported |
? |
WSHttpBinding |
HTTP |
Text |
SOAP 1.2 WS-Addressing 1.0 |
Message |
Disabled |
WS-AtomicTransactions |
? |
WSDualHttpBinding |
HTTP |
Text |
SOAP 1.2 WS-Addressing 1.0 |
Message |
Enabled |
WS-AtomicTransactions |
? |
WSFederationHttpBinding |
HTTP |
Text |
SOAP 1.2 WS-Addressing 1.0 |
Message |
Disabled |
WS-AtomicTransactions |
? |
NetTcpBinding |
TCP |
Binary |
SOAP 1.2 |
Transport |
Disabled |
OleTransactions |
? |
NetPeerTcpBinding |
P2P |
Binary |
SOAP 1.2 |
Transport |
Not Supported |
Not Supported |
? |
NetNamedPipesBinding |
Named Pipes |
Binary |
SOAP 1.2 |
Transport |
Not Supported |
OleTransactions |
? |
NetMsmqBinding |
MSMQ |
Binary |
SOAP 1.2 |
Message |
Not Supported |
Not Supported |
? |
MsmqIntegrationBinding |
MSMQ |
Not Supported (uses a pre-WCF serialization format) |
Not Supported |
Transport |
Not Supported |
Not Supported |
? |
CustomBinding |
You Decide |
You Decide |
You Decide |
You Decide |
You Decide |
You Decide |
? |
服务端配置:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="wsHttpBindingConfiguration" maxReceivedMessageSize="2147483647"> <security mode="None"> </security> </binding> </wsHttpBinding> <basicHttpBinding> <binding name="basicHttpBindingConfiguration" maxReceivedMessageSize="2147483647"> <security mode="None"> </security> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="behaviorConfiguration"> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceMetadata httpGetBinding="webHttpBinding" httpGetBindingConfiguration="" httpGetEnabled="true" /> <serviceThrottling maxConcurrentSessions="10000" maxConcurrentInstances="10000" maxConcurrentCalls="10000" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="behaviorConfiguration" name="WcfServer.UserService" > <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration" contract="WcfServer.IUserService" /> <endpoint address="soap11" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" contract="WcfServer.IUserService" /> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
客户端配置:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="wsHttpBindingConfiguration" > <security mode="None"> </security> </binding> </wsHttpBinding> <basicHttpBinding> <binding name="basicHttpBindingConfiguration"> <security mode="None"> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost/WcfServer/UserService.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration" contract="WcfServer.IUserService" name="UserService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="http://localhost/WcfServer/UserService.svc/Soap11" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" contract="WcfServer.IUserService" name="UserServiceSoap11"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>