将 WCF 服务器和客户端分别部署到不同机器上,可能会触发如下异常。
未处理 System.ServiceModel.Security.SecurityNegotiationException Message="服务器已拒绝客户端凭据。" Source="mscorlib"
解决方法,就是调整服务器端 Binding 的安全方式,比如设为 "None"。
NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None;
ServiceHost host = new ServiceHost(typeof(MyService)); host.AddServiceEndpoint(typeof(IService), binding, "net.tcp://192.168.10.112:8081");
ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetUrl = new Uri("http://192.168.10.112:8080"); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata);
host.Open();
ServiceHost host = new ServiceHost(typeof(MyService)); host.AddServiceEndpoint(typeof(IService), binding, "net.tcp://192.168.10.112:8081");
ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetUrl = new Uri("http://192.168.10.112:8080"); metadata.HttpGetEnabled = true; host.Description.Behaviors.Add(metadata);
host.Open();
或者在配置文件中设置。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NewBinding0"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="NewBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://192.168.10.112:8080" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="NewBehavior" name="Learn.Library.WCF.MyService"> <endpoint address="net.tcp://192.168.0.112:8081" binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="Learn.Library.WCF.IService"> </endpoint> </service> </services> </system.serviceModel> </configuration>