【问题标题】:WCF Multiple Bindings Port numbersWCF 多个绑定端口号
【发布时间】:2011-11-22 22:33:25
【问题描述】:

当我的 WCF 服务中有许多端点时...

为他们重复使用相同的端口号是否明智或可能。

问题是在部署服务时,对于使用的不同绑定,有太多端口号无法记住。

【问题讨论】:

    标签: wcf binding numbers port


    【解决方案1】:

    这当然是可能的,我也会说是明智的 - 特别是如果您将它作为 Web 服务托管在端口 80 上,甚至 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>
    

    【讨论】:

      【解决方案2】:

      如果您在一项服务中有多个端点,可能具有不同的合同或绑定,那么您可以使用使用基地址的相对寻址,如下所示。

      <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>
      

      【讨论】:

        猜你喜欢
        • 2016-01-17
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 2015-04-26
        相关资源
        最近更新 更多