RealSense.DomainBasedConfig 1.14.0

dotnet add package RealSense.DomainBasedConfig --version 1.14.0
                    
NuGet\Install-Package RealSense.DomainBasedConfig -Version 1.14.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RealSense.DomainBasedConfig" Version="1.14.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RealSense.DomainBasedConfig" Version="1.14.0" />
                    
Directory.Packages.props
<PackageReference Include="RealSense.DomainBasedConfig" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RealSense.DomainBasedConfig --version 1.14.0
                    
#r "nuget: RealSense.DomainBasedConfig, 1.14.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RealSense.DomainBasedConfig@1.14.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RealSense.DomainBasedConfig&version=1.14.0
                    
Install as a Cake Addin
#tool nuget:?package=RealSense.DomainBasedConfig&version=1.14.0
                    
Install as a Cake Tool

DomainBasedConfig

This library provides a configuration of for named Environments along with Database settings and other settings.

How to use

  1. Use Nuget to add to your project:

    Via nuget Package Manager:

     Install-Package RealSense.DomainBasedConfig
    

    Via .NET CLI:

     dotnet add package RealSense.DomainBasedConfig
    

    In project file:

     <PackageReference Include="RealSense.DomainBasedConfig" Version="1.12.0" />
    
  2. In your DI configuration, bind one of two services to the IDomainBasedConfigService as follows:

     // .NET Framework 4.8
     kernel.Bind<IDomainBasedConfigService>().To<FrameworkDomainBasedConfigService>().InSingletonScope();
    
     // .NET
     kernel.Bind<IDomainBasedConfigService>().To<CoreDomainBasedConfigService>().InSingletonScope();
    
  3. Add details about environments into your configuration

    .NET Framework 4.8 Add to app.config or web.config (see example as shown in step 1 below in the legacy app.config or web.config)

    .NET Add the following to appsettings.json:

     {
     	"Environments":{
     		"MyDev1":{
     			"Database":{
     				"Server":"(local)",
     				"Name":"Database1",
     				"UseWindowsAuthentication":true
     			},
     			"Settings":{
     				"Setting1":"Value1",
     				"Setting2":"Value2",
     				"Setting3":"Value3",
     			}
     		},
     		"MyDev2":{ ... }
     		"MyTest1":{ ... }
     		"MyTest2":{ ... }
     		"MyLive":{ ... }
     	},
     	"Domains":{
     		"localhost":"MyDev1",
     		"localhost:8080":"MyDev2",
     		"mysite.net":"MyLive",
     		"test1.mysite.net":"MyTest1",
     		"test2.mysite.net":"MyTest2",
     	}
     }
    
  4. Inject the IDomainBasedConfigService using standard DI practices (typically constructor injection or possibly Property injection)

  5. Get database details and settings for an artbitory configuration using:

    IConfig config = DomainBasedConfigService.GetConfig()

  6. Get a modified named database connection strings or environment using the following interfaces:

     /// <summary>
     /// Gets a resolved connection string by name for the given environment
     /// </summary>
     /// <param name="name">Connection String Name</param>
     /// <param name="environment">Environment Name</param>
     /// <returns>Connection String</returns>
     string GetEnvConnectionString(string name, string environment);
    
     /// <summary>
     /// Get an environment by name
     /// </summary>
     /// <param name="name">Name of environment to get</param>
     /// <returns>IEnvironment or null if not found</returns>
     IEnvironment GetEnvironment(string name);
    
     /// <summary>
     /// Get an environment from HttpContext
     /// </summary>
     /// <param name="host">Host Name</param>
     /// <param name="port">Port Number</param>
     /// <returns>IEnvironment or null if not found</returns>
     IEnvironment GetEnvironmentFromHttp(string host, int port);
    

===================== LEGACY APPROACH BELOW. USE DI INSTEAD =====================

  1. Add the following section to your .config file
