採用nettcp繫結的wcf宿主到iis7

weixin_34119545發表於2012-03-05

也許事情太雜了記憶力明顯下降,做過的東西很容易忘記。上次成功將採用nettcp繫結方式的wcf部署到iis7,重新來做已經忘得差不多了。

重新在網上搜尋相關資料,進行記錄以備再用。
一、準備:首先iis必須是7.0或更高,同時需要安裝需要在“開啟或關閉Windows功能”中安裝Microsoft .NET Framework 3.5.1中的Windows Communication Foundation HTTP Activation、Windows Communication Foundation Non-HTTP Activation和Web管理工具-IIS6管理相容性-IIS後設資料庫和IIS6配置相容性這三個功能;(我的系統是win7旗艦版)

在iis中,我乾脆把所有的服務都安裝了。

二、配置網站支援nettcp協議
防火牆需要為nettcp服務開洞,要允許埠:4502-4534通過

4502:*
 下面就可以建立你的網站虛擬目錄並確定啟用nettcp協議

(3)wcf在訪問的時候存在跨域問題,在wwwroot資料夾即網站跟目錄下放置跨域訪問檔案和策略檔案clientaccesspolicy.xml、crossdomain.xml。檔案內容如下:
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>
crossdomain.xml內容:

<?xml version="1.0"?>
<cross-domain-policy>
 <allow-access-from domain="*"/>
</cross-domain-policy>

三、其他注意問題:

    4.在向Silverlight專案中新增服務引用的時候有時會出現Could not load file or assembly 'System.ServiceModel.Activation.HttpModule’ from assembly…的錯誤,則需要在命令列中執行C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -iru重新安裝ASP.NET 4.0

5.注意WAS的啟動

Windows (Process) Activation Service (WAS)作為 IIS7.0 特有的新增功能,和以前IIS 6.0的功能相比更加強大,因為它提供並支援除HTTP之外的更多協議,比如TCP方式和Pipe(管道)方式。利用WAS作為WCF(Windows Communication Foundation)的宿主,我們能充分利用WAS的很多優點,因為我們再也不用為非HTTP方式的WCF Service單獨編寫宿主程式了。而WAS本身的特點,也讓我們的服務端程式能享受到只有以往的HTTP方式的WCF Service才能擁有的很多特性。
WAS在Windows Vista/7/2008/R2中,其實是由一個單獨的Windows Service來實現的 在Windows服務裡頭可以找到一個Windows Service,就叫做"Windows Process Activation Service"。由此看來,WAS的程式和IIS程式在物理上是隔離開的,能為我們提供一個靈活、穩定的WCF Service宿主環境。WAS內部的工作機制,大致和ASMX WebService類似。
IIS 7上 引入了以下三組網路監聽器(Listener)和監聽介面卡(Adapter),實現了基於TCP、Named Pipes和MSMQ的網路監聽。分別是:
TCPListener|TCP Listener Adapter
NamedPipes Listener|Named Pipes Listener Adapter
MSMQ Listener|MSMQ Listener Adapter 
在IIS 7的結構如下
 

由於IIS 7提供了基於非HTTP網路協議的監聽支援,那麼就意味著當我們當我們通過IIS進行WCF服務寄宿(Hosting)的時候,可以採用非HTTP的通訊方式。其中的TCPListener|TCP Listener Adapter 依賴於服務NetTcpPortSharing,為net.tcp繫結提供tcp的埠共享,具體可參看啟用WCF NetTcpBinding的共享埠。

文章來自學IT網:http://www.xueit.com/html/2010-01-25/21-982353359406.html

一些參考網址:http://archive.cnblogs.com/a/1883612/
http://files.cnblogs.com/dhuxin/WCF.NetTCP%e7%bb%91%e5%ae%9a%e9%85%8d%e7%bd%ae%e8%af%b4%e6%98%8e.pdf
http://www.cnblogs.com/chocstarfish/archive/2010/06/26/1765568.html
http://www.singingeels.com/Articles/Duplex_WCF_Services_Hosted_in_IIS_Using_NetTcp.aspx
http://www.cnblogs.com/cubean/archive/2009/08/28/1555223.html
http://archive.cnblogs.com/a/1959748/
四、1、我的wcfweb配置檔案

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

  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="tcpBindingNoSecurity"  receiveTimeout="24.00:00:00">
          <reliableSession inactivityTimeout="24.00:00:00" />
          <!--每次最大傳送資料流大小-->
          <readerQuotas maxArrayLength="2147483647" />
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
     <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
          <!--原來為FALSE-->
          <!--這裡新增行開始修改預設的可序列化的集合長度為預設值的100倍6553600-->
          <dataContractSerializer maxItemsInObjectGraph="6553600"/>
          <!--這裡新增行結束-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--這裡開始新增-->
    <services>
      <service name="ActService.Service">
        <endpoint address="net.tcp://11.1.21.0:4502/Service/Service.svc"
            binding="netTcpBinding" bindingConfiguration="tcpBindingNoSecurity"
            contract="ActService.IService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    
    <!--這裡結束新增-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

  <connectionStrings>
    <add name="ActivitiesEntities" connectionString="metadata=res://*/Activities.csdl|res://*/Activities.ssdl|res://*/Activities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=4CB17D79510E48F;Initial Catalog=Activities;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
   
  </connectionStrings>
  <appSettings>
    <!--
            ===================================
            自定義資料庫連線資訊
    -->
    <add key="ConnectionString" value="Data Source=*.*.*.*;Initial Catalog=A;User ID=ad;Password=*"/>
  </appSettings>
</configuration>
2、silverlight下的配置檔案:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="NetTcpBinding_IService">
                    <binaryMessageEncoding />
                    <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://10.2.21.13:4502/ActService/Service.svc"
                binding="customBinding" bindingConfiguration="NetTcpBinding_IService"
                contract="ActService.IService" name="NetTcpBinding_IService" />
        </client>
    </system.serviceModel>
</configuration>

3、web下wcf資料服務的配置檔案

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="ActivitiesEntities" connectionString="metadata=res://*/Activities.csdl|res://*/Activities.ssdl|res://*/Activities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=11.1.21.0;Initial Catalog=A;Persist Security Info=True;User ID=Ad;Password=1;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  
  </connectionStrings>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

</configuration>

相關文章