WCF配置多个终节点
配置多个终节点的意义(自己理解):
一个服务可以有多个终节点,网上也经常有人说终节点才是服务的真正的接口,的确如此,当我们为一个服务配置多个终节点时,就表明这个服务可以被以不同的方式访问(不同的绑定等等),我们配置完服务端后,在客户端引用这个服务(引用服务只是为了让你看明白,它生成了什么东西),你会发现客户端生成了两个endpoint。我们再来讲一下,客户端是怎么去调用的,客户端调用服务时,通常会传一个客户端的endpoint的一个name:
ServiceReference1.Service1Client c = new ServiceReference1.Service1Client("WSHttpBinding_IService1");
这样呢,我们根据这个name的endpoint就可以找到它调用的是服务端的哪个服务。
后面我会再讲一下,一个服务配置多个基址(http,tcp)的情况。
服务端配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="bingding_WS">
<security mode="None" />
</binding>
<binding name="bingding_WS1">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyBehavior"
name="WcfService1.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
</baseAddresses>
</host>
<endpoint address="HelloService" binding="wsHttpBinding" bindingConfiguration="bingding_WS" contract="WcfService1.IService1"></endpoint>
<endpoint address="HelloService1" binding="wsHttpBinding" bindingConfiguration="bingding_WS" contract="WcfService1.IService1"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点-->
<serviceMetadata httpGetEnabled="true" httpGetUrl="false" />
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="bingding_WS">
<security mode="None" />
</binding>
<binding name="bingding_WS1">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyBehavior"
name="WcfService1.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
</baseAddresses>
</host>
<endpoint address="HelloService" binding="wsHttpBinding" bindingConfiguration="bingding_WS" contract="WcfService1.IService1"></endpoint>
<endpoint address="HelloService1" binding="wsHttpBinding" bindingConfiguration="bingding_WS" contract="WcfService1.IService1"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点-->
<serviceMetadata httpGetEnabled="true" httpGetUrl="false" />
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
客户端配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8001/HelloService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" />
<endpoint address="http://localhost:8001/HelloService1" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService11" />
</client>
</system.serviceModel>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8001/HelloService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" />
<endpoint address="http://localhost:8001/HelloService1" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService11" />
</client>
</system.serviceModel>
</configuration>
点我下载源码 (WCF一个服务配置多个终节点.rar)系统有问题,后面上传。