簡體   English   中英

WCF服務基地址Http和netTcp

[英]WCF Service Base Address Http and netTcp

我在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地址進行服務訪問?

您將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端點。

由於沒有足夠清楚的答案,我決定自己找出最好的解決方案。 以下是在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.為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 - >配置 - >服務,停止Net.Tcp偵聽器適配器和Net.Tcp端口共享服務。 然后再次啟動它們。

這就是在IIS中啟用nettcp WCF端點所需要做的全部工作。

希望這可以幫助。

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM