c# - 向WCF添加服务引用(失败)

标签 c# wcf c#-4.0

我一直在尝试使用 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/

相关文章:

.net - Web服务异步方法的使用

c# - WCF 服务引用命名空间与原始命名空间不同

c# - Restful 服务 URL 参数 - 服务堆栈

c# - 在 C# 中序列化 DataTable 会占用大量内存——如何改进这一点

c# - 无法评估表达式错误

c# - 有没有办法从 C# 中的多维数组中获取数组?

jquery - WCF 服务因方法不允许而失败

c# - 字母和空格的正则表达式

c# - 从列表中获取月份名称和年份

c# - 在 LINQ to Entities 中删除记录的正确方法