如何托管公共WCF服务?

3

我正在尝试创建一个不需要IIS托管且对公众无需身份验证即可访问的TCP WCF服务。

以下是我的步骤:

ServiceHost svh = new ServiceHost(typeof(MyService));

var tcpbinding = new NetTcpBinding(SecurityMode.None);

var location = "net.tcp://localhost:11111";
svh.AddServiceEndpoint(typeof(IMyService), tcpbinding, location);

svh.Open();
....

该服务在本地主机上运行良好,但当我在服务器上设置它(添加了防火墙例外)时,客户端会崩溃并显示以下错误信息:

The socket connection was aborted. This could be caused by an error processing 
your message or a receive timeout being exceeded by the remote host, or an 
underlying network resource issue. Local socket timeout was '00:00:59.7344663'.

以下是客户端配置文件:

这是客户端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IMyService">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://myserver:11111/"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyService"
                contract="MyServer.IMyService" name="NetTcpBinding_IMyService" />
        </client>
    </system.serviceModel>
</configuration>

我该如何修改才能让这个运作呢?

你有没有阅读过 http://msdn.microsoft.com/en-us/library/bb332338.aspx? - Kirill Bestemyanov
1个回答

1
我曾经遇到过类似的问题,解决方法是在终结点地址(ServiceHost .ctor)中不使用localhost,而是使用实际的机器名称。此外,您还可以在服务主机本身中定义地址,如下所示。
ServiceHost svh = new ServiceHost(typeof(MyService), new Uri("net.tcp://myserver:11111"));
var tcpbinding = new NetTcpBinding(SecurityMode.None);
svh.AddServiceEndpoint(typeof(IMyService), tcpbinding, "");
svh.Open();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接