WCF net.tcp方式的配置

本文介绍了一个WCF服务的配置示例及其客户端调用代码。服务使用NetTcpBinding进行配置,并启用了可靠会话及Windows身份验证。客户端通过相同的绑定设置连接到服务并调用SayHello方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

主机配置文件

  <!-- WCF配置部分 -->
  <system.serviceModel>
    <services>
      <service name="Gren.Practices.Wcf.Behavior.WcfService" behaviorConfiguration="Gren.Practices.Wcf.BehaviorConfiguration">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:9001/WcfService" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" contract="Gren.Practices.Wcf.Interface.IWcfService" bindingConfiguration="netTcpBindingConfiguration" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Gren.Practices.Wcf.BehaviorConfiguration">
          <serviceMetadata httpGetEnabled="False" />
          <serviceDebug includeExceptionDetailInFaults="False" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfiguration"
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:10:00"
                 transactionFlow="false"
                 transferMode="Buffered" 
                 transactionProtocol="OleTransactions"
                 hostNameComparisonMode="StrongWildcard"
                 listenBacklog="10" 
                 maxBufferPoolSize="2147483647 "
                 maxBufferSize="2147483647 "
                 maxConnections="10"
                 maxReceivedMessageSize="2147483647 ">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647 " maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

 

 

客户端代码

 

NetTcpBinding binding = new NetTcpBinding();
binding.SendTimeout = TimeSpan.FromMinutes(10);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;

binding.ReaderQuotas.MaxDepth = 64;
binding.ReaderQuotas.MaxStringContentLength = 2147483647;
binding.ReaderQuotas.MaxArrayLength = 2147483647;
binding.ReaderQuotas.MaxBytesPerRead = 4096;
binding.ReaderQuotas.MaxNameTableCharCount = 16384;

binding.ReliableSession.Ordered = true;
binding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
binding.ReliableSession.Enabled = false;

binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;

EndpointAddress address = new EndpointAddress("net.tcp://KEAI1365:9001/WcfService");
ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(binding, address);
factory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential("test", "test", "KEAI1365");

try
{
    IWcfService channel = factory.CreateChannel();
    Console.WriteLine(channel.SayHello());
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    factory.Close();
}

Console.ReadLine();

 

转载于:https://www.cnblogs.com/wangyz/archive/2010/09/19/1830524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值