简体   繁体   English

自托管WCF数据服务-指定要监听的IP地址?

[英]Self Hosted WCF Data Service - Specify IP Address to Listen on?

I've written a WCF Data Service that is self hosted in a Windows console application. 我编写了WCF数据服务,该服务自托管在Windows控制台应用程序中。

The service is initialised with the following code: 该服务使用以下代码初始化:

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

When I run this, the console app runs and appears to listen on 0.0.0.0:9999 and not 12.34.56.78:9999. 当我运行此命令时,控制台应用程序将运行并似乎在监听0.0.0.0:9999,而不是在12.34.56.78:9999。

Does this mean that the service is listening on all IP addresses? 这是否意味着服务正在侦听所有IP地址?

Is there a way I can get the service to only listen on the IP specified (12.34.56.67:9999)? 有没有办法让我的服务仅侦听指定的IP(12.34.56.67:9999)?

Thanks 谢谢

To specify the listen IP, you must use the HostNameComparisonMode.Exact . 要指定监听IP,必须使用HostNameComparisonMode.Exact For example, the code below prints the following in NETSTAT : 例如,下面的代码在NETSTAT中输出以下内容:

C:\drop>netstat /a /p tcp

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    10.200.32.98:9999      Zeta2:0                LISTENING

From code: 来自代码:

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

From config: 从配置:

<basicHttpBinding>
    <binding name="yourBindingName" hostNameComparisonMode="Exact" />
</basicHttpBinding>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM