配置多個終節點的意義(自己理解):
一個服務可以有多個終節點,網上也經常有人說終節點才是服務的真正的接口,的確如此,當我們為一個服務配置多個終節點時,就表明這個服務可以被以不同的方式訪問(不同的綁定等等),我們配置完服務端后,在客戶端引用這個服務(引用服務只是為了讓你看明白,它生成了什么東西),你會發現客戶端生成了兩個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)系統有問題,后面上傳。