C# 自托管WCF数据服务-指定要侦听的IP地址?
我已经编写了一个WCF数据服务,它在Windows控制台应用程序中自托管 使用以下代码初始化服务:C# 自托管WCF数据服务-指定要侦听的IP地址?,c#,wcf,wcf-data-services,C#,Wcf,Wcf Data Services,我已经编写了一个WCF数据服务,它在Windows控制台应用程序中自托管 使用以下代码初始化服务: static void Main(string[] args) { DataServiceHost host; Uri[] BaseAddresses = new Uri[] { new Uri("http://12.34.56.78:9999")}; using (host = new DataServiceHost( typeof( MyServerService
static void Main(string[] args)
{
DataServiceHost host;
Uri[] BaseAddresses = new Uri[] { new Uri("http://12.34.56.78:9999")};
using (host = new DataServiceHost( typeof( MyServerService ), BaseAddresses ) )
{
host.Open();
Console.ReadLine();
}
}
当我运行此命令时,console应用程序运行并显示为在0.0.0.0:9999而不是12.34.56.78:9999上侦听
这是否意味着服务正在侦听所有IP地址
有没有办法让服务只在指定的IP上侦听(12.34.56.67:9999)
感谢要指定侦听IP,您必须使用
主机名ComparisonMode.Exact
。例如,下面的代码在NETSTAT
中打印以下内容:
C:\drop>netstat /a /p tcp
Active Connections
Proto Local Address Foreign Address State
TCP 10.200.32.98:9999 Zeta2:0 LISTENING
从代码:
class Program
{
static void Main(string[] args)
{
Uri[] BaseAddresses = new Uri[] { new Uri("http://10.200.32.98:9999") };
using (var host = new ServiceHost(typeof(Service), BaseAddresses))
{
host.AddServiceEndpoint(typeof(Service), new BasicHttpBinding() { HostNameComparisonMode = HostNameComparisonMode.Exact }, "");
host.Open();
Console.ReadLine();
}
}
}
[ServiceContract]
class Service
{
[OperationContract]
public void doit()
{
}
}
从配置:
<basicHttpBinding>
<binding name="yourBindingName" hostNameComparisonMode="Exact" />
</basicHttpBinding>
谢谢米奇-主机名ComparisonMode正是我想要的。