自承载 WCF 数据服务

编程入门 行业动态 更新时间:2024-10-26 04:32:17
本文介绍了自承载 WCF 数据服务 - 指定要侦听的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我编写了一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:20:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!