问题
Following is the sample self hosting RestApi code, i have created with OWIN. It is working in my local machine.
i have hosted it in aws-ec2, and started as administrator.
I am able to access the RestApi from inside the instance (using chrome/IE) giving localhost as url.
But getting a Bad request invalid hostname, when i replace the localhost with public DNS of Instance (running within Instance's browser).
when i tried to access the RestApi from outside the instance browser from my local machine (browser/postman), it is returning status 0.
Copied the code below.
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" }
};
}
}
I have created a Windows 2012 R2 - AWS EC2 instance.
Configured the instance:
- To allow all traffic in inbound and outbound.
- Opened all ports.
- I have disabled Windows Firewall
- I have tried to ping the DNS name from my local machine cmd, and it was success.
- WebApi is using, port number - 65435 (Ephemeral Port)
- Owin version - 5.2.3.0, .Net version - 4.5 (4.5.2 is installed on EC2 instance)
- Changed the "localhost" in the code to public-dns (Same issue persists) and ip-address (throws an Exception - "Exception has been thrown by the target of an invocation.")
Please help me to configure this.!!
回答1:
You're starting the WebApp
on localhost:
http://localhost:65435
It's started on the loopback adapter and not on the actual ethernet adapter that you're trying to access from outside. You need to start it with the server's ip.
回答2:
If nobody can solve this problem, then OWIN is useless to me. "Switching back to IIS"
来源:https://stackoverflow.com/questions/31302499/unable-to-access-owin-self-hosted-restapi-deployed-to-aws-ec2-windows-2012-r