您的位置:首页 > 编程学习 > ASP.NET > 正文

WCF NetTcpBinding 绑定

更多 时间:2015-10-7 类别:编程学习 浏览量:62

WCF NetTcpBinding 绑定

WCF NetTcpBinding 绑定


一、WCF的绑定方式比较多,常用的大体有四种

wsHttpBinding 

basicHttpBinding 

netTcpBinding 

wsDualHttpBinding

 

这四种绑定方式中,有两种支持双工通信

wsDualHttpBinding 

netTcpBinding

 

二、WCF NetTcpBinding 绑定实例

 

1、定义接口IFly

  •  
  • 
    namespace TECO
    {
        [ServiceContract]
        public interface IFly
        {
            [OperationContract]
            string Fly(string name);
        }
    }
    
    		
  •  

    2、定义实现

  •  
  • 
    namespace TECO
    {
        public class People:IFly
        {
    
           IFly 成員
        }
    }
    
    		
  •  

    3、定义宿主 host,启动服务

  •  
  • 
    public class Program {
            static void Main(string[] args) {
       
                using (ServiceHost host = new ServiceHost(typeof(People)))
                {
                    host.Open();
                    Console.WriteLine("服务已经启动");
                    Console.Read();
                }
    
            }
        }
    
    		
  •  

    4、配置app.config 文件

  •  
  • XML 代码   复制
  • 
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <system.serviceModel>
        
        <behaviors>
          <serviceBehaviors>
            <behavior name="MessageBehavior">
              <serviceMetadata httpGetEnabled="false"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="MessageBehavior" name="TECO.People">
            <endpoint address="" binding="netTcpBinding" bindingConfiguration="Binding1"
              contract="TECO.IFly" />
            <endpoint address="net.tcp://localhost:4503/People/mex" binding="mexTcpBinding"
              contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://localhost:1234/People" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
          <netTcpBinding>
            <binding name="Binding1"
                     closeTimeout="00:01:00"
                     openTimeout="00:01:00"
                     receiveTimeout="00:10:00"
                     sendTimeout="00:01:00"
                     transactionFlow="false"
                     transferMode="Buffered"
                     transactionProtocol="OleTransactions"
                     hostNameComparisonMode="StrongWildcard"
                     listenBacklog="10"
                     maxBufferPoolSize="524288"
                     maxBufferSize="65536"
                     maxConnections="10"
                     maxReceivedMessageSize="65536">
              <readerQuotas maxDepth="32"
                            maxStringContentLength="8192"
                            maxArrayLength="16384"
                            maxBytesPerRead="4096"
                            maxNameTableCharCount="16384" />
              <reliableSession ordered="true"
                               inactivityTimeout="00:10:00"
                               enabled="false" />
              <security mode="None"></security>
            </binding>
          </netTcpBinding>
        </bindings>
      </system.serviceModel>
    </configuration>
    
    		
  •  

    5、添加引用 (使用代理类調用服务)

    点击添加引用:输入路径为net.tcp://localhost:4503/People/mex,此時需要先启动服务。如果服务启动了,那么可以看到服务的內容。

    点确定会生成客戶端代理。例如 ServiceReference1

     

    6、在客戶端使用代理类操作

  •  
  • C# 代码   复制
  • 
    ServiceReference1.FlyClient clent = new ServiceReference1.FlyClient();
    Console.WriteLine(clent.Fly("zzy"));
    
    		
  •  

    三、在WCF NetTcpBinding的配置过程中,找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注册的基址方案是 [http]   错误的解决方法

     

     

    1、尽量要保证你的配置文件是正确的

    例如

  •  
  • XML 代码   复制
  • 
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="ShuiMoNet_Service.FileOperation.binding" closeTimeout="00:30:00"
                    openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                    transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                    maxReceivedMessageSize="2147483647">
              <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"></transport>
                <message clientCredentialType="Windows" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <services>
          <service name="ShuiMoNet_Service.FileOperation" behaviorConfiguration="ShuiMoNet_Service.FileOperation.Behavior">
            <endpoint binding="netTcpBinding"  bindingConfiguration="ShuiMoNet_Service.FileOperation.binding" contract="ShuiMoNet_Service.IFileOperation" address="FileOperation"></endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://localhost:8001/"/>
                <add baseAddress="http://localhost:8002/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ShuiMoNet_Service.FileOperation.Behavior">
              <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>
    
    		
  •  

    2、检查你是否启用了 WCF 功能

     

    3、检查是否在 IIS 中启用了net.tcp 协议

     

    4、在站点绑定中如下配置net.tcp路由

     

    标签:WCF