<configuration>
	<configSections>
		<section name="domainBasedConfig" type="RealSense.DomainBasedConfig.DomainBasedConfigSection, RealSense.DomainBasedConfig" allowLocation="true" allowDefinition="Everywhere" />
	</configSections>
</configuration>
  1. Add the following section to your .config file
<configuration>
	<domainBasedConfig>
		<domains>
			<add domain="localhost" port="3000" map="DevEnv" />
			<add domain="test1.example.com" map="Test1Env" />
			<add domain="example.com" map="LiveEnv" />
			<add domain="www.example.com" map="LiveEnv" />
		</domains>
		<environments>
			<add name="DevEnv">
				<database server="(local)" database="Dev1" />
				<settings>
					<add name="Setting1" value="Apple" />
					<add name="Setting2" value="Cheese" />
				</settings>
			</add>
			<add name="Test1Env">
				<database server="(local)" database="Test1" />
				<settings>
					<add name="Setting1" value="Grape" />
					<add name="Setting2" value="Egg" />
				</settings>
			</add>
			<add name="LiveEnv">
				<database server="(local)\MSSQL2020" database="LiveDatabase" />
				<settings>
					<add name="Setting1" value="Plum" />
					<add name="Setting2" value="Date" />
				</settings>
			</add>
		</environments>
</configuration>

NOTE: The <domains> section is only required for Web Applications and provides automatic resolution of which environment to use based on the HttpContext.Request details. For console applications (for example) you can request settings and connection strings by stating the environment name.

  1. Access to a connection string from Entity Framework is done adding a new partial class that implements a static method as follows:
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;

	namespace WebService
	
	public partial class MyEntities
	{
		/// <summary>
		/// Initializes a new instance using a modified connection string 
		/// based on host name.
		/// </summary>
		public static MyEntities CreateDomainBased()
		{
			string s = RealSense.DomainBasedConfig.Instance.GetHttpConnectionString("MyEntities");
			return new MyEntities(s);
		}
	}
	
  1. Change your Entity Framework instantiation from
	using (var ctx = new MyEntities())
	{
	}
TO:
	using (var ctx = MyEntities.CreateDomainBased())
	{
	}

Example Application

// Program.cs
using Inst = RealSense.DomainBasedConfig.Instance;
using Debug = System.Diagnostics.Debug;

namespace TestDomainBasedConfig;

