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?}");
        });
    }
}
Elnoor
  • 3,401
  • 4
  • 24
  • 39
  • 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. – rickvdbosch Apr 09 '18 at 19:57
  • @rickvdbosch, ok, but how ? I have done the reverse of all above, still getting this "hintish" error. – Elnoor Apr 09 '18 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](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.developerexceptionpageextensions.usedeveloperexceptionpage?view=aspnetcore-2.0#Microsoft_AspNetCore_Builder_DeveloperExceptionPageExtensions_UseDeveloperExceptionPage_Microsoft_AspNetCore_Builder_IApplicationBuilder_) – rickvdbosch Apr 09 '18 at 20:05
  • I have done that one already as well, which didn't help either @rickvdbosch – Elnoor Apr 09 '18 at 20:06
  • you should be able to set it within web config as well – Neville Nazerane Apr 09 '18 at 20:38
  • @NevilleNazerane as default there is no web.config file in .net core apps – Elnoor Apr 09 '18 at 20:52
  • @rickvdbosch is right - you are *not* in development mode if you see this message. If you were, you'd see a different layout containing a stack trace, amongst other things. –  Apr 12 '18 at 14:36
  • @RhysWatkin ok, how do i make it to be in development mode ? – Elnoor Apr 12 '18 at 14:48
  • As @rickvdbosch also said, "Set the ASPNETCORE_ENVIRONMENT variable to Development and restart the application." –  Apr 12 '18 at 14:48
  • @RhysWatkin as i have said before, i have already checked that with no success – Elnoor Apr 12 '18 at 14:50
  • So -- did you try setting it in (a) launchsettings, (b) as an environment variable, (c) both, (d) neither? –  Apr 12 '18 at 14:53
  • @RhysWatkin yes, and 80 more combinations. – Elnoor Apr 12 '18 at 14:55
  • Also, I am aware that Beanstalk does not propagate environment variables to dotnet core as we might expect: although [this question](https://stackoverflow.com/questions/40127703/aws-elastic-beanstalk-environment-variables-in-asp-net-core-1-0/49615749#49615749) was about dotnet core 1.0 I answered it for dotnet core 2.0 –  Apr 12 '18 at 14:55
  • @RhysWatkin EB doesn't even save/store the variables, so that is another issue. And i don't want to rely on that. Definitely there should be a way to set app to be in dev mode within .net core – Elnoor Apr 12 '18 at 14:59
  • Would you mind showing your `launchsettings.json` and `Startup.cs`? –  Apr 12 '18 at 15:02
  • @mrblewog check the question again please, i added those – Elnoor Apr 13 '18 at 06:27

3 Answers3

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

mukesh joshi
  • 584
  • 5
  • 19
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();
مهدی
  • 333
  • 3
  • 6