0
我遵循http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api的分步說明。在EC2上自主託管OWIN的Web API
這裏是控制檯應用程序的代碼:
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" };
}
}
}
以下爲真:
- OWIN服務器正在運行
- 端口(可以從相同的機器使用招,瀏覽器連接)的EC2實例對通過安全組的入站流量打開
- 沒有其他進程正在偵聽該端口
- OWIN版本是5.2.2 Microsoft.AspNet.WebApi.OwinSelfHost
- .NET 4.5安裝在EC2實例
問題,我碰到:
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
我嘗試沒有成功以下:
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'
除了打開1961端口之外,還必須打開[Ephemeral Ports](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_ACLs.html#VPC_ACLs_Ephemeral_Ports)。這基本上允許服務器打開一個端口將數據返回給客戶端。 – qmo 2014-10-23 03:08:32