在所有IP地址上使用Mono和WCF Net.Tcp绑定

4
this文章所述,在.net中可以使用地址0将Web服务绑定到所有IP地址。但是,在Mono(版本2.10.8.1)中似乎无法实现这一点。
以下是我的示例代码:
客户端:
string ipAddressOfTheService = "192.168.0.23";
EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:8081/myService", ipAddressOfTheService));
NetTcpBinding binding = new NetTcpBinding();
ServiceProxy proxy = new ServiceProxy(binding, address);
if(proxy.CheckConnection())
{
    MessageBox.Show("Service is available");
}
else
{
    MessageBox.Show("Service is not available");
}

ServiceProxy:

public class ServiceProxy : ClientBase<IMyService>, IMyService
{
    public ServiceProxy(Binding binding, EndpointAddress address)
        : base(binding, address)
    {
    }

    public bool CheckConnection()
    {
        bool isConnected = false;
        try
        {
            isConnected = Channel.CheckConnection();
        }
        catch (Exception)
        {
        }
        return isConnected;
    }
}

IMyService:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    bool CheckConnection();
}

MyService:

class MyService : IMyService
{
    public bool CheckConnection()
    {
        Console.WriteLine("Check requested!");
        return true;
    }
}

ServiceHost:

class MyServiceHost
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri(string.Format("net.tcp://0:8081/myService");
        using (ServiceHost host = new ServiceHost(typeof(MonitoringService), baseAddress))
        {
            NetTcpBinding binding = new NetTcpBinding();
            binding.Security.Mode = SecurityMode.None;

            host.AddServiceEndpoint(typeof(IMyService), binding, baseAddress);
            host.Open();
            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();
            host.Close();
        }
    }
}

如果我在Windows PC上使用.net运行此服务和客户端,一切都正常。
在我的Linux机器(Raspberry Pi, Debian soft-float)上,服务可以启动而没有任何问题,但是客户端无法连接。
如果我使用其IP地址而不是“0”地址托管服务,则一切正常。
这只是mono中的另一个bug还是我需要绑定到其他IP地址而不是0?
如果这是mono中的一个错误,是否有任何解决方法?
(顺便说一下,我仍然在寻找用于mono/net.tcp端口共享问题的解决方法,如果有人能帮忙 -> net.tcp port sharing and mono)
3个回答

0

我在Ubuntu Server 16.04 LTS和Mono JIT编译器版本5.0.1.1(2017-02/5077205 Thu May 25 09:19:18 UTC 2017)中遇到了类似的问题。

我通过在/etc/hosts中添加以下行来解决它:

0.0.0.0 IP4Any

然后,当然,将服务绑定到IP4Any


0

使用 * 代替 0,我得到的是:Unhandled Exception: System.Net.Sockets.SocketException: No such host is known。而使用 +,我得到的是:Unhandled Exception: System.UriFormatException: Invalid URI: The hostname could not be parsed. - bema
使用0.0.0.0,我得到了以下错误:未处理的异常:System.ArgumentException:地址0.0.0.0(IPv4)和::0(IPv6)是未指定的地址。您不能将它们用作目标地址。 顺便说一下,感谢您非常快速的回答 :) - bema
你正在使用哪个版本的Mono? - PhonicUK
哦,抱歉我完全忽略了那个。Mono版本是2.10.8.1。 - bema
那么这似乎是Mono中的一个bug。您可以在https://bugzilla.xamarin.com/index.cgi上报告它-请确保包含一个测试用例! - PhonicUK

0

在Mono上使用NetTcpBinding使WCF工作的教程

要在Mono中使用IP地址进行WCF,您需要在文件/ etc / hosts中编写域名。然后,您将能够使用IP地址连接主机和客户端。

如何编写域名: 在主机上打开文件/ etc / hosts并添加IP.address.of.host HostName


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