VOOZH about

URL: https://www.nuget.org/packages/Ecng.Configuration/

⇱ NuGet Gallery | Ecng.Configuration 1.0.266




👁 Image
Ecng.Configuration 1.0.266

dotnet add package Ecng.Configuration --version 1.0.266
 
 
NuGet\Install-Package Ecng.Configuration -Version 1.0.266
 
 
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="Ecng.Configuration" Version="1.0.266" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Configuration" Version="1.0.266" />
 
Directory.Packages.props
<PackageReference Include="Ecng.Configuration" />
 
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 Ecng.Configuration --version 1.0.266
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Ecng.Configuration, 1.0.266"
 
 
#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 Ecng.Configuration@1.0.266
 
 
#: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=Ecng.Configuration&version=1.0.266
 
Install as a Cake Addin
#tool nuget:?package=Ecng.Configuration&version=1.0.266
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Ecng.Configuration

A lightweight and efficient configuration management library for .NET applications, providing typed access to configuration sections, application settings, and a flexible service registry.

Overview

Ecng.Configuration wraps and extends the .NET System.Configuration framework, offering:

  • Type-safe access to configuration sections and groups
  • Service registry pattern for dependency injection and service location
  • Event-driven notifications for service registration
  • AppSettings helpers with type conversion
  • Thread-safe operations for concurrent access

Installation

This library requires:

  • .NET Standard 2.0+ or .NET 6.0+
  • System.Configuration.ConfigurationManager package
<PackageReference Include="System.Configuration.ConfigurationManager" />

Key Features

1. Configuration Sections

Access configuration sections by type or name with built-in caching for performance.

Access by Type
using Ecng.Configuration;

// Get a custom configuration section by type
var section = ConfigManager.GetSection<MyCustomSection>();

// Section will be null if not found
if (section != null)
{
 // Use the section
 var value = section.SomeProperty;
}
Access by Name
// Get section by name
var connectionStrings = ConfigManager.GetSection<ConnectionStringsSection>("connectionStrings");

// Or without type parameter
var section = ConfigManager.GetSection("mySection");
Example Configuration File
<configuration>
 <configSections>
 <section name="mySection" type="MyNamespace.MyCustomSection, MyAssembly"/>
 </configSections>

 <mySection someProperty="value" />
</configuration>

2. Configuration Section Groups

Organize related configuration sections into groups.

// Get a section group by type
var group = ConfigManager.GetGroup<MyConfigurationGroup>();

// Get a section group by name
var systemWeb = ConfigManager.GetGroup<SystemWebSectionGroup>("system.web");

3. Application Settings

Simple access to AppSettings with type conversion support.

// Get a setting with default value
int timeout = ConfigManager.TryGet<int>("RequestTimeout", 30);

// Get a boolean setting
bool enableLogging = ConfigManager.TryGet<bool>("EnableLogging", false);

// Get a string setting
string apiUrl = ConfigManager.TryGet<string>("ApiUrl", "https://default-api.com");

// Direct access to all AppSettings
var allSettings = ConfigManager.AppSettings;
foreach (string key in allSettings.AllKeys)
{
 Console.WriteLine($"{key} = {allSettings[key]}");
}

Configuration File:

<configuration>
 <appSettings>
 <add key="RequestTimeout" value="60" />
 <add key="EnableLogging" value="true" />
 <add key="ApiUrl" value="https://api.example.com" />
 </appSettings>
</configuration>

4. Service Registry

A lightweight service locator pattern for managing application services without heavy DI frameworks.

Register Services
// Register a service with default name (type's AssemblyQualifiedName)
var securityProvider = new CollectionSecurityProvider(securities);
ConfigManager.RegisterService<ISecurityProvider>(securityProvider);

// Register a service with a custom name
ConfigManager.RegisterService<ILogger>("FileLogger", new FileLogger());
ConfigManager.RegisterService<ILogger>("ConsoleLogger", new ConsoleLogger());
Retrieve Services
// Get a service by type (using default name)
var provider = ConfigManager.GetService<ISecurityProvider>();

// Get a service by name
var fileLogger = ConfigManager.GetService<ILogger>("FileLogger");
var consoleLogger = ConfigManager.GetService<ILogger>("ConsoleLogger");

// Try to get a service (returns default if not found)
var optionalService = ConfigManager.TryGetService<IOptionalFeature>();
if (optionalService != null)
{
 // Service is available
}
Check Service Registration
// Check if a service is registered
if (ConfigManager.IsServiceRegistered<IMessageAdapter>())
{
 var adapter = ConfigManager.GetService<IMessageAdapter>();
}

// Check by name
if (ConfigManager.IsServiceRegistered<ILogger>("FileLogger"))
{
 // Use the file logger
}
Conditional Registration
// Register only if not already registered
ConfigManager.TryRegisterService<IDefaultService>(new DefaultServiceImpl());

