主机配置文件

  <!-- 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 2010-09-19 01:49  王庭安  阅读(5239)  评论(1编辑  收藏  举报