WCF:如何將net.tcp協議寄宿到IIS


1 部署IIS

1.1 安裝WAS

IIS原本是不支持非HTTP協議的服務,為了讓IIS支持net.tcp,必須先安裝WAS(Windows Process Activation Service),即windows進程激活服務。

打開控制面板--程序和功能--打開或關閉windows功能,安裝WAS,如圖:

安裝完畢后在Services窗口中可以到到如下服務:Windows Process Activation Service;Net.Msmq Listener Adapter;Net.Pipe Listener Adapter;Net.Tcp Listener Adapter;Net.Tcp Port Sharing Service.這幾個服務。確定Net.Tcp Listener Adapter 與Net.Tcp Port Sharing Service是否已經啟動。

1.2  確定WCF是否啟用Non-Http支持

同樣是在控件面板中打開這個功能,如圖:

 

1.3 給站點添加net.tcp綁定

在IIS中,選中你的網站,然后在右邊的操作菜單欄中單擊綁定,會彈出一個“網站綁定”窗口,點擊添加,類型選擇net.tcp

1.4 啟用net.tcp協議

 選擇你的網站,點擊“高級設置”,彈出的的窗體中,在“已啟用的協議”一欄中手動添加:net.tcp

2 測試服務

 2.1 新建服務

用VS2010新建一個WCF服務,為了簡單,我就直接用VS默認生成的作測試了。只有一個GetData方法

下面是配置的Config:

View Code
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    <protocolMapping>
      <add scheme="tcp" binding="netTcpBinding"/>
    </protocolMapping>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindConfig"  closeTimeout="00:30:00" portSharingEnabled="true"
                openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10">
          
          <readerQuotas maxDepth="2147483647"
                                    maxStringContentLength="2147483647"
                                    maxArrayLength="2147483647"
                                    maxBytesPerRead="2147483647"
                                    maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true"  inactivityTimeout="00:01:00" enabled="false" />
          <security mode="None">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"  />
            <message clientCredentialType="Windows"  />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>

      <service behaviorConfiguration="MyBehavior" name="WCFService.Service1">
        <endpoint address="" binding="netTcpBinding" contract="WCFService.IService1" bindingConfiguration="netTcpBindConfig"></endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint>
      </service>

    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior" >
          <serviceMetadata/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="6553600"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

2.2 發布服務

將服務發布到IIS,在瀏覽器中訪問服務,如果訪問正常就說明服務部署成功,如圖:

2.3 測試服務

 新建一個控制台項目,測試服務。添加服務

測試服務正常。

3 遇到的問題

問題1找不到具有綁定 NetTcpBinding 的終結點的與方案 net.tcp 匹配的基址。注冊的基址方案是 [http]。

 這可能是你的網站中沒有啟用net.tcp協議所到致,也就是少了上面的1.4.

問題2:未找到 URI“net.tcp://gyoung/Service1.svc/mex”的兼容 TransportManager。這可能是因為使用了指向虛擬應用程序外部的絕對地址,或終結點的綁定設置與其他服務或終結點所設置的綁定設置不匹配。 請注意,同一協議的所有綁定在同一應用程序中應具有相同的設置。

 這個問題我並沒有找到真正的原因,應該是binding設置的原因,我原先的binding配置是:

<binding name="netTcpBindConfig"  closeTimeout="00:30:00" portSharingEnabled="true"
                openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
          maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                maxReceivedMessageSize="2147483647">

這樣的話會出現上面的錯誤,但當我將后面四個節點去掉后,即變成:

<binding name="netTcpBindConfig"  closeTimeout="00:30:00" portSharingEnabled="true"
                openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10">

就沒有報這個錯誤了。最后一個問題,園子里哪位大神知道具體原因的,求指導~

問題3有沒有必要綁定host地址?

之前我在service節點下有增加host地址

      <host>
          <baseAddresses>
            <add baseAddress="http://localhost:4504"/>
            <add baseAddress="net.tcp://localhost:808/Service1.svc"/>
          </baseAddresses>
        </host>

但我發現這根本不起作用,因不不管我怎么設置,最后我的net.tcp地址都是上面那個,是我設置有錯誤?

 

補充一點:

如果你的Silverlight 程序無法調用net.tcp服務,可能是你少了跨域文件:clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
        <socket-resource port="4502-4530" protocol="tcp" />
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

將clientaccesspolicy.xml放到IIS的根目錄:C:\inetpub\wwwroot中,因為SL默認只訪問80端口,所以要增加這個文件。

參考:http://www.cnblogs.com/chenkai/archive/2011/03/14/1984104.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM