日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF基础知识

發(fā)布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF基础知识 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

管理方式配置終結點

地址格式:[基地址]/[可選的URI]

基地址格式:[傳輸協(xié)議]://[機器名或域名][:可選端口]

以管理方式配置一個終節(jié)點需要將終結點信息放到托管進程的配置文件中。如下面的服務定義:

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>配置節(jié)點中定制的綁定名一致。

服務端綁定的配置

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參數(shù)為string類型,contract參數(shù)為Type類型,binding參數(shù)為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();

元數(shù)據(jù)交換

服務有兩種方案發(fā)布元數(shù)據(jù):一種基于HTTP-GET協(xié)議;一種使用專門的終結點。

WCF能夠為服務提供基于HTTP-GET的元數(shù)據(jù),要顯式的添加服務行為(Behavior)功能。

1管理方式啟用元數(shù)據(jù)交換

配置文件急用元數(shù)據(jù)交換行為

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編程方式啟用元數(shù)據(jù)交換

啟用元數(shù)據(jù)交換行為

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();

元數(shù)據(jù)交換終結點

HTTP-GET發(fā)布元數(shù)據(jù)僅是WCF一特性,并不保證交互的其他平臺會支持。發(fā)布標準方式,通過稱之為元數(shù)據(jù)交換終結點(MEX)發(fā)布。

三個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是什么?

一個要和服務端通訊癿客戶端要做癿第一件事情,就是搞清數(shù)據(jù)要収給誰?目癿地在哪?而Address正是通過一個Uri 來唯一標示一個WCF 癿終節(jié)點(EndPoint)癿,它標示了消息収送癿目癿地。在WCF 數(shù)據(jù)通訊中,它解決了服務在哪里癿問題。

如何在配置文件中指定 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>

配置文件規(guī)范

多個服務的配置文件

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

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的WCF基础知识的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。