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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF基础知识

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

管理方式配置終結(jié)點(diǎn)

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

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

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

View Code namespace Mynamespace
{
[ServiceContract]
interface IMyContract
{。。。}
class MyService: IMyContract
{。。。}
}

管理方式配置終結(jié)點(diǎn)

<system.serviceModel>
<services>
<service name="MyNamespace.MyServices">
<endpoint
address
="http://localhost:8000/MyService"
binging
="wsHttpBinging"
Contract
="MyNamespace.IMyContract"
/>
</service>
</services>
</system.serviceModel>

相同服務(wù)的多個(gè)終結(jié)點(diǎn)

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>

配置綁定

使用配置文件可以為終結(jié)點(diǎn)使用的綁定進(jìn)行定制。添加bindingConfiguration屬性。它的值要與<bindings>配置節(jié)點(diǎn)中定制的綁定名一致。

服務(wù)端綁定的配置

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>

編程方式配置終結(jié)點(diǎn)

?

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抽象類的一個(gè)子類。

由宿主提供了基地址,只使用基地址

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

綁定配置

編程方式設(shè)置綁定屬性。下面啟用事務(wù)傳播

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ù)交換

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

WCF能夠?yàn)榉?wù)提供基于HTTP-GET的元數(shù)據(jù),要顯式的添加服務(wù)行為(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ù)交換終結(jié)點(diǎn)

HTTP-GET發(fā)布元數(shù)據(jù)僅是WCF一特性,并不保證交互的其他平臺(tái)會(huì)支持。發(fā)布標(biāo)準(zhǔn)方式,通過稱之為元數(shù)據(jù)交換終結(jié)點(diǎn)(MEX)發(fā)布。

三個(gè)MEX終結(jié)點(diǎn),分別基于HTTP、TCP和IPC。第一個(gè)使用絕對地址,后兩個(gè)使用相對地址。

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終結(jié)點(diǎn)

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

客戶端編程

管理方式配置客戶端

客戶端需要知道服務(wù)的位置與服務(wù)相同的綁定還要導(dǎo)入服務(wù)的七月定義。配置文件如下:

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>

調(diào)用超時(shí)

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

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

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

多個(gè)服務(wù)的配置文件

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>

轉(zhuǎn)載于:https://www.cnblogs.com/sjllef/archive/2011/03/04/1970560.html

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。