WCF基础知识
管理方式配置終結點
地址格式:[基地址]/[可選的URI]
基地址格式:[傳輸協議]://[機器名或域名][:可選端口]
以管理方式配置一個終節點需要將終結點信息放到托管進程的配置文件中。如下面的服務定義:
View Code namespace Mynamespace{
[ServiceContract]
interface IMyContract
{。。。}
class MyService: IMyContract
{。。。}
}
管理方式配置終結點
<system.serviceModel><services>
<service name="MyNamespace.MyServices">
<endpoint
address="http://localhost:8000/MyService"
binging="wsHttpBinging"
Contract="MyNamespace.IMyContract"
/>
</service>
</services>
</system.serviceModel>
相同服務的多個終結點
View Code <system.serviceModel><endpoint
address="http://localhost:8000/MyService"
binging="wsHttpBinging"
contract="IMyContract"
/>
<endpoint
address="net.tcp://localhost:8001/MyService"
binging="netTcpBinging"
contract="IMyContract"
/>
<endpoint
address="net.tcp://localhost:8001/MyService"
binging="netTcpBinging"
contract="IMyOtherContract"
/>
</system.serviceModel>
配置綁定
使用配置文件可以為終結點使用的綁定進行定制。添加bindingConfiguration屬性。它的值要與<bindings>配置節點中定制的綁定名一致。
服務端綁定的配置
View Code <system.serviceModel><services>
<service name="MyService">
<endpoint
address="net.tcp://localhost:8000/MyService"
bindingConfiguration="TransactionalTCP"
binding="netTcpBinding"
contract="IMyContract"
/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TransactionalTCP"
transactionFlow="true"
/>
</netTcpBinding>
</bindings>
</system.serviceModel>
編程方式配置終結點
?
View Code ServiceHost host = new ServiceHost(typeof(MyService));Binding wsBinding = new WSHttpBinding();
Binding tcpBinding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(IMyContract), wsBinding, "http://localhost:8000/MyService");
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8000/MyService");
host.Open();
address參數為string類型,contract參數為Type類型,binding參數為Binding抽象類的一個子類。
由宿主提供了基地址,只使用基地址
View Code Uri tcpBaseAddress = new Uri("net.tcp://localhost:8000/");ServiceHost host = new ServiceHost(typeof(MyService), tcpBaseAddress);
Binding tcpBinding = new NetTcpBinding();
//使用基地址作為地址
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "");
//添加相對地址
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "MyService");
//忽略基地址
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8000/MyService");
host.Open();
綁定配置
編程方式設置綁定屬性。下面啟用事務傳播
View Code ServiceHost host = new ServiceHost(typeof(MyService), tcpBaseAddress);NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.TransactionFlow = true;
host.AddServiceEndpoint(typeof(IMyContract), tcpBinding, "net.tcp://localhost:8000/MyService");
host.Open();
元數據交換
服務有兩種方案發布元數據:一種基于HTTP-GET協議;一種使用專門的終結點。
WCF能夠為服務提供基于HTTP-GET的元數據,要顯式的添加服務行為(Behavior)功能。
1管理方式啟用元數據交換
配置文件急用元數據交換行為
View Code <system.serviceModel><services>
<service name="MyService" behaviorConfiguration="MEXGET">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
</service>
<service name="MyOtherService" behaviorConfiguration="MEXGET">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MEXGET">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
2編程方式啟用元數據交換
啟用元數據交換行為
View Code ServiceHost host = new ServiceHost(typeof(MyService));ServiceMetadataBehavior behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (behavior == null)
{
behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
}
host.Open();
元數據交換終結點
HTTP-GET發布元數據僅是WCF一特性,并不保證交互的其他平臺會支持。發布標準方式,通過稱之為元數據交換終結點(MEX)發布。
三個MEX終結點,分別基于HTTP、TCP和IPC。第一個使用絕對地址,后兩個使用相對地址。
View Code <system.serviceModel><services>
<service name="MyService" behaviorConfiguration="MEX">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8001/"/>
<add baseAddress="net.pipe://localhost/"/>
</baseAddresses>
</host>
<endpoint
address="MEX"
binding="mexTcpBinding"
contract="IMetadataExchang"
/>
<endpoint
address="MEX"
binding="mexNamePipeBinding"
contract="IMetadataExchang"
/>
<endpoint
address="http://localhost:8000/MEX"
binding="mexHttpBinding"
contract="IMetadataExchang"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MEX">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
編程方式添加MEX終結點
View Code BindingElement bindingElement = new TcpTransportBindingElement();CustomBinding binding = new CustomBinding(bindingElement);
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000/");
ServiceHost host = new ServiceHost(typeof(MyService), tcpBaseAddress);
ServiceMetadataBehavior behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (behavior == null)
{
behavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(behavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
host.Open();
客戶端編程
管理方式配置客戶端
客戶端需要知道服務的位置與服務相同的綁定還要導入服務的七月定義。配置文件如下:
View Code <system.serviceModel><client>
<endpoint name="MyEndpoint"
address="http://localhost:8000/MyService"
binding="wsHttpBiding"
contract="IMyContract"
/>
</client>
</system.serviceModel>
綁定配置
View Code <system.serviceModel><client>
<endpoint name="FirstEndpoint"
address="net.tcp://localhost:8000/MyService"
behaviorConfiguration="TransactionTCP"
binding="netTcpBiding"
contract="IMyContract"
/>
</client>
<bindings>
<netTcpBinding>
<binding name="TransactionTCP"
transactionFlow="true"
/>
</netTcpBinding>
</bindings>
</system.serviceModel>
調用超時
View Code <system.serviceModel><client>
<endpoint name="FirstEndpoint"
. . . .
behaviorConfiguration="LongTimeout"
binding="wsHttpBinding"
. . . .
/>
</client>
<bindings>
<wsHttpBinding>
<binding name="LongTimeout" sendTimeout="00:05:00" />
</wsHttpBinding>
</bindings>
</system.serviceModel>
Address是什么?
一個要和服務端通訊癿客戶端要做癿第一件事情,就是搞清數據要収給誰?目癿地在哪?而Address正是通過一個Uri 來唯一標示一個WCF 癿終節點(EndPoint)癿,它標示了消息収送癿目癿地。在WCF 數據通訊中,它解決了服務在哪里癿問題。
如何在配置文件中指定 Address?
在配置文件中,有兩種方弅可以挃定 Address,一種是絕對地址方弅,另外是相對地址方弅,凾刪如下:
絕對地址???
?<host>
????????? <baseAddresses>
??????????? <add baseAddress = "http://localhost:8731/" />
????????? </baseAddresses>
?? </host>
?? <endpoint address ="http://localhost:8731/Service" binding="basicHttpBindi
ng" contract="Wcf_Address_Config.IService1"> </endpoint>
相對地址???
?<host>
????????? <baseAddresses>
??????????? <add baseAddress = "http://localhost:8731/" />
????????? </baseAddresses>
? </host>
? <endpoint address ="Service1" binding="basicHttpBinding" contract="Wcf_A
ddress_Config.IService1"></endpoint>
配置文件規范
多個服務的配置文件
View Code <system.serviceModel><services>
<service name="WCFService.WCFServicePerCall" behaviorConfiguration="WCFService.WCFServiceBehavior">
<endpoint
address="net.tcp://localhost:9001/WCFServicePerCall"
binding="netTcpBinding"
contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9001/"/>
</baseAddresses>
</host>
</service>
<service name="WCFService.WCFServicePerSession" behaviorConfiguration="WCFService.WCFServiceBehavior">
<endpoint
address="net.tcp://localhost:9002/WCFServicePerSession"
binding="netTcpBinding"
contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9002/"/>
</baseAddresses>
</host>
</service>
<service name="WCFService.WCFServiceSingleTon" behaviorConfiguration="WCFService.WCFServiceBehavior">
<endpoint
address="net.tcp://localhost:9003/WCFServiceSingleTon"
binding="netTcpBinding"
contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9003/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
轉載于:https://www.cnblogs.com/sjllef/archive/2011/03/04/1970560.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: java开启新线程的三种方法
- 下一篇: xcode windows版安装使用教程