用雇凶杀人的例子来理解WCF中服务、宿主、客户端关系与配置
2012-04-14 20:11
337 查看
突然发现WCF中WCF服务、宿主程序、客户端的关系配置与现实中的雇凶杀人模式很像,对应关系如下:
View Code
因为添加服务引用时,VS2012发现宿主程序配置文件中有两个endpoint节点,一个是wsHttpBinding、一个是netTcpBinding,所以在客户端配置中生成两个对应的Binding和两个对应的client。
这样,客户端就有了两种方式通过宿主程序调用WCF服务。
8、 下一步来看一下客户端怎样选用HTTP、TCP两种方式调用。在客户端程序的Form1中,添加两个按钮,(具体请看源代码,最后有链接),第一个按钮要用TCP调用,单击事件中添加代码:
用的是客户端配置文件中bindingConfiguration为NetTcpBinding_IService1的节点,走的是TCP。
第二个按钮要用HTTP调用,单击事件中添加代码:
用的是客户端配置文件中bindingConfiguration为WSHttpBinding_IService1的节点,走的是HTTP。
整个客户端配置文件中,只有与宿主程序关联的信息,而没有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服务的信息。
这里就像雇凶者,可以用互联网或者熟人关系等方式联系代理机构,从而调用杀手执行任务,不用知道杀手的信息,只知道代理机构的信息就行了。
此实例下载:实例下载
相关文章推荐
- WCF服务类库、宿主、web客户端配置
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序客户端运行详细配置参考
- WCF服务生成客户端的配置文件步骤
- WCF——1.3 WCF服务、WinForm宿主、WinForm客户端
- WCF服务在宿主程序中的配置说明
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序客户端运行详细配置参考
- WCF随客户端软件一起发布,客户端自动识别WCF服务地址,不通过配置文件绑定WCF服务,客户端动态获取版本号
- [问题]通过IIS宿主发布WCF服务,客户端添加服务引用出错的解决办法
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序客户端运行详细配置参考
- WPF宿主、WCF服务、SilverLight客户端搭建完善聊天室
- WCF——1.2 WCF服务、控制台宿主、控制台客户端
- 2008年3月小记(设置Windows服务的依赖关系,配置MSMQ的访问权限,WCF的安全配置,删除重建网站,HttpWebRequest,一年中的周,在WCF中使用HttpContext,String.Join)
- 2008年3月小记(设置Windows服务的依赖关系,配置MSMQ的访问权限,WCF的安全配置,删除重建网站,HttpWebRequest,一年中的周,在WCF中使用HttpContext,String.Join)
- WCF客户端调用服务时配置产生的错误
- CentOS5.5下安装zabbix客户端并配置成SysV服务启动
- WCF系列(一) -- 完全不使用配置文件构建和使用WCF服务
- 使用WindowsService为宿主实装WCF 服务
- 客户端动态调用WCF服务中的方法
- WCF RIA 服务 (二十八)-- Silverlight 客户端 9
- WCF分布式开发步步为赢(3)WCF服务元数据交换、配置及编程开发