1

I have a .NET Core MVC app that I deployed to AWS ElasticBeanstalk. But when i go to the app i get an error with message:

Development environment should not be enabled in deployed applications

  1. In launchsettings.json file i have set ASPNETCORE_ENVIRONMENT's value to Production.
  2. When i deploy app using Visual Studio (AWS toolkit) i set Project build configuration's value to Release.
  3. I have also created environment variable with name ASPNETCORE_ENVIRONMENT and value Production in EB Software Configuration.

But I am still getting the same error, any idea what would be the fix ? enter image description here

My launchSettings.json file looks like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://DUMMY.us-west-2.elasticbeanstalk.com/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "http://DUMMY.us-west-2.elasticbeanstalk.com/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "AlacsWeb": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://DUMMY.us-west-2.elasticbeanstalk.com/"
    }
  }
}

And startup.cs file:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddMvc();

        // Add http context
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
16
  • 1
    Development environment should not be enabled in deployed applications is not the error message. It's a hint about not enabling Development Mode in production environments. You're probably getting a 'normal' HttpStatus code 500. Please do enable development just to have it display the actual error message real quick. And don't forget to disable it after testing. Apr 9, 2018 at 19:57
  • @rickvdbosch, ok, but how ? I have done the reverse of all above, still getting this "hintish" error.
    – Elnoor
    Apr 9, 2018 at 20:03
  • Set the ASPNETCORE_ENVIRONMENT variable to Development and restart the application. If you have a fairly standard implementation, chances are Development gives you a DeveloperExceptionPage Apr 9, 2018 at 20:05
  • I have done that one already as well, which didn't help either @rickvdbosch
    – Elnoor
    Apr 9, 2018 at 20:06
  • you should be able to set it within web config as well Apr 9, 2018 at 20:38

3 Answers 3

1

Thanks for posting your code. I'm answering here because there's more to say than the comment form will allow.

TLDR

Your environment variables are ignored in EBS because your startup isn't picking them up.

I don't know what influence launchSettings.json has in EBS but given that the environment variables in that file are ignored I suspect the answer is none whatsoever.

You can save environment variables in EBS.

launchSettings.json

I don't use these myself, so what follows is research and trivial testing.

This MS article claims that this file only kicks in for running in Visual Studio

When using Visual Studio, environment variables may be set in the launchSettings.json file.

However I know from a simple test that these are also picked up by dotnet run in the project directory. Also, I know that VS Code ignores it in favour of .vscode/launch.json.

What I do not know is whether IIS pays any attention to it. (IIS in an EBS instance I mean, as opposed to IIS Express on your dev box).

Environment variables

I think I can see why environment variables are being ignored.

Startup.cs has an alternative constructor which lets you build the configuration object from environment variables, configuration files, and so on. It accepts an IHostingEnvironment instance.

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
                .AddEnvironmentVariables(); // <--- This picks up env variables
        Configuration = builder.Build();
    }


    public IConfigurationRoot Configuration { get; }

    // etc ....
}

EB environment variables

As I explained in a comment, EB + dotnet core 2 are in a right mess over environment variables. Our solution is not to parse the infernal file per my earlier answer but to Dockerise our dotnet apps.

That said, you can save environment variables in EB. As you say, Software Configuration is the correct place to enter them. Then, click on your environment (the green/grey/whatever card as it appears in EB), go to the Actions menu, and Save Configuration.

1

launchsettings.json is visual studio build specific. It doesn't impact deployment.

Following question has provided some insight on the issue

AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0

0

a crazy solution:

replace:

  if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

to

  app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.