2016-08-20 33 views
0

我想爲自我託管的服務創建通道工廠。這是我App.config文件:WCF無法創建給定端點名稱的通道工廠

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="mexBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="mexBehavior" name="Service.TexasHoldemService"> 
     <endpoint address="TexasHoldem" binding="netTcpBinding" bindingConfiguration="" 
      contract="Service.ITexasHoldemService" name="TexasHoldem"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080" /> 
      <add baseAddress="net.tcp://localhost:8090" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 

這是我如何承載服務

host = new ServiceHost(typeof (Service.TexasHoldemService)); 
host.Open() 

var factory = new ChannelFactory<ITexasHoldemService>("TexasHoldem"); 

但是,我得到一個例外是這樣的:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll 

Could not find endpoint element with name 'TexasHoldem' and contract 'Service.ITexasHoldemService' 
in the ServiceModel client configuration section. This might be because no configuration file was 
found for your application, or because no endpoint element matching this name could be 
found in the client element. 

我也嘗試過在ChannelFactory的構造函數中設置地址爲:

http://localhost/TexasHoldem 
http://localhost/Service.TexasHoldemService/TexasHoldem 
net.tcp://localhost/TexasHoldem 
net.tcp://localhost/Service.TexasHoldemService/TexasHoldem 

,沒有上述作品;/

回答

1

您需要添加一個<endpoint>一個<client>元素下能夠從ChannelFactory稱之爲:

添加以下代碼<system.serviceModel>下:

<client> 
    <endpoint address="net.tcp://localhost:8090/TexasHoldem" binding="netTcpBinding" bindingConfiguration="" contract="Service.ITexasHoldemService" name="TexasHoldem"> 
    </endpoint> 
    </client> 
+0

我做你的建議,當我與「net.tcp:// localhost:8090/TexasHoldem」創建工廠時,我得到了同樣的錯誤:(當我創建工廠與「TexasHoldem」字符串作爲端點地址,我得到一個異常該URI必須是絕對的。 – Zwierzak

+0

負責託管的配置中的一段代碼必須具有地址=「net.tcp:// localhost:8090/TexasHoldem」,並且客戶端點也應該指向相同的地址。這不是有效的地址爲WCF服務 - 地址=「TexasHoldem」 –