WCF服务基址Http和netTcp

10

我的WCF服务配置文件中定义了两个基地址:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>      
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\WCF Service Logs\app_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcp" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000">
          <readerQuotas maxDepth="500" maxStringContentLength="50000000" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateReportService">
        <endpoint address="TemplateService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.ITemplateService"></endpoint>
        <endpoint address="ReportService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.IReportService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/TemplateReportService" />
            <add baseAddress="http://localhost:8181/TemplateReportService"  />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

尽管我已将端点绑定设置为netTcpBinding,但我只能通过基址访问我的WCF服务:

http://localhost:8181/TemplateReportService

与其说不是,不如说是没有

net.tcp://localhost:8001/TemplateReportService

如何使用 netTcp 地址访问我的服务?

2个回答

17

您定义了一个Net.TCP基址:

net.tcp://localhost:8001/TemplateReportService

您的Net TCP终端点为:

<endpoint address="TemplateService" 

<endpoint address="ReportService" 

因此,他们的完整服务地址将是“netTcp基础地址” + “在<endpoint>元素中定义的相对地址”,这样就得到:

net.tcp://localhost:8001/TemplateReportService/TemplateService

而且

net.tcp://localhost:8001/TemplateReportService/ReportService

敬礼。

您可以在这些地址使用它们吗?

此外 - 您为HTTP协议定义了一个"mex"(元数据交换)端点 - 这就是为什么在导航到HTTP地址时会看到某些内容。但是您没有为netTcp指定MEX端点。


谢谢Marc,这是否意味着即使我为两个端点设置了netTcpBinding,但如果我为HttpBinding公开了MetaData,它将在Http地址上公开服务。 - inutan
它将在http地址上公开服务的元数据 - 这意味着您只能使用浏览器导航到那里。NetTcp端点是活动的并且可以使用 - 您只需无法通过浏览器获取任何内容... - marc_s

1
由于没有足够清晰的答案,我决定自己找出最佳解决方案。以下是在IIS 7.0中配置WCF NET TCP服务端点所需执行的步骤:
1. 配置WCF net tcp端点的配置文件 2. 配置net tcp的IIS
1. 配置WCF net tcp端点的配置文件
下面是一个典型的配置文件,注意服务端点中“address”的值为空。
还要注意,在节点“host”内部添加了2个基础地址,分别是http和net tcp协议。由于服务托管在IIS中,您不必担心端点中的绝对地址,IIS会根据我们在节点“host”中定义的基础地址来确定这一点。
    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfig">
          <!--your custom behavior here-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
     <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfig"/>
      </basicHttpBinding>
      <netTcpBinding>
        <!--our netTcpBinding binding-->
        <binding name="netTcpBindingConfig"/>   
      </netTcpBinding>
    </bindings>
    <services>
      <service name="MyNamespace.ServiceLayer.MyService" behaviorConfiguration="behaviorConfig">
        <!--Endpoint for basicHttpBinding-->
        <endpoint address="" binding="basicHttpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="basicHttpBindingConfig"/>
        <!--Endpoint for netTcpBindingConfig-->
        <endpoint address="" binding="netTcpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="netTcpBindingConfig">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
    <!--Make sure you add a mexTcpBinding, without it you will receive an error when trying to resolve service's reference-->
    <endpoint address="mex" 
              binding="mexTcpBinding" 
              bindingConfiguration=""
              name="MyServiceMexTcpBidingEndpoint" 
              contract="IMetadataExchange" />
        <host>
            <!--This part is important for IIS to determine the base addresses for your endpoints-->
            <baseAddresses>            
                 <!--Notice that baseAddress for net tcp is preceded by net.tcp -->
                 <!--then ://localhost:{port} -->
                 <!--Also the same for http, so you can figure out how to define a baseAddress for https-->
               <add baseAddress="net.tcp://localhost:8090"/>
               <add baseAddress="http://localhost:8080"/>
            </baseAddresses>
        </host>
      </service>
    </services>    
  </system.serviceModel>

2. 配置IIS以使用net tcp协议

在IIS中(在更新服务的新配置后),启用net.tcp协议。以下是步骤:

  • 打开IIS。
  • 在站点列表中,找到您托管wcf的“站点”,然后右键单击它
  • 然后选择“编辑绑定”,
  • 出现绑定列表(确保没有net.tcp),然后添加您的net.tcp配置,
  • 点击“添加”,
  • 输入net.tcp(或从下拉列表中选择net.tcp,如果可用)
  • 在绑定信息中输入:8090:*(确保与您添加的主机>基本地址中的端口相同)
  • 然后关闭此窗口并重新启动服务。

完成此操作后,还有另一个配置:

  • 打开IIS
  • 在站点列表中,找到您托管wcf的“站点”,然后右键单击它
  • 点击“高级设置”
  • 选择“启用的协议”
  • 将值设置为http,net.tcp(确保http,net.tcp之间没有空格)
  • 重新启动服务

另外,如果您仍无法访问服务,则可能需要在服务器上启动您的Net.tcp侦听器适配器。为了实现这一点,请按照以下步骤进行:

  • 转到ServerManager -> Configuration -> Services,停止Net.Tcp Listener Adapter和Net.Tcp Port Sharing Service。然后再次启动它们。

这就是使nettcp WCF端点在IIS中启用所需做的全部操作。

希望这有所帮助。

敬礼


我不理解你在http://stackoverflow.com/questions/18575496/could-not-find-a-base-address-that-matches-scheme-net-tcp-for-the-endpoint-with/18575819#18575819中对我的回答所做的修改。在推广之前,请检查您的状态。IIS6不支持NET.TCP!请参见http://msdn.microsoft.com/en-us/library/ms730158.aspx。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接