Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
BestASPNETHostingReview.com | Best and recommended ASP.NET 5 Hosting. ASP.NET 5 is Microsoft’s latest web framework and the new big thing on the .NET landscape. It comes with a whole lot of new features and other changes which makes it very distinctive from previous ASP.NET versions. It is basically a complete re-write of the framework, optimized for the cloud and cross platform compatible. If you haven’t checked it out yet then you are definitely missing out!
Some of the newly introduced features are the new configuration options, which come as part of the Microsoft.Extensions.Configuration NuGet packages. They allow you a more flexible way of loading configuration values into an application and make it significantly easier to move an application across different environments without having to change configuration files or run through nasty configuration transformations as part of the process. Scott Hanselman has nicely summarized this topic in one of his recent blog posts.
It is needless to say that a lot of people are hugely excited about the new framework and many projects are being written in ASP.NET 5 right now, but there is also a significant amount of people who cannot easily migrate their existing projects to the new framework yet. This might be for various reasons but essentially means they are stuck on an older version of ASP.NET at least for a while.
However, it doesn’t mean that those people cannot already benefit from some of the new ideas which have been publicly introduced in ASP.NET 5 such as the new configuration options.
Implementing ASP.NET 5 like configuration options
The idea is to provide multiple sources from where an app can load configuration values. Additionally we want to put those sources into an order of precedence, where one source overrules another.
While ASP.NET 5 implementes a Builder pattern, you can easily achieve the same thing with a good old Decorator.
First we need an interface which provides a method to retrieve a config value:
1 2 3 4 | public interface IConfiguration { string Get(string key); } |
Next I implement a simple class which loads a value from an app- or web.config file:
1 2 3 4 5 6 7 8 9 | using System.Configuration; public class AppConfigConfiguration : IConfiguration { public string Get(string key) { return ConfigurationManager.AppSettings[key]; } } |
This is trivial and probably the same as what you have in your current application, just that it has been wrapped in a class which is behind an interface.
Finally we can provide an additional implementation which loads a value from the environment variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; public class EnvironmentVariablesConfiguration : IConfiguration { private readonly IConfiguration _backupConfiguration; public EnvironmentVariablesConfiguration( IConfiguration backupConfiguration) { _backupConfiguration = backupConfiguration; } public string Get(string key) { var value = Environment.GetEnvironmentVariable(key); return value ?? _backupConfiguration.Get(key); } } |
This particular implementation wraps another IConfiguration
implementation. If the setting does not exist in the environment variables then it defers to the next implementation. This could be anything and in this particular example will be the AppConfigConfiguration
:
1 2 3 | container.Register<IConfiguration>( new EnvironmentVariablesConfiguration( new AppConfigConfiguration())); |
It is really as simple as that. The order of precedence is determined by the order of the classes being put together.
This pattern can be extended as far as you like. For example I could add two more classes and compose something like this:
1 2 3 4 5 | container.Register<IConfiguration>( new EnvironmentVariablesConfiguration( new JsonFileConfiguration( new AzureTableStorageConfiguration( new AppConfigConfiguration())))); |
Now I can access configuration values from other classes through an IConfiguration
dependency:
1 | var value = _configuration.Get("SomeKey"); |
With this trick you can easily implement a “cloud optimized” configuration in any version of ASP.NET and follow good practice patterns no matter where you code!