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 地址访问我的服务?

4

2 回答 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 端点。

于 2010-01-06T12:27:58.320 回答
1

由于没有足够清晰的答案,我决定自己找出最好的解决方案。这是在IIS 7.0中配置 WCF NET TCP 服务终结点所必须执行的操作。

为此,我们需要两个步骤:

  1. 为 WCF net tcp 端点配置配置文件
  2. 为 net tcp 配置 IIS

1.为WCF net tcp端点配置配置文件

下面是一个典型的配置文件,请注意服务端点中“地址”的值是空的。

另请注意,在节点“主机”内,我分别添加了 2 个基地址、一个 http 和一个 net tcp 协议。由于服务托管在 IIS 中,您不必担心端点中的绝对地址,IIS 会根据我们在节点“主机”中定义的基地址来计算这一点。

    <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.为net tcp配置IIS

在 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。然后重新启动它们。

这就是在 IIS 中启用 nettcp WCF 端点所要做的一切。

希望这可以帮助。

问候

于 2014-07-24T15:21:30.070 回答