我一直在尝试使用 WCF 并将 DataContract 添加到我的服务器并更新 ServiceContract。当 ServiceReference 首次添加到客户端时,我在同一台电脑上运行它们,所以我使用 localhost。我现在尝试通过以太网电缆连接 2 台电脑并以这种方式更新服务引用。我已将局域网上的服务器 IP 地址设置为 192.168.10.10 。单击服务器表单上的按钮时将执行以下代码。
private void btnCommenceService_Click(object sender, EventArgs e)
{
host = new ServiceHost((typeof(Service)), new Uri[] { new Uri("http://localhost:9000")});
host.AddServiceEndpoint(typeof(IServices),
new WSDualHttpBinding(),
"ServerService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
txtStatus.Text = "Service is open.";
}
我已关闭两台电脑上的防火墙,并已成功从客户端电脑 ping 地址“http://192.168.10.10”
。如果我在浏览器中输入该网页,我也可以访问一些随机网页。
在客户端中,如果我单击我的解决方案并选择“添加服务引用”并输入
“http://192.168.10.10”
“http://192.168.10.10:9000”
“http://192.168.10.10:9000/ServerService”
所有这些都会返回某种形式的错误,通常如下所示:
The document at the url http://192.168.10.10:9000/ was not recognised as a known document type.
The error message for each known type may help u fix the problem:
-Report from 'XML Schema' is The document format is not recognised(the content type is 'text/html;charset=utf-8').'.
Report from DISCO Document is There was an error downloading `'http://localhost:9000/?disco'.'.`
Unable to connect to the remote server.
No connection could be made because the target machine actively refused it 127.0.0.1:9000
etc
有人知道为什么它不能通过 LAN 工作但我可以访问并 ping 该地址吗?
最佳答案
尝试像这样更改您的绑定(bind)安全配置
WSDualHttpBinding wsDualBinding= new WSDualHttpBinding();
wsDualBinding.Security.Mode = WSDualHttpSecurityMode.None;
wsDualBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
然后添加服务端点
host.AddServiceEndpoint(typeof(IServices),wsDualBinding,"ServerService");
希望有用
关于c# - 向WCF添加服务引用(失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523671/