突然发现WCF中WCF服务、宿主程序、客户端的关系配置与现实中的雇凶杀人模式很像,对应关系如下:
雇凶者想雇佣杀手杀掉某人,首先找到代理人,代理机构指派杀手执行任务。同样,客户端要执行一个操作,首先找到宿主程序,宿主程序调用WCF服务执行操作。
整个过程中,雇凶者可选择互联网、熟人关系方式找到代理机构,而且不用知道杀手的信息,而代理机构只要提供与雇凶者得联系方式和知道怎样指派杀手就行了。客户端可以选择Internet HTTP协议或Intranet TCP协议与宿主程序交互(并不止这两种协议),并不关心WCF服务,而宿主程序只是提供了几个endpoint节点(联系方式)供客户端选择,客户端选择任何一个endpoint都可成功执行最终操作,另外宿主程序是怎样引用WCF服务的,也无需客户端知道。
结合最简单的一个例子来看看这个过程,这个例子中,宿主程序提供了Http和Tcp两种协议供客户端选择调用。
1、 在VS2012中,新建一个WCF服务库项目,删除掉自动生成的App.config文件,这里并不需要配置,我们之后会在宿主程序里的App.config里配置。因为是测试,其他不用修改,用自动生成的服务就可以了。在这里,可以看到,WCF服务和杀手一样,只是负责做事的,不管外部怎样调用。
2、 在解决方案里,再添加一个Windows应用程序项目,用来做为宿主程序,添加对System.ServiceModel和WCF服务库项目的引用。添加对WCF服务的引用就像代理机构知道了怎样指派杀手。
3、 宿主程序项目中新建App.config文件,配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8000"/>
<add baseAddress="http://localhost:8001/Service1"/>
</baseAddresses>
</host>
<endpoint
binding="wsHttpBinding"
contract="WcfServiceLibrary1.IService1"/>
<endpoint
binding="netTcpBinding"
contract="WcfServiceLibrary1.IService1"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
宿主程序中,定义了两个endpoint节点,即定义了两个供客户端选用的WCF访问节点,第一个是HTTP的,第二个是TCP的。这里宿主程序提供了两种协议,就像代理机构提供了互联网、熟人关系两个联系方式供雇凶者找到杀手。(顺便说一下第三个endpoints,它是用来让外界快速知道WCF接口的,在VS2012中的客户端添加服务引用是,就是通过它生成的客户端配置文件,当WCF部署后,应该把这个endpoint节点删除掉,以保证安全)
4、 宿主程序的Form1中添加代码,来打开服务。(具体请看源代码,最后有链接)
ServiceHost host ;
host=new ServiceHost(typeof(Service1));
host.Open();
5、 在资源管理器中将宿主程序debug复制到别处,启动宿主程序,启动后才能在客户端添加服务引用。
6、 下一步建立客户端程序,在解决方案中再添加一个Window应用程序,命名为MyClient。然后添加服务引用,因为宿主程序的配置文件中用的是地址为mex的节点用的是mexTcpBinding,所以引用地址为:net.tcp://localhost:8000/mex。(也可以将宿主中改为mexHttpBinding,那就得用第二个http://localhost:8001/Service1/mex添加服务引用了。)
7、 添加引用后,客户端自动生成一下配置文件,我们应该看一下。
<?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服务的信息。
这里就像雇凶者,可以用互联网或者熟人关系等方式联系代理机构,从而调用杀手执行任务,不用知道杀手的信息,只知道代理机构的信息就行了。
此实例下载:实例下载