![]() |
VOOZH | about |
dotnet add package Ecng.Configuration --version 1.0.251
NuGet\Install-Package Ecng.Configuration -Version 1.0.251
<PackageReference Include="Ecng.Configuration" Version="1.0.251" />
<PackageVersion Include="Ecng.Configuration" Version="1.0.251" />Directory.Packages.props
<PackageReference Include="Ecng.Configuration" />Project file
paket add Ecng.Configuration --version 1.0.251
#r "nuget: Ecng.Configuration, 1.0.251"
#:package Ecng.Configuration@1.0.251
#addin nuget:?package=Ecng.Configuration&version=1.0.251Install as a Cake Addin
#tool nuget:?package=Ecng.Configuration&version=1.0.251Install as a Cake Tool
A lightweight and efficient configuration management library for .NET applications, providing typed access to configuration sections, application settings, and a flexible service registry.
Ecng.Configuration wraps and extends the .NET System.Configuration framework, offering:
This library requires:
System.Configuration.ConfigurationManager package<PackageReference Include="System.Configuration.ConfigurationManager" />
Access configuration sections by type or name with built-in caching for performance.
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;
}
// Get section by name
var connectionStrings = ConfigManager.GetSection<ConnectionStringsSection>("connectionStrings");
// Or without type parameter
var section = ConfigManager.GetSection("mySection");
<configuration>
<configSections>
<section name="mySection" type="MyNamespace.MyCustomSection, MyAssembly"/>
</configSections>
<mySection someProperty="value" />
</configuration>
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");
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>
A lightweight service locator pattern for managing application services without heavy DI frameworks.
// 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());
// 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 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
}
// 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 registered services of a specific type
var allLoggers = ConfigManager.GetServices<ILogger>();
foreach (var logger in allLoggers)
{
logger.Log("Application started");
}
React to service registration with event handlers or subscriptions.
// 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
// 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));
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
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}");
}
}
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);
}
}
All operations in ConfigManager are thread-safe:
// Safe to call from multiple threads
Parallel.For(0, 100, i =>
{
var service = ConfigManager.GetService<IMyService>();
service.DoWork();
});
// Register all services during application initialization
public static void ConfigureServices()
{
ConfigManager.RegisterService<ILogger>(new Logger());
ConfigManager.RegisterService<IDataService>(new DataService());
// ... register other services
}
// Prefer type-safe access over string-based access
var section = ConfigManager.GetSection<MySection>(); // Good
var section = (MySection)ConfigManager.GetSection("mySection"); // Less safe
// Always provide sensible defaults
var timeout = ConfigManager.TryGet<int>("Timeout", 30); // Good
var timeout = ConfigManager.TryGet<int>("Timeout"); // May return 0
// For optional services, check registration first
if (ConfigManager.IsServiceRegistered<IOptionalFeature>())
{
var feature = ConfigManager.GetService<IOptionalFeature>();
feature.Execute();
}
// 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");
// 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
// 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>();
// 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);
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");
Part of the StockSharp/Ecng framework.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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/ |
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 | 564 | 6/17/2026 |
| 1.0.265 | 171 | 6/15/2026 |
| 1.0.264 | 161 | 6/12/2026 |
| 1.0.263 | 549 | 5/15/2026 |
| 1.0.262 | 118 | 5/14/2026 |
| 1.0.261 | 297 | 5/3/2026 |
| 1.0.260 | 580 | 4/14/2026 |
| 1.0.259 | 1,175 | 3/17/2026 |
| 1.0.258 | 155 | 3/17/2026 |
| 1.0.257 | 766 | 3/3/2026 |
| 1.0.256 | 264 | 2/28/2026 |
| 1.0.255 | 880 | 2/4/2026 |
| 1.0.254 | 313 | 2/1/2026 |
| 1.0.253 | 222 | 1/22/2026 |
| 1.0.252 | 237 | 1/19/2026 |
| 1.0.251 | 155 | 1/18/2026 |
| 1.0.250 | 154 | 1/18/2026 |
| 1.0.249 | 224 | 1/14/2026 |
| 1.0.248 | 155 | 1/13/2026 |
| 1.0.247 | 150 | 1/13/2026 |