【问题标题】:Self Hosting OWIN to other computers on the Network自托管 OWIN 到网络上的其他计算机
【发布时间】:2015-10-09 19:42:03
【问题描述】:

我正在尝试在 WinForms 应用程序中自行托管 OWIN 管道。该管道同时托管静态文件和 Web Api v2 内容。该实现在本地运行良好,但我不确定为了能够从我的网络上的远程计算机访问托管文件和 API,我缺少什么。

为简单起见,我从 codeplex here 下载了示例自托管应用程序,并在对基地址进行了以下修改后尝试远程访问测试方法(我尝试运行 netsh 注册和我正在运行管理员模式),我仍然无法访问它们。我需要更改哪些配置才能查看同一网络上其他计算机的内容?

static void Main()
{
    string baseAddress = "http://*:10281/";
    // Start OWIN host
    using (WebApp.Start<Startup>(url: baseAddress))
    {
        // Create HttpCient and make a request to api/values
        HttpClient client = new HttpClient();

        HttpResponseMessage response = client.GetAsync("http://localhost:10281/api/values").Result;

        Console.WriteLine(response);
        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
        Console.ReadLine(); // Keeps the host from disposing immediately
    }
}

这是启动配置,非常基本的东西:

public class Startup
{
     // This code configures Web API contained in the class Startup, which is additionally specified as the type parameter in WebApplication.Start
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for Self-Host
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
}

【问题讨论】:

  • client.GetAsync("http://localhost:10281/api/values") 可能想将“localhost”更改为您机器的 IP 地址以远程查看该端点...或者使用相同的基地址格式,尽管我认为您应该使用“如果我没记错的话,+”而不是“*”。
  • 这只是测试代码,我在本地浏览器和远程机器上测试时都使用我的IP地址。

标签: c# asp.net-web-api owin self-hosting


【解决方案1】:

该代码看起来不错,没有理由不工作,特别是如果您可以在本地访问 OWIN 服务器。

有两个可能的问题阻止网络访问。您需要为端口 10281 向 Windows 防火墙添加入站规则(或暂时禁用主机上的任何防火墙),或者您的网络以某种方式阻止了流量。

【讨论】:

  • Windows 防火墙抓住了我。应该知道,在启动需要防火墙访问的新应用程序之前,我总是会收到提示。不确定 OWIN 应用程序有什么不同,但 Windows 从未问过我。我添加了入站规则,瞧!
猜你喜欢
  • 1970-01-01
  • 2014-11-18
  • 2011-01-12
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多