首页 文章

无法访问Owin Self托管的RestApi - 部署到AWS EC2 - Windows 2012 R2

提问于
浏览
4

以下是我自己托管RestApi代码的示例,我使用OWIN创建 . 它在我的本地机器上工作 .

我在aws-ec2中托管了它,并以管理员身份启动 .

我能够从实例内部(使用chrome / IE)访问RestApi,将localhost作为url .

Access inside Instan

但是当我用实例的公共DNS(在Instance的浏览器中运行)替换localhost时,获取Bad请求无效的主机名 .

Instance DNS name

当我试图从我的本地机器(浏览器/邮递员)从实例浏览器外部访问RestApi时,它返回状态0 .

Accessing from outside

复制下面的代码 .

class Program {
    static void Main(string[] args) {
        const string BaseUri = "http://localhost:65435";
        Console.WriteLine("Starting web Server...");
        WebApp.Start<Startup>(BaseUri);
        ...
    }
}

public class Startup {
    public void Configuration(IAppBuilder app) {
        var webApiConfiguration = ConfigureWebApi();
        app.UseWebApi(webApiConfiguration);
    }

    private HttpConfiguration ConfigureWebApi() {
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            "DefaultApi",
            "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
        return config;
    }
}

public class CompaniesController : ApiController {
    public IEnumerable<Company> Get() {
        return new List<Company>(){
                       new Company() { Id = 1, Name = "Microsoft" },
                       new Company() { Id = 2, Name = "Amazon" }
                   };
    }
}

我创建了一个Windows 2012 R2 - AWS EC2实例 .

配置实例:

  • 允许入站和出站的所有流量 .

  • 打开所有端口 .

  • 我已禁用Windows防火墙

  • 我试图从本地计算机cmd ping DNS名称,这是成功的 .

  • WebApi正在使用,端口号 - 65435(临时端口)

  • Owin版本 - 5.2.3.0,.Net版本 - 4.5(EC2实例上安装了4.5.2)

  • 将代码中的"localhost"更改为public-dns(相同问题仍然存在)和ip-address(抛出异常 - "Exception has been thrown by the target of an invocation.")

Inbound Rule

Outbound Rule

请帮我配置一下 . !!

2 回答

  • 0

    你在localhost上启动了 WebApp

    http://localhost:65435

    它是在环回适配器上启动的,而不是在您尝试从外部访问的实际以太网适配器上启动的 . 你需要用服务器的ip启动它 .

  • 0

    如果没有人能解决这个问题,那么OWIN对我来说毫无用处 . “切换回IIS”

相关问题