问题
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:
- OWIN server is running (can connect from the same machine using Fiddler, browser)
- Port of EC2 instance is opened to inbound traffic via security group
- No other processes are listening to that port
- OWIN version is Microsoft.AspNet.WebApi.OwinSelfHost 5.2.2
- .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