C# WCF 服务基地址与端点地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18720810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:01:25  来源:igfitidea点击:

WCF Service Base Address vs endpoint address

c#.netwcfconfiguration

提问by Simsons

What's the difference between the following two cases:

以下两种情况有什么区别:

Configuration 1:

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

Configuration 2:

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

What I understand is In either case base address + endpoint addressresolves to same absolute address

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

But why I get the error on Configuration 2: "No end point is listening at net.tcp://127.0.0.1:808/
but Configuration 1runs the service without any errors!!!

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

Edit 1:

编辑1:

Working Config:

工作配置:

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

Non working Config:

非工作配置:

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

In this case, I have removed base address and provided complete service address (with out .svc path) but get a socket time out error. What's wrong in this case? Does the end point address always need the complete address with .svc when base address is not defined? If so, what could be the reason behind?

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

采纳答案by Brad Christie

baseAddressis just that, the base address for your endpoints (unless specified explicitly). So every <endpoint>will inherit from <baseAddress>(which is why they are usually ""and "mex"). e.g.

baseAddress就是你的端点的基地址(除非明确指定)。所以每个人<endpoint>都会继承自<baseAddress>(这就是为什么它们通常是""and "mex")。例如

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

You now have two endpoints:

您现在有两个端点:

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

By exempting the <baseAddress>you're requiring the <endpoints>to both be fully qualified (including the mex (which is not)). e.g.

通过免除<baseAddress>您要求<endpoints>两者都完全合格(包括墨西哥(不是))。例如

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

You now have two different endpoints:

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

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