网上的太多太多是不完整的了
我研究了几个小时才弄明白
搞来搞去费神太多
下面我把完整的代码贴上
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="服务类">
<endpoint address="" binding="netTcpBinding" contract="服务接口" bindingConfiguration="myTcpBinding">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:端口/mex" />
</baseAddresses>
</host>
</service>
</services>
<bindings >
<netTcpBinding >
<binding name="myTcpBinding" >
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
</security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
以上是Host的app.config
请自己更改3个参数
NetTcpBinding binding = new NetTcpBinding();
binding.CloseTimeout = TimeSpan.FromMinutes(5);
binding.OpenTimeout = TimeSpan.FromSeconds(5);
binding.SendTimeout = TimeSpan.FromMinutes(10);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.TransactionFlow = false;
binding.TransferMode = TransferMode.Buffered;
binding.TransactionProtocol = TransactionProtocol.OleTransactions;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
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.Message.ClientCredentialType = MessageCredentialType.Windows;
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
EndpointAddress address = new EndpointAddress(new Uri("net.tcp://" + url + ":8770/mex"), EndpointIdentity.CreateDnsIdentity("localhost"));
ChannelFactory<服务接口> factory = new ChannelFactory<服务接口>(binding, address);
服务接口 client = factory.CreateChannel();
以上是客户端实例化代码。
自动以当前Windows用户登录。
服务器通过OperationContext.Current.ServiceSecurityContext.WindowsIdentity获得登录身份
Win7上如果想要WindowsBuiltInRole.Administrator身份
客户端需要使用管理员权限打开程序