【问题标题】:Self Hosted WCF Data Service - Specify IP Address to Listen on?自托管 WCF 数据服务 - 指定要侦听的 IP 地址?
【发布时间】:2015-01-09 11:59:12
【问题描述】:

我编写了一个 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 ), BaseAddresses ) )
    {
        host.Open(); 
        Console.ReadLine();
    }
}

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

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

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

谢谢

【问题讨论】:

    标签: c# wcf wcf-data-services


    【解决方案1】:

    要指定监听 IP,您必须使用HostNameComparisonMode.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>
    

    【讨论】:

    • 谢谢 Mitch - hostNameComparisonMode 正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 2011-12-05
    • 1970-01-01
    • 2011-07-19
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多