ASP.Net Core应用程序可在Visual Studio中使用,但不能与dotnet运行一起使用 [英] ASP.Net Core app works in visual studio but not with dotnet run

查看:260
本文介绍了ASP.Net Core应用程序可在Visual Studio中使用,但不能与dotnet运行一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net核心应用程序,我是通过Visual Studio开始构建的,通常当我按F5或按Visual Studio中的调试按钮时,它可以正常工作.

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.

但是,如果我尝试从命令行使用dotnet run,它仍然可以工作并开始在默认端口5000上侦听(我假设这是红est侦听).

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).

但是,如果我尝试使用 http://localhost:5000 通过任何浏览器访问该页面,错误,提示无法连接.我了解如果我是从Visual Studio运行的,它将自动将我的应用放到IIS后面,但是我不明白为什么如果该应用只是自托管的,为什么它不起作用.

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.

我想将我的项目移到linux环境中,这似乎是唯一现在阻止我的事情.我已经测试过通过cli创建新的dotnet应用程序,并且如果我尝试通过浏览器进行访问,这些应用程序也可以正常工作,似乎我在VS中创建的该特定应用程序不起作用.

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.

我的dotnet --version返回1.0.4

因此,为了补充下面的Sanket回答,我还弄清楚了只有将ASPNETCORE_ENVIRONMENT设置为Development时,我的应用才可以运行的真正原因.

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;
}

当我决定为暂存环境配置UseDeveloperExceptionPage()时,我意识到了这个问题,当我在问题所在的地方运行它时,这一点就变得显而易见.我的控制器类尝试在未首先配置邮件服务时对其进行初始化.因此,我对控制器进行了如下更改:

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.

推荐答案

发生错误,因为 ASPNETCORE_ENVIRONMENT 变量的值不同.如果是Visual Studio,则将其设置为Development;如果是dotnet cli,则将其设置为Production.

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.

要使用命令行设置ASPNETCORE_ENVIRONMENT变量,请使用以下命令-

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.

这篇关于ASP.Net Core应用程序可在Visual Studio中使用,但不能与dotnet运行一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