Self hosting Web API with OWIN on EC2

心不动则不痛 提交于 2019-12-13 04:42:05

问题


I followed step-by-step instructions from http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.

Here is the code of console application:

namespace OWINTest
{
class Program
  {
    static void Main(string[] args)
    {
        string baseAddress = "http://localhost:1961/";
        // Start OWIN host 
        WebApp.Start<Startup>(url: baseAddress);
        ...
    }
  }
class Startup
  {
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    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);
    }
  }
public class ValuesController : ApiController
  {
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
  }
}

The following is true:

  1. OWIN server is running (can connect from the same machine using Fiddler, browser)
  2. Port of EC2 instance is opened to inbound traffic via security group
  3. No other processes are listening to that port
  4. OWIN version is Microsoft.AspNet.WebApi.OwinSelfHost 5.2.2
  5. .NET 4.5 is installed on EC2 instance

Issue I run into:

HTTP/1.1 502 - Connection Failed error when calling http://(uc2-00-000-000-us-west-1.compute.amazonaws.com):1961/api/values with the following message: The connection to 'ec2-00-000-000-us-west-1.compute.amazonaws.com' failed. Error: TimedOut (0x274c). System.Net.Sockets.SocketException A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 

I tried the following without success:

 1. 'netsh http add urlacl url=http://localhost:1961 user=everyone'
 2. 'netsh http add urlacl url=http://+:1961 user=everyone'
 3. 'netsh http add urlacl url=http://*:1961 user=everyone'

回答1:


Use base address http://+:1961/ and depending on which windows account you are running your owin server as you won't need the urlacl. ie:

  • you're running your owin server as a windows service using the local system account then you won't need the urlacl.
  • you're running the owin server from the command prompt while logged in the administrator account.


来源:https://stackoverflow.com/questions/26006284/self-hosting-web-api-with-owin-on-ec2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!