Home > Net >  Is it possible to write a class that only executes in DEVELOPMENT ENVIRONMENT?
Is it possible to write a class that only executes in DEVELOPMENT ENVIRONMENT?

Time:12-13

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

  • Related