Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Asp.net Core 2.0 is IHostingEnvironment extendable

Currently, Asp.Net core 2 IHostingEnvironment has three boolean properties

  • IsProduction
  • IsStaging
  • IsDevelopment

is it extendable if I wanted to create two additional properties? (e.g. IsTesting, IsCloudDb)

Since I am not a professional programmer I'm not sure how to go about this IF it is doable.

like image 782
dinotom Avatar asked Nov 25 '25 09:11

dinotom


1 Answers

Those are not properties but extension methods for IHostingEnvironment interface. All those extension methods do is compare IHostingEnvironment.EnvironmentName with predefined string. You can do the same:

public static class EnvironmentExtensions {
    const string CloudDbEnvironment = "CloudDb";
    const string TestingEnvironment = "Testing";

    public static bool IsCloudDb(this IHostingEnvironment env) {
        return env.IsEnvironment(CloudDbEnvironment);
    }

    public static bool IsTesting(this IHostingEnvironment env) {
        return env.IsEnvironment(TestingEnvironment);
    }
}

Of course you should set EnvironmentName to the related string for those methods to return true.

like image 117
Evk Avatar answered Nov 27 '25 21:11

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!