【问题标题】:ASP.NET unable to bind to all interfacesASP.NET 无法绑定到所有接口
【发布时间】:2019-10-11 19:05:14
【问题描述】:

我尝试创建一个 asp.net 服务;如果我尝试在本地访问它,它会起作用。但不可能从远程位置到达它。

下面是我用来实例化服务器的代码:

using System;
using System.Web.Http;
using System.Web.Http.SelfHost;
using System.Configuration;

namespace RESTService
{
  public class Service : IDisposable
  {
    private HttpSelfHostServer server;

    public Service()
    {
      var config = new HttpSelfHostConfiguration($"http://*:{ConfigurationSettings.AppSettings["port"]}/");
      config.Routes.MapHttpRoute("TTS", "{controller}/{text}",
        new[] { "RESTService.Controller" });

      server = new HttpSelfHostServer(config);
    }

    public void Run()
    {
      server.OpenAsync().Wait();
    }

    public void Dispose()
    {
      server.Dispose();
    }
  }
}

我用 * 替换了 localhost,如 Web API self host - bind on all network interfaces 中所述。不幸的是,我用这段代码得到了一个 UriFormatException 。如何将服务绑定到所有接口?

【问题讨论】:

  • 您的异常的详细信息是什么?执行后 $"http://*:{ConfigurationSettings.AppSettings["port"]}/ 的值是多少?
  • 我也想从外网地址(192.168.0.73)访问服务。端口的值为 8888
  • 挂钩到服务器的 IP 地址 (192.168.0.73?),或者只是 0.0.0.0。 * 对 Uri 类无效,就像异常所说的那样。
  • 谢谢,这给了我需要的提示:-)。

标签: c# asp.net .net


【解决方案1】:

感谢 Lex Li;他的评论为我指明了正确的方向。以下代码有效:

new HttpSelfHostConfiguration($"http://{IPAddress.Any}:{ConfigurationSettings.AppSettings["port"]}/")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多