c# - 所有 IP 地址上的 Mono 和 WCF Net.Tcp 绑定(bind)

标签 c# wcf mono net.tcp

this 中所述文章,在 .net 中,可以使用“地址”0 将 Web 服务绑定(bind)到所有 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");
}

服务代理:

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;
    }
}

我的服务:

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

我的服务:

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

服务主机:

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();
        }
    }
}

如果我在使用 .net 的 Windows PC 上运行这个(服务和客户端)一切正常。
在我的 Linux 机器(Raspberry Pi、Debian soft-float)上,服务启动没有任何问题,但是客户端无法连接。
如果我使用其 IP 地址而不是“0”地址托管服务,一切正常。

这只是单声道中的另一个错误,还是我必须绑定(bind)到任何其他 IP 地址而不是 0? 如果它是单声道中的错误,是否有任何解决方法?

(顺便说一句,我仍在寻找解决 mono/net.tcp 端口共享问题的解决方法,如果有人可以在这里提供帮助 -> net.tcp port sharing and mono)

最佳答案

尝试使用 +* 而不是 0 来绑定(bind)到所有地址。

编辑:或尝试 0.0.0.0

Edit2:看起来像是 Mono 中的错误。您可以通过https://bugzilla.xamarin.com/index.cgi举报

关于c# - 所有 IP 地址上的 Mono 和 WCF Net.Tcp 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16056746/

相关文章:

c# - 状态栏通知 xamarin native android

c# - 如何找到由 ""分隔的字符串中的最后一个单词? C#

c# - EF Core 查询存储过程映射到类型

c# - 保持 WCF 服务连接

android - mono droid 和 mvvm cross 的奇怪异常无法解析 IoC 类型

c# - Mono Migration Analyzer 的替代品

c# - MVVM 选项卡 : Focus new tab

c# - WCF 的表单例份验证

c# - 使用 .Net Standard 2.0 库中的 x509 证书使用 WCF Web 服务?

c# - 使 GTK# 文件选择器仅选择文件