WCF服务基地址与端点地址

时间:2013-09-10 13:52:27

标签: c# .net wcf configuration

以下两种情况之间的区别是什么:

配置1:

<service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
    <host>
        <baseAddresses>
            <add baseAddress="net.tcp://127.0.0.1:808/" />
        </baseAddresses>
    </host>
    <endpoint address="service"
              binding="netTcpBinding" 
              bindingConfiguration="MainBinding" 
              bindingName="MainBinding" 
              name="DefaultEndpoint" 
              contract="WcfService1.IService1" />
    <endpoint address="mex" 
              binding="mexTcpBinding" 
              contract="IMetadataExchange" />
</service>

配置2:

<service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
    <host>
        <baseAddresses>
            <add baseAddress="net.tcp://127.0.0.1:808/service" />
        </baseAddresses>
    </host>
    <endpoint address="net.tcp://127.0.0.1:808/service" 
              binding="netTcpBinding" 
              bindingConfiguration="MainBinding" 
              bindingName="MainBinding" 
              name="DefaultEndpoint" 
              contract="WcfService1.IService1" />
    <endpoint address="mex" 
              binding="mexTcpBinding" 
              contract="IMetadataExchange" />
  </service>

我的理解是在任何一种情况下基地址+端点地址解析为相同的绝对地址

但为什么我在配置2 上收到错误:“没有终点正在监听net.tcp://127.0.0.1:808 /
配置1 运行服务时没有任何错误 !!!

修改1:

工作配置:

<host>
    <baseAddresses>
        <add baseAddress="net.tcp://127.0.0.1:808/" />
    </baseAddresses>
</host>
<endpoint address="service"
          binding="netTcpBinding" 
          bindingConfiguration="MainBinding" 
          bindingName="MainBinding" 
          name="DefaultEndpoint" 
          contract="WcfService1.IService1" />

非工作配置:

<host>
    <!--
    <baseAddresses>
        <add baseAddress="" />
    </baseAddresses>
    -->
 </host>
 <endpoint address="net.tcp://127.0.0.1:808/service"
           binding="netTcpBinding" 
           bindingConfiguration="MainBinding" 
           bindingName="MainBinding" 
           name="DefaultEndpoint" 
           contract="WcfService1.IService1" />
 <endpoint address="mex" 
           binding="mexTcpBinding" 
           contract="IMetadataExchange" />

在这种情况下,我删除了基址并提供了完整的服务地址(带有.svc路径),但是出现套接字超时错误。这种情况怎么了?当没有定义基址时,端点地址是否始终需要.svc的完整地址?如果是这样,背后的原因可能是什么?

1 个答案:

答案 0 :(得分:15)

baseAddress就是你的端点的基地址(除非明确指定)。因此,每个<endpoint>都会继承<baseAddress>(这就是为什么它们通常为"""mex")。 e.g。

<baseAddresses>
  <add baseAddress="http://127.0.0.1:1337/" />
</baseaddresses>
...
<endpoint address="" contract="MyService.IMyContract" ... />
<endpoint address="mex" contract="IMetadataExchange" ... />

您现在有两个端点:

  • http://127.0.0.1:1337/ - 服务端点
  • http://127.0.0.1:1337/mex - 元数据端点

通过免除<baseAddress>你要求<endpoints>都是完全合格的(包括mex(不是))。 e.g。

<baseAddresses></baseaddresses>
...
<endpoint address="net.tcp://127.0.0.1:1337/" contract="MyService.IMyContract" ... />
<endpoint address="http://127.0.0.1:1337/mex" contract="IMetadataExchange" ... />

您现在有两个不同的端点:

  • net.tcp://127.0.0.1:1337/ - 服务端点
  • http://127.0.0.1:1337/mex - 元数据端点