自托管OWIN到网络上的其他计算机

时间:2015-07-20 15:36:23

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

我正在尝试在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);
    }
}

1 个答案:

答案 0 :(得分:8)

该代码看起来很好,并且它没有理由不起作用,特别是如果您可以在本地访问OWIN服务器。

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