【发布时间】: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类无效,就像异常所说的那样。 -
谢谢,这给了我需要的提示:-)。