WCF4.0安裝 NET.TCP啟用及常見問題


WCF4.0安裝及NET.TCP啟用

 

WCF 4.0 一般默認安裝.net Framework 4.0的時候已經安裝。

但如果先裝.net framework 4.0,后裝IIS,就會出現問題。需要重新注冊WCF4.0。

WCF4.0 已經是.net 4.0的一個內部組件,不需要.net3.5 那樣麻煩先裝windows組件。 

 

一、確認是否安裝WCF4.0:

 

如下圖,查看*.svc 后綴的文件是否被svc-Integrated-4.0 或 svc-ISAPI-4.0_64/32bit 程序處理:

 

 如果沒有上面的三個處理程序,則可以判定 wcf 4.0 沒有安裝。

 

二、安裝WCF 4.0

找到對應的.net framework 目錄,運行命令:

"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\aspnet_regiis" –i –enable

"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\ServiceModelReg.exe" -r

 

Running the command aspnet_regiis –i –enable will make the Default App Pool run using .NET Framework 4, which may produce incompatibility issues for other applications on the same computer.

 

如果你有另外的app Pool,可以不用管這個 –enable,可以手動去設置那個pool的.net 版本是4.0.

 

 三、啟動服務:NetTCP Listener Adapter 和 Net TCp Port Sharing Service:

 

 

 

 

四、為IIS站點配置NETTCP協議支持:

 

 

1)綁定808:*端口:

 

 

 

2)啟用net.tcp協議:

如果不配置會出現下面錯誤:

 

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].

 

 

五、WCF3.0 和WCF 4.0 沖突:

 

如果有.net 3.5 和.net 4.0同時存在,有可能程序會找錯對應的處理程序。

 

Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.

 

重新注冊就可以:

"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\aspnet_regiis" –i –enable

"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\ServiceModelReg.exe" -r

 

 

我有參考這篇文章:http://www.cnblogs.com/Gyoung/archive/2012/12/11/2812555.html

在 WCF 4.0 啟用NET.TCP  不需要 這2個操作:

1) 安裝WAS

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

 

 

 六、其他可能的錯誤:

1) IIS站點多主機頭綁定的錯誤:

Server Error in '/WcfServiceOfMyTest' Application.

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection. If your service is being hosted in IIS you can fix the problem by setting 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' to true or specifying 'system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters'.
Parameter name: item

 

方法是:在config的</system.serviceModel> 里面允許多主機頭綁定:

</system.serviceModel>
.......
<
serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

 

2) 為什么測試界面的協議不一樣 

svcutil.exe http://localhost/WcfServiceOfMyTest/Service1.svc?wsdl

svcutil.exe net.tcp://dst52382.cn1.global.ctrip.com/WcfServiceOfMyTest/Service1.svc/mex

區別在協議 http 和 net.tcp 的是否啟用,配置在這里會影響:

 <serviceMetadata httpGetEnabled="false"/>

 

 

 

七、測試程序

新建一個默認的wcf service

配置如下:

 

<?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" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
                    maxConnections="100" maxReceivedMessageSize="2147483647"
                  transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                  hostNameComparisonMode="StrongWildcard" listenBacklog="100">

            <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="WcfServiceOfMyTest.Service_Test_NetTcp"> <!--這個服務名字不是隨便取得,要跟你的class文件里的類的名字一致!-->
          <endpoint address="" binding="netTcpBinding" contract="WcfServiceOfMyTest.IJustAnInterface" bindingConfiguration="netTcpBindConfig"></endpoint>
          <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint>
        </service>

      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyBehavior" >
            <serviceMetadata httpGetEnabled="false"/>
            <serviceDebug includeExceptionDetailInFaults="true" />
            <dataContractSerializer maxItemsInObjectGraph="6553600"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

這個配置會有錯誤:

There is no compatible TransportManager found for URI 'net.tcp://dst52382.cn1.global.ctrip.com/WcfServiceOfMyTest/Service1.svc/mex'. This may be because that you have used an absolute address which points outside of the virtual application, or the binding settings of the endpoint do not match those that have been set by other services or endpoints. Note that all bindings for the same protocol should have same settings in the same application.

對應的中文描述:

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

這個錯誤的重點在后半句話,endpoint的binding配置需要一致,這里2個endpoint,一個是服務終結點,它的 maxConnections="100" 不是默認值10,另外一個是元數據終結點,這里的max connection默認是10.

<endpoint address="" binding="netTcpBinding"
<endpoint address="mex" binding="mexTcpBinding"

他們的binding類型不一致。將他們改為一致。如下:
 <service behaviorConfiguration="MyBehavior" name="WcfServiceOfMyTest.Service_Test_NetTcp">
      <endpoint address="" binding="netTcpBinding" contract="WcfServiceOfMyTest.IJustAnInterface" bindingConfiguration="netTcpBindConfig"></endpoint>
      <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"  bindingConfiguration="netTcpBindConfig" ></endpoint>
 </service>

 

八、完整源代碼:

接口源代碼:
namespace WcfServiceOfMyTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IJustAnInterface
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

 

服務源代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServiceOfMyTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service_Test_NetTcp : IJustAnInterface
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

 

web.config 配置:

<?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" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
                    maxConnections="100" maxReceivedMessageSize="2147483647"
                  transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                  hostNameComparisonMode="StrongWildcard" listenBacklog="100">

            <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="WcfServiceOfMyTest.Service_Test_NetTcp">
          <endpoint address="" binding="netTcpBinding" contract="WcfServiceOfMyTest.IJustAnInterface" bindingConfiguration="netTcpBindConfig"></endpoint>
          <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"  bindingConfiguration="netTcpBindConfig" ></endpoint>
        </service>

      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyBehavior" >
            <serviceMetadata httpGetEnabled="false"/>
            <serviceDebug includeExceptionDetailInFaults="true" />
            <dataContractSerializer maxItemsInObjectGraph="6553600"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

 

九、HTTP 與 NET.TCP 通信效率的差別

 

 

 

參考資料:

http://www.cnblogs.com/artech/archive/2009/06/21/1507945.html

http://www.cnblogs.com/Gyoung/archive/2012/12/11/2812555.html

http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/08/iis-7-support-for-non-http-protocols.aspx

 

http://blogs.msdn.com/b/vivekkum/archive/2009/05/07/http-error-404-3-not-found-when-browsing-wcf-application-after-reinstalling-iis.aspx

http://galratner.com/blogs/net/archive/2010/10/08/setting-up-a-nettcpbinding-enabled-wcf-service-in-iis-7.aspx

 

http://www.cnblogs.com/webabcd/archive/2010/08/02/1790175.html

 

http://www.codeproject.com/Articles/16765/WCF-Windows-Communication-Foundation-Example

http://www.codeproject.com/Tips/90804/Using-WCF-without-a-generated-proxy-by-using-Chann

 

http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx

http://www.codeproject.com/Articles/89312/Windows-Communication-Foundation-QuickStart-Multip


免責聲明!

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



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