如this文章所述,在.net中可以使用地址0将Web服务绑定到所有IP地址。但是,在Mono(版本2.10.8.1)中似乎无法实现这一点。
以下是我的示例代码:
客户端:
如果我在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)
以下是我的示例代码:
客户端:
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)
Unhandled Exception: System.Net.Sockets.SocketException: No such host is known
。而使用 +,我得到的是:Unhandled Exception: System.UriFormatException: Invalid URI: The hostname could not be parsed.
。 - bema未处理的异常:System.ArgumentException:地址0.0.0.0(IPv4)和::0(IPv6)是未指定的地址。您不能将它们用作目标地址。
顺便说一下,感谢您非常快速的回答 :) - bema