// This won't override the existing registration
ConfigManager.TryRegisterService<IDefaultService>(new AnotherImpl());
Get All Services of a Type
// Get all registered services of a specific type
var allLoggers = ConfigManager.GetServices<ILogger>();

foreach (var logger in allLoggers)
{
 logger.Log("Application started");
}

5. Service Registration Events

React to service registration with event handlers or subscriptions.

Global Event Handler
// Subscribe to all service registrations
ConfigManager.ServiceRegistered += (type, service) =>
{
 Console.WriteLine($"Service registered: {type.Name}");
};

// Now register a service
ConfigManager.RegisterService<IMyService>(new MyServiceImpl());
// Output: Service registered: IMyService
Type-Specific Subscription
// Subscribe to a specific service type registration
ConfigManager.SubscribeOnRegister<ISecurityProvider>(provider =>
{
 Console.WriteLine($"SecurityProvider registered with {provider.Count} securities");
 // Initialize or configure the provider
});

// When this service is registered, the callback will be invoked
ConfigManager.RegisterService<ISecurityProvider>(new CollectionSecurityProvider(securities));

6. Service Fallback

Provide a fallback mechanism to create services on-demand when they're not registered.

// Set up a service fallback handler
ConfigManager.ServiceFallback += (type, name) =>
{
 // Create services dynamically based on type
 if (type == typeof(ILogger))
 {
 return new DefaultLogger();
 }

 if (type == typeof(ICache))
 {
 return new MemoryCache();
 }

 // Return null to indicate service cannot be created
 return null;
};

// Now getting an unregistered service will use the fallback
var logger = ConfigManager.GetService<ILogger>(); // Creates DefaultLogger

7. Access Underlying Configuration

For advanced scenarios, access the underlying .NET Configuration object.

// Get the underlying Configuration object
var config = ConfigManager.InnerConfig;

if (config != null)
{
 Console.WriteLine($"Configuration file: {config.FilePath}");

 // Access sections directly
 foreach (ConfigurationSection section in config.Sections)
 {
 Console.WriteLine($"Section: {section.SectionInformation.Name}");
 }
}

Complete Usage Example

Here's a complete example showing typical usage in an application:

using Ecng.Configuration;
using System;

namespace MyApplication
{
 public class Program
 {
 static void Main(string[] args)
 {
 // 1. Configure service fallback (optional)
 ConfigManager.ServiceFallback += CreateDefaultServices;

 // 2. Subscribe to service registrations (optional)
 ConfigManager.SubscribeOnRegister<ILogger>(logger =>
 {
 logger.Initialize();
 });

 // 3. Register core services
 ConfigManager.RegisterService<ILogger>(new FileLogger("app.log"));
 ConfigManager.RegisterService<IExchangeInfoProvider>(
 new InMemoryExchangeInfoProvider()
 );

 // 4. Register multiple implementations with names
 ConfigManager.RegisterService<ICache>("Memory", new MemoryCache());
 ConfigManager.RegisterService<ICache>("Distributed", new RedisCache());

 // 5. Read application settings
 var timeout = ConfigManager.TryGet<int>("Timeout", 30);
 var apiUrl = ConfigManager.TryGet<string>("ApiUrl");

 // 6. Get configuration section
 var customSection = ConfigManager.GetSection<CustomAppSettings>();

 // 7. Use services throughout your application
 RunApplication();
 }

 static void RunApplication()
 {
 // Retrieve services when needed
 var logger = ConfigManager.GetService<ILogger>();
 logger.Log("Application started");

 var provider = ConfigManager.GetService<IExchangeInfoProvider>();
 var exchanges = provider.GetExchanges();

 // Use named service
 var cache = ConfigManager.GetService<ICache>("Memory");
 cache.Set("key", "value");
 }

 static object CreateDefaultServices(Type type, string name)
 {
 // Provide default implementations
 if (type == typeof(ILogger))
 return new ConsoleLogger();

 return null;
 }
 }

 // Example interfaces
 public interface ILogger
 {
 void Initialize();
 void Log(string message);
 }

 public interface IExchangeInfoProvider
 {
 IEnumerable<Exchange> GetExchanges();
 }

 public interface ICache
 {
 void Set(string key, string value);
 string Get(string key);
 }
}

Thread Safety

All operations in ConfigManager are thread-safe:

  • Configuration sections and groups are cached on first access
  • Service registry operations use locking to prevent race conditions
  • Multiple threads can safely register and retrieve services concurrently
// Safe to call from multiple threads
Parallel.For(0, 100, i =>
{
 var service = ConfigManager.GetService<IMyService>();
 service.DoWork();
});

Best Practices

1. Register Services at Application Startup

// Register all services during application initialization
public static void ConfigureServices()
{
 ConfigManager.RegisterService<ILogger>(new Logger());
 ConfigManager.RegisterService<IDataService>(new DataService());
 // ... register other services
}

2. Use Type-Safe Access

// Prefer type-safe access over string-based access
var section = ConfigManager.GetSection<MySection>(); // Good
var section = (MySection)ConfigManager.GetSection("mySection"); // Less safe

3. Provide Defaults for AppSettings

// Always provide sensible defaults
var timeout = ConfigManager.TryGet<int>("Timeout", 30); // Good
var timeout = ConfigManager.TryGet<int>("Timeout"); // May return 0

4. Check Service Registration Before Use

// For optional services, check registration first
if (ConfigManager.IsServiceRegistered<IOptionalFeature>())
{
 var feature = ConfigManager.GetService<IOptionalFeature>();
 feature.Execute();
}

5. Use Named Services for Multiple Implementations

// Register different implementations with descriptive names
ConfigManager.RegisterService<IRepository>("User", new UserRepository());
ConfigManager.RegisterService<IRepository>("Product", new ProductRepository());

// Retrieve specific implementation
var userRepo = ConfigManager.GetService<IRepository>("User");

Common Patterns

Singleton Services

// Register singleton services
var singletonService = new MySingletonService();
ConfigManager.RegisterService<IMySingletonService>(singletonService);

// Every call returns the same instance
var service1 = ConfigManager.GetService<IMySingletonService>();
var service2 = ConfigManager.GetService<IMySingletonService>();
// service1 == service2

Factory Pattern with Fallback

// Use fallback as a factory
ConfigManager.ServiceFallback += (type, name) =>
{
 if (type == typeof(IDataService))
 {
 // Create with dependencies
 var logger = ConfigManager.GetService<ILogger>();
 return new DataService(logger);
 }
 return null;
};

// First call creates and registers the service
var service = ConfigManager.GetService<IDataService>();

Configuration-Driven Service Selection

// Select service implementation based on configuration
var loggerType = ConfigManager.TryGet<string>("LoggerType", "Console");

ILogger logger = loggerType switch
{
 "File" => new FileLogger(),
 "Console" => new ConsoleLogger(),
 "Database" => new DatabaseLogger(),
 _ => new ConsoleLogger()
};

ConfigManager.RegisterService<ILogger>(logger);

Migration from .NET ConfigurationManager

If you're migrating from using ConfigurationManager directly:

// Before:
var setting = ConfigurationManager.AppSettings["MySetting"];

// After:
var setting = ConfigManager.AppSettings["MySetting"];
// Or with type conversion:
var typedSetting = ConfigManager.TryGet<int>("MySetting", 0);

// Before:
var section = (MySection)ConfigurationManager.GetSection("mySection");

// After:
var section = ConfigManager.GetSection<MySection>("mySection");

Limitations

  • Configuration file must exist and be valid at application startup
  • Configuration sections are cached on first load (not reloaded automatically)
  • Service registry is in-memory only (not persisted)
  • Not a replacement for full dependency injection frameworks (suitable for simpler scenarios)

See Also

License

Part of the StockSharp/Ecng framework.

Product Versions Compatible and additional computed target framework versions.
.NET net6.0 net6.0 is compatible.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Ecng.Configuration:

Package Downloads
StockSharp.BusinessEntities

Trading entities (security, trade etc.). More info on web site https://stocksharp.com/store/

StockSharp.Configuration

Configuration components. More info on web site https://stocksharp.com/store/

StockSharp.Finam

Finam

StockSharp.Mfd

MFD

StockSharp.Algo.Compilation

Compilation components. More info on web site https://stocksharp.com/store/

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Ecng.Configuration:

Repository Stars
StockSharp/StockSharp
Algorithmic trading and quantitative trading open source platform to develop trading robots (stock markets, forex, crypto, bitcoins, and options).
Version Downloads Last Updated
1.0.266 57 6/17/2026
1.0.265 124 6/15/2026
1.0.264 115 6/12/2026
1.0.263 545 5/15/2026
1.0.262 115 5/14/2026
1.0.261 295 5/3/2026
1.0.260 579 4/14/2026
1.0.259 1,172 3/17/2026
1.0.258 151 3/17/2026
1.0.257 763 3/3/2026
1.0.256 263 2/28/2026
1.0.255 879 2/4/2026
1.0.254 310 2/1/2026
1.0.253 221 1/22/2026
1.0.252 236 1/19/2026
1.0.251 154 1/18/2026
1.0.250 152 1/18/2026
1.0.249 223 1/14/2026
1.0.248 153 1/13/2026
1.0.247 147 1/13/2026
Loading failed

Configuration: guard InitSectionGroups against a malformed .config