Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自托管OWIN到网络上的其他计算机_C#_Asp.net Web Api_Owin_Self Hosting - Fatal编程技术网

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

C# 自托管OWIN到网络上的其他计算机,c#,asp.net-web-api,owin,self-hosting,C#,Asp.net Web Api,Owin,Self Hosting,我正在尝试在WinForms应用程序中自托管OWIN管道。管道同时承载静态文件和Web Api v2内容。该实现在本地运行得很好,但我不确定为了能够从网络上的远程机器访问托管文件和API,我缺少了什么 为了简单起见,我从codeplex下载了示例self-host应用程序,并尝试在对基址进行以下修改后远程访问测试方法(我尝试运行netsh registration,并且在管理模式下运行),但仍然无法访问它们。为了能够查看来自同一网络上其他计算机的内容,我需要更改哪些配置 static void

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

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

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
    }
}

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


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

client.GetAsync(“http://localhost:10281/api/values“”
可能要更改“localhost”到您的计算机的IP地址以远程查看该端点。。。或者使用相同的基址格式,尽管我想如果我没记错的话,你应该使用“+”而不是“*”。这只是测试代码,我在本地浏览器和远程机器上进行测试时都使用我的IP地址。Windows Firewall抓住了我。我应该知道,在启动需要防火墙访问的新应用程序时,我以前总是收到提示。不确定OWIN应用程序有什么不同,但Windows从未问过我。我添加了入站规则,瞧!
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);
    }
}