您的位置:首页 > 其它

用雇凶杀人的例子来理解WCF中服务、宿主、客户端关系与配置

2012-04-14 20:11 337 查看
突然发现WCF中WCF服务、宿主程序、客户端的关系配置与现实中的雇凶杀人模式很像,对应关系如下:

View Code

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8001/Service1" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="Ref.IService1"
name="WSHttpBinding_IService1">
<identity>
<userPrincipalName value="QINGXIN\Admin" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:8000/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService1" contract="Ref.IService1"
name="NetTcpBinding_IService1">
<identity>
<userPrincipalName value="QINGXIN\Admin" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>


因为添加服务引用时,VS2012发现宿主程序配置文件中有两个endpoint节点,一个是wsHttpBinding、一个是netTcpBinding,所以在客户端配置中生成两个对应的Binding和两个对应的client。

这样,客户端就有了两种方式通过宿主程序调用WCF服务。

8、 下一步来看一下客户端怎样选用HTTP、TCP两种方式调用。在客户端程序的Form1中,添加两个按钮,(具体请看源代码,最后有链接),第一个按钮要用TCP调用,单击事件中添加代码:

Ref.Service1Client client = new Ref.Service1Client("NetTcpBinding_IService1");
MessageBox.Show(client.GetData(1));


用的是客户端配置文件中bindingConfiguration为NetTcpBinding_IService1的节点,走的是TCP。

第二个按钮要用HTTP调用,单击事件中添加代码:

Ref.Service1Client client = new Ref.Service1Client("WSHttpBinding_IService1");
MessageBox.Show(client.GetData(1));


用的是客户端配置文件中bindingConfiguration为WSHttpBinding_IService1的节点,走的是HTTP。

整个客户端配置文件中,只有与宿主程序关联的信息,而没有WCF服务的信息。

这里就像雇凶者,可以用互联网或者熟人关系等方式联系代理机构,从而调用杀手执行任务,不用知道杀手的信息,只知道代理机构的信息就行了。



此实例下载:实例下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