0

In my startup.cs class I have a check to see if it's DEV environment and has a lot of key-value pairs in my appsettings.json and appsettings.development.json file.

however I want to encapsulate some code that only runs in the DEV environment and when deployment to either UAT, TEST or LIVE the code is not deployed.

how can this be achieved?

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
           
        }
        else
        {
             app.UseHsts();
        }

And have a class, as this as a value in my class to get which URL the subdomain runs (which also would be the value of the roles in my IDENTITY database

  private string SubDomain(HttpContext httpContext)
        {
            HttpRequest request = httpContext.Request;

            string url = request.Headers["Host"];
            string subDomainUrl = url.Replace("https://", "").Split('/')[0];

//I want to be able to get my domain based on http://localhost:44312/?role=adminUsers //so if it's running on localhost and the role is admin users this only would be executed locally, as the remote URL has http://adminusers.mydomain.com

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<InjectMyClassHere>(); //My class or interface goes here

I hope this is clear. How can I achieve this? ie have this class only execute in DEV and not deployed in UAT, TEST, OR LIVE environments

csmaster
  • 579
  • 4
  • 14
Ehioze
  • 39
  • 1
  • 4
  • Does this answer your question? [Proper use of IsDevelopement() on self hosted .net core 2.2 application](https://stackoverflow.com/questions/57329341/proper-use-of-isdevelopement-on-self-hosted-net-core-2-2-application) – Karl Knechtel Dec 12 '21 at 09:01
  • Use an interface and expose different implementations through DI depending on env – arynaq Dec 12 '21 at 11:09

1 Answers1

5

If you surround code you only want in the dev environment in

#if DEBUG

//code here

#endif

Then that code will only exist in the compiled code when Debug mode (at the top of VS) is active. The compiler will literally cut out and throw away any code between the directives when the compile runs in Release mode - as if you'd highlighted it and pressed Delete before you built

Visual Studio helps you by lighting up/greying out code that is/not active because of this:

See [Debug] mode is set at the top and the code is lit up:

enter image description here

Now see [Release] mode is set at the top. Code is grey because it is removed from the compilation. Also note there is now a compile error because OnlyDebug does not exist:

enter image description here

If your VS does not behave like this, check the project properties.. Release mode shout NOT define DEBUG constant, and Debug mode SHOULD define DEBUG constant:

enter image description here

enter image description here

When deployed to production your code should, of course, not use the Debug build mode for this to work out..


Footnote: you can put #else too, or you can use #if !DEBUG to provide "this or that" and "non debug" code:

enter image description here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80