posts - 21,comments - 64,views - 25007

 

主机配置文件

  <!-- 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();

 

posted on   王庭安  阅读(5258)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
< 2010年9月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示