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

拟墨画扇 提交于 2019-12-24 15:06:21

问题


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

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.

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

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

Thanks


回答1:


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

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>


来源:https://stackoverflow.com/questions/27860095/self-hosted-wcf-data-service-specify-ip-address-to-listen-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!