internal class Program
{
	private static void Main(string[] args)
	{
		Debug.WriteLine("1) Dump Config");
		Debug.WriteLine(Inst.DumpConfig());

		Debug.WriteLine("2) Get Environment (valid)");
		var env1 = Inst.GetEnvironment("Dev2Env");
		Debug.WriteLine(env1);

		Debug.WriteLine("3) Get Configuration String \"Stock\" under \"Dev2Env\"");
		Debug.WriteLine(Inst.GetEnvConnectionString("Stock", "Dev2Env"));

		Debug.WriteLine("4) Get Setting from Environment (env1) (will throw if setting doesn't exist)");
		var v1 = env1.Settings["Setting1"];
		Debug.Assert(v1 == "Strawberry");
		Debug.WriteLine($"Setting1=\"{v1}\" under \"{env1.Name}\" environment..");

		Debug.WriteLine("5) Safely get non-existant Setting from Environment (env1) (returns provided defaultValue or null if doesn't exist)");
		var value2 = env1.Settings.Get("Setting2");
		Debug.Assert(value2 is null);
		t = value2 ?? "(null)";
		Debug.WriteLine($"Setting Setting2: {t}");

		Debug.WriteLine("7) Get Environment based on HttpContext");
		// NOTE: will throw for non-web applications as HttpContext.Current will be null
		try
		{
			var env3 = Inst.GetEnvironmentFromHttp();
			Debug.Assert(env3 is not null);
		}
		catch (RealSense.DomainBasedConfig.NonHttpEnvironmentException ex)
		{
			Debug.WriteLine(ex.Message);
		}

		Debug.WriteLine("8) Get Connection String based on HttpContext");
		// NOTE: This will throw for non-web applications as HttpContext.Current will be null
		try
		{
			var connString2 = Inst.GetHttpConnectionString("HumanResources");
			Debug.Assert(connString2 is not null);
		}
		catch (RealSense.DomainBasedConfig.NonHttpEnvironmentException ex)
		{
			Debug.WriteLine(ex.Message);
		}

		Debug.WriteLine("9) Enumerate settings in an environment");
		Debug.WriteLine($"Settings for {env1.Name} environment:");
		foreach(var setting in env1.Settings)
			Debug.WriteLine($" - {setting.Key} => {setting.Value}");
		Debug.WriteLine($"Settings for Test1Env environment:");
		foreach(var setting in Inst.GetEnvironment("Test1Env").Settings)
			Debug.WriteLine($" - {setting.Key} => {setting.Value}");

		Debug.WriteLine("10) Enumerate all Settings for all Environments");
		Debug.WriteLine($"Enumerate all settings:");
		foreach (var env in Inst.GetConfig().Environments)
		{
			Debug.WriteLine($" - Environment: {env.Key}");
			foreach (var setting in env.Value.Settings)
				Debug.WriteLine($" - - {setting.Key} => {setting.Value}");
		}

		Debug.WriteLine("11) Enumerate all Connection Strings for all Environments");
		var csList = new string[] { "Stock", "Marketing", "HumanResources", "Development" };
		Debug.WriteLine($"Enumerate all connection strings:");
		foreach (var env in Inst.GetConfig().Environments)
		{
			var db = env.Value.Database;
			Debug.WriteLine($" - Environment: {env.Key}; Database: {db}");
			foreach (var cs in csList)
			{
				var str = Inst.GetEnvConnectionString(cs, env.Key);
				Debug.WriteLine($" - - {cs} => {str}");
			}
		}
	}
}
// App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<section name="domainBasedConfig" type="RealSense.DomainBasedConfig.DomainBasedConfigSection, RealSense.DomainBasedConfig" allowLocation="true" allowDefinition="Everywhere" />
	</configSections>
	<startup>
		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
	</startup>
	<connectionStrings>
		<remove name="LocalSqlServer" />
		<add name="Stock" connectionString="Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
		<add name="HumanResources" connectionString="metadata=res://HumanResources/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
	</connectionStrings>
	<domainBasedConfig>
		<domains>
			<add domain="localhost" port="3000" map="MyDev1" />
			<add domain="example1.com" port="*" map="MyDev1" />
			<add domain="example2.com" map="MyDev1" />
			<add domain="example3.com" map="MyLive" />
			<add domain="www.example3.com" map="MyLive" />
			<add domain="*" port="5000" map="MyDev1" />
			<add domain="*" port="5001" map="MyDev1" />
		</domains>
		<environments>
			<add name="MyDev1">
				<database server="(local)" database="Dev1Database" />
				<settings>
					<add name="Setting1" value="Strawberry" />
					<add name="Setting2" value="Pear" />
				</settings>
			</add>
			<add name="MyLive">
				<database server="(local)" database="LiveDatabase" />
				<settings>
					<add name="Setting1" value="Plum" />
					<add name="Setting2" value="Date" />
				</settings>
			</add>
		</environments>
	</domainBasedConfig>
</configuration>
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.14.0 36 5/22/2026
1.13.0 36 5/21/2026
1.12.0 36 5/20/2026
1.11.0 29 5/20/2026
1.10.0 37 5/20/2026
1.9.0 42 5/20/2026
1.8.0 34 5/20/2026
1.7.0 32 5/20/2026
1.5.0 650 9/7/2021
1.4.0 560 9/7/2021
1.3.0 552 4/17/2021
1.2.0 828 8/13/2019
1.1.0 962 12/4/2018
1.0.0 1,336 8/16/2017