自承载 WCF 数据服务 - 指定要侦听的 IP 地址? [英] Self Hosted WCF Data Service - Specify IP Address to Listen on?

查看:18
本文介绍了自承载 WCF 数据服务 - 指定要侦听的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 WCF 数据服务,它自托管在 Windows 控制台应用程序中.

I've written a WCF Data Service that is self hosted in a Windows console application.

使用以下代码初始化服务:

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

当我运行它时,控制台应用程序运行并且似乎在 0.0.0.0:9999 而不是 12.34.56.78:9999 上侦听.

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.

这是否意味着该服务正在侦听所有 IP 地址?

Does this mean that the service is listening on all IP addresses?

有没有办法让服务只监听指定的 IP (12.34.56.67:9999)?

Is there a way I can get the service to only listen on the IP specified (12.34.56.67:9999)?

谢谢

推荐答案

要指定监听 IP,您必须使用 HostNameComparisonMode.Exact.例如,下面的代码在 NETSTAT 中打印以下内容:

To specify the listen IP, you must use the HostNameComparisonMode.Exact. For example, the code below prints the following in 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>

这篇关于自承载 WCF 数据服务 - 指定要侦听的 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