ASP.Net Core app works in visual studio but not wi

2020-03-06 03:48发布

I have a asp.net core app that I started building through visual studio and normally it works fine when I press F5 or press the debug button in visual studio.

However, if I try to use dotnet run from command line, it still works and starts listening on the default port 5000 (I'm assuming this is kestrel listening). enter image description here

But if I try to access the page through any browser using http://localhost:5000, I just get error saying unable to connect. I understand that if I run from visual studio, it automatically puts my app behind IIS but I dont understand why it doesn't work if the app is just self hosted.

I want to move my project to a linux environment and this is the only thing that seems to be stopping me now. I've tested with creating new dotnet apps through cli and those work fine if I try to access through browser, it just seems that this specific app that I created in VS doesn't work.

My dotnet --version returns 1.0.4

Edit (detailed answer)

So to add to Sanket's answer below, I also figured out the real reason why my app was only working if I had set my ASPNETCORE_ENVIRONMENT to Development.

It turns out that I had set a testing mail service to only be configured if my environment was Development like so:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, 
        WorldContextSeedData seeder)
    {

        if (env.IsEnvironment("Development"))
        {
            app.UseDeveloperExceptionPage(); 
            loggerFactory.AddDebug(LogLevel.Information);
        }
    }

But in one of my controllers I had initalized the mail service without checking the environment like so:

public AppController(IMailService mailService, IConfigurationRoot config, WorldRepository repository, ILogger<AppController> logger, IHostingEnvironment env)
{
    _env = env;
    _mailService = mailService;
    _config = config;
    _repository = repository;
    _logger = logger;
}

I realized this problem when I decided to configure UseDeveloperExceptionPage() for Staging environment and it became obvious as soon as I ran it where my issue was. My controller class was trying to initialize the mail service when it hadn't been configured in the first place. So I made some changes to my controller like so:

        public AppController(
#if Development
            IMailService mailService,
#endif
            IConfigurationRoot config, 
            IWorldRepository repository, ILogger<AppController> logger, IHostingEnvironment env)
        {
            _env = env;
#if Development
            _mailService = mailService;
#endif
            _config = config;
            _repository = repository;
            _logger = logger;
        }

And now my app works in any environment.

1条回答
祖国的老花朵
2楼-- · 2020-03-06 04:09

Error occurred because value of ASPNETCORE_ENVIRONMENT variable is different. In case of Visual Studio it was set to Development and in case of dotnet cli (as you can see in screenshot in question), it was Production.

To set ASPNETCORE_ENVIRONMENT variable using command line, use below command -

setx ASPNETCORE_ENVIRONMENT "Development"

For more information on how to set hosting environment, please refer this article.

查看更多
登录 后发表回答