2014-09-23 72 views
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" }; 
    } 
    } 
} 

以下爲真:

  1. OWIN服務器正在運行
  2. 端口(可以從相同的機器使用招,瀏覽器連接)的EC2實例對通過安全組的入站流量打開
  3. 沒有其他進程正在偵聽該端口
  4. OWIN版本是5.2.2 Microsoft.AspNet.WebApi.OwinSelfHost
  5. .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' 
+0

除了打開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

回答

0

使用基址http://+:1961/,並根據所Windows帳戶您運行的owin服務器因爲你不需要urlacl。 即:

  • 您正在使用的帳戶local system,那麼你將不需要urlacl運行您owin服務器作爲Windows服務。
  • 您在登錄管理員帳戶時從命令提示符運行owin服務器。
相關問題