2011-11-22 68 views
1

當我在我的WCF服務的終端數量...WCF多個綁定端口號

難道是明智的或可能resuse相同的端口號碼他們。

問題是服務部署時,有太多的端口號用於記錄所使用的不同綁定。

回答

5

這當然是可能的,我也會說明智 - 特別是如果您將其作爲端口80上的Web服務託管,但即使是TCP也是如此。每項服務都有一個端口對我來說似乎總是矯枉過正。

你將需要每個綁定的端口,但(選擇一個端口用於TCP,一個用於HTTP等)。

您可以像這樣爲你服務指定相同的根地址(這是一個JSON REST服務,但結合是無關緊要的) - 注意地址屬性:

<system.serviceModel> 
    <services> 
    <service name="Demo.SampleService2Implementation"> 
     <endpoint address="http://localhost:85/sample2" 
       behaviorConfiguration="json" 
       binding="webHttpBinding" 
       name="jsonEndpoint2" 
       contract="Demo.ISampleService2" /> 
    </service> 

    <service name="Demo.SampleServiceImplementation"> 
     <endpoint address="http://localhost:85/sample1" 
       behaviorConfiguration="json" 
       binding="webHttpBinding" 
       name="jsonEndpoint1" 
       contract="Demo.ISampleService" /> 
    </service> 
    </services> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="json"> 
     <webHttp/> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 

這是客戶端配置:

<system.serviceModel> 
    <client> 
    <endpoint name="SampleServiceEndpoint" 
       address="http://localhost:85/sample1" 
       binding="webHttpBinding" 
       contract="Demo.ISampleService" 
       behaviorConfiguration="json"> 
    </endpoint> 

    <endpoint name="SampleServiceEndpoint2" 
       address="http://localhost:85/sample2" 
       binding="webHttpBinding" 
       contract="Demo.ISampleService2" 
       behaviorConfiguration="json"> 
    </endpoint> 
    </client> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="json"> 
     <webHttp/> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 
1

如果您在一個服務中有多個端點,也許使用不同的合同或綁定,那麼您可以使用基地址進行相對尋址,如下所示。

<services> 
    <service name="CalculatorService"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:8000/CalculatorService"/> 
     <add baseAddress="net.tcp://localhost:8001/CalculatorService"/> 
     </baseAddresses> 
    </host> 
    <endpoint address="Mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    <endpoint address="Basic" binding="basicHttpBinding" contract="IBasicCalculator" /> 
    <endpoint address="Scientific" binding="netTcpBinding" contract="IScientificCalculator" /> 
    </service> 
</services>