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

标签 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);
    }
}

最佳答案

该代码看起来不错,没有理由不能正常工作,尤其是当您可以在本地访问 OWIN 服务器时。

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

关于c# - 自托管 OWIN 到网络上的其他计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520650/

相关文章:

asp.net-web-api - Web API 帮助页面上的资源描述显示 "None."

c# - 如何防止阿拉伯字符被AntiXssEncoder重新编码?

c# - 如何在 MVC 5 中设置自定义 ClaimsPrincipal?

asp.net - OWIN AuthorizeEndpoint 的 redirect_uri 与 web api 的 uri 不同

c# - 在子对象属性上使用 Sum 的 LINQ to Entities 查询问题

c# - 从 10 位数字中获取所有可能的连续 4 位数字

c# - 可以设置多个异常记录器的用例是什么

c# - 使用 IAppBuilder.UseWebApi 时出现问题

javascript - 使用按钮 OnClick 事件调用 Javascript 函数

c# - NHibernate Criteria API 是否支持对集合属性的预测?