Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

C# ASP.Net 5 configuration and backwards compatibility with Class Libraries

$
0
0

I've got multiple legacy libraries which configure themselves via the ConfigurationManager. Example:

var port = ConfigurationManager.AppSettings["service.port"];

The new ASP.Net system prefers strongly typed models based on an additional "config.json" file. Example (taken from Rick Strahl's Web Log [1]):

//from AppSettings.cs
public class AppSettings
{
    public string SiteTitle { get; set; }
}

//from config.json
{
    "AppSettings": {"SiteTitle": "WebApplication2",
    },"Data": {"DefaultConnection": {"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=blahfoo;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    }
}

// from Startup.cs
public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        // Setup configuration sources.
        var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

        configuration.AddEnvironmentVariables();
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Application settings to the services container.
        services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

        …
    }
}

My question: Is there a way to embrace the new ASP.Net 5 and it's strongly typed configuration methodology while maintaining backwards compatibility with my other application libraries?

Or rather, can I utilize common libraries from our portfolio without having to rewrite them?

[1]: https://weblog.west-wind.com/posts/2015/Jun/03/Strongly-typed-AppSettings-Configuration-in-ASPNET-5

(also posted on http://stackoverflow.com/questions/32634279/c-sharp-asp-net-5-configuration-and-backwards-compatibility-with-class-libraries)


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>