Asp.Net Core Web API app: how to change listening address?

我的梦境 提交于 2019-11-27 21:53:03

问题


I write simple Asp.Net Core WebAPI 2.0 application, it works on my local machine. But i want to deploy it to server. So, i do it.

My system:

Ubuntu 16.04.
Asp.Net WebAPI 2.0 (and dotnet version 2.1.105)

But, when app starts it writes:

Now listening on:http://localhost:53115

When i try to get values from it:

  http://id_address:53115/api/values

And i can not get response. In Postman:

 Could not get any response
 There was an error connecting to 
 http://id_address:53115/api/values.
 Why this might have happened:
 The server couldn't send a response:
 Ensure that the backend is working properly
 Self-signed SSL certificates are being blocked:
 Fix this by turning off 'SSL certificate verification' in Settings > General
 Proxy configured incorrectly
 Ensure that proxy is configured correctly in Settings > Proxy
 Request timeout:
 Change request timeout in Settings > General

What i should do? Can you tell me how to fix that?

I do not know where to start looking for.

Thank you!


回答1:


I'm using .NETCore 2.1 prev, so I haven't been able to test it myself but if I believe that https://www.billbogaiv.com/posts/setting-aspnet-host-address-in-net-core-2, adding .UseUrls(urls: "http://*:5000") might be instructing Kestrel to listen to port 5000 and not only on localhost, so it should work also on a remote server.

Other possible solution, UseKestrel(..) https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.1&tabs=aspnetcore2x using IPAddress.Any instead of Loopback.




回答2:


This is configured by Server urls host configuration

Indicates the IP addresses or host addresses with ports and protocols that the server should listen on for requests.
Key: urls
Type: string
Default: http://localhost:5000
Set using: UseUrls
Environment variable: ASPNETCORE_URLS

Set to a semicolon-separated (;) list of URL prefixes to which the server should respond.


It is also possible to set URL using command line arguments. For example:

dotnet run --urls=http://0.0.0.0:5001

but this doesn't work out of the box for old versions of ASP.NET Core (depends on whether this fix applied to used version or not).

A workaround for old versions based on fact that you always can set host settings directly via .UseConfiguration method:

var config = new ConfigurationBuilder().AddCommandLine(args).Build();

return WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)

Note, the same idea may be used to read setting value from any other configuration source, like configuration file for example.



来源:https://stackoverflow.com/questions/50097268/asp-net-core-web-api-app-how-to-change-listening-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!