Puffix.IoC.Configuration
3.3.0
dotnet add package Puffix.IoC.Configuration --version 3.3.0
NuGet\Install-Package Puffix.IoC.Configuration -Version 3.3.0
<PackageReference Include="Puffix.IoC.Configuration" Version="3.3.0" />
paket add Puffix.IoC.Configuration --version 3.3.0
#r "nuget: Puffix.IoC.Configuration, 3.3.0"
// Install Puffix.IoC.Configuration as a Cake Addin #addin nuget:?package=Puffix.IoC.Configuration&version=3.3.0 // Install Puffix.IoC.Configuration as a Cake Tool #tool nuget:?package=Puffix.IoC.Configuration&version=3.3.0
Puffix IoC Configuration
Extension for including Microsoft.Extensions.Configuration in the Puffix IoC library.
Minimal implementation
In the sample below, Autofac is used.
First, a base class is needed to reference the objects to map:
using Autofac;
using Microsoft.Extensions.Configuration;
using Puffix.IoC;
using Puffix.IoC.Configuration;
namespace YourAppName;
public class IoCContainer : IIoCContainerWithConfiguration
{
private readonly IContainer? container;
public IConfigurationRoot ConfigurationRoot { get; }
public IoCContainer(ContainerBuilder containerBuilder, IConfigurationRoot configuration)
{
// Self-register the container.
containerBuilder.Register(_ => this).As<IIoCContainerWithConfiguration>().SingleInstance();
containerBuilder.Register(_ => this).As<IIoCContainer>().SingleInstance();
container = containerBuilder.Build();
ConfigurationRoot = configuration;
}
public static IIoCContainerWithConfiguration BuildContainer(IConfigurationRoot configuration)
{
ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.RegisterAssemblyTypes
(
typeof(IoCContainer).Assembly // Current Assembly.
)
.AsSelf()
.AsImplementedInterfaces();
containerBuilder.RegisterInstance(configuration).SingleInstance();
return new IoCContainer(containerBuilder, configuration);
}
public ObjectT Resolve<ObjectT>(params IoCNamedParameter[] parameters)
where ObjectT : class
{
if (container == null)
throw new ArgumentNullException($"The class {GetType().Name} is not well initialized.");
ObjectT resolvedObject;
if (parameters != null)
resolvedObject = container.Resolve<ObjectT>(ConvertIoCNamedParametersToAutfac(parameters));
else
resolvedObject = container.Resolve<ObjectT>();
return resolvedObject;
}
public object Resolve(Type objectType, params IoCNamedParameter[] parameters)
{
if (container == null)
throw new ArgumentNullException($"The class {GetType().Name} is not well initialized.");
object resolvedObject;
if (parameters != null)
resolvedObject = container.Resolve(objectType, ConvertIoCNamedParametersToAutfac(parameters));
else
resolvedObject = container.Resolve(objectType);
return resolvedObject;
}
private IEnumerable<NamedParameter> ConvertIoCNamedParametersToAutfac(IEnumerable<IoCNamedParameter> parameters)
{
foreach (var parameter in parameters)
{
if (parameter != null)
yield return new NamedParameter(parameter.Name, parameter.Value);
}
}
}
To access Microsoft .NET configuration framework, you need to reference some Microsoft.Extensions.Configuration.* packages.
In the sample below, the following packages are referenced:
- Microsoft.Extensions.Configuration.Binder to easily get typed values,
- Microsoft.Extensions.Configuration.Json to load configuration from a json file.
The container may be initialized like this:
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
.Build();
IIoCContainerWithConfiguration container = IoCContainer.BuildContainer();
And then you can resolve your objects in the code:
MyObject myObject = container.Resolve<MyObject>();
You can also access to the configuration like this:
string myStringParameter = container.ConfigurationRoot[nameof(myStringParameter)];
long myLongParameter = container.ConfigurationRoot.GetValue<int>(nameof(myLongParameter));
Use Polly to manage HttpClient
Polly is a library used to better manage the HttpClient
instances.
The following packages are needed:
- Autofac.Extensions.DependencyInjection,
- Microsoft.Extensions.Http,
- Microsoft.Extensions.Http.Polly,
- Polly.
First, a base class is needed to reference the objects to map:
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Puffix.IoC;
using Puffix.IoC.Configuration;
namespace YourAppName;
public class IoCContainer : IIoCContainerWithConfiguration
{
private readonly IContainer? container;
public IConfigurationRoot ConfigurationRoot { get; }
public IoCContainer(ContainerBuilder containerBuilder, IConfigurationRoot configuration)
{
// Self-register the container.
containerBuilder.Register(_ => this).As<IIoCContainerWithConfiguration>().SingleInstance();
containerBuilder.Register(_ => this).As<IIoCContainer>().SingleInstance();
container = containerBuilder.Build();
ConfigurationRoot = configuration;
}
public static IIoCContainerWithConfiguration BuildContainer(IConfigurationRoot configuration)
{
// Register HttpClientMessageFactory
ServiceCollection services = new ServiceCollection();
services.AddHttpClient();
AutofacServiceProviderFactory providerFactory = new AutofacServiceProviderFactory();
ContainerBuilder containerBuilder = providerFactory.CreateBuilder(services);
containerBuilder.RegisterAssemblyTypes
(
typeof(IoCContainer).Assembly // Current Assembly.
)
.AsSelf()
.AsImplementedInterfaces();
containerBuilder.RegisterInstance(configuration).SingleInstance();
return new IoCContainer(containerBuilder, configuration);
}
public ObjectT Resolve<ObjectT>(params IoCNamedParameter[] parameters)
where ObjectT : class
{
if (container == null)
throw new ArgumentNullException($"The class {GetType().Name} is not well initialized.");
ObjectT resolvedObject;
if (parameters != null)
resolvedObject = container.Resolve<ObjectT>(ConvertIoCNamedParametersToAutfac(parameters));
else
resolvedObject = container.Resolve<ObjectT>();
return resolvedObject;
}
public object Resolve(Type objectType, params IoCNamedParameter[] parameters)
{
if (container == null)
throw new ArgumentNullException($"The class {GetType().Name} is not well initialized.");
object resolvedObject;
if (parameters != null)
resolvedObject = container.Resolve(objectType, ConvertIoCNamedParametersToAutfac(parameters));
else
resolvedObject = container.Resolve(objectType);
return resolvedObject;
}
private IEnumerable<NamedParameter> ConvertIoCNamedParametersToAutfac(IEnumerable<IoCNamedParameter> parameters)
{
foreach (var parameter in parameters)
{
if (parameter != null)
yield return new NamedParameter(parameter.Name, parameter.Value);
}
}
}
The container may be initialized like this:
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
.Build();
IIoCContainerWithConfiguration container = IoCContainer.BuildContainer();
And then you can resolve your objects in the code:
MyObject myObject = container.Resolve<MyObject>();
You can also access to the configuration like this (refer to the paragraph below for the required packages):
string myStringParameter = container.ConfigurationRoot[nameof(myStringParameter)];
long myLongParameter = container.ConfigurationRoot.GetValue<int>(nameof(myLongParameter));
Finally, here is a sample class where an HttpClient
is needed:
using System.Text.Json;
namespace Puffix.Ovh.Api.Infra;
public MyClassWithHttpClient
{
private readonly IHttpClientFactory httpClientFactory;
public MyClassWithHttpClient(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
public Task<string> CallHttpAsync(Uri targetUri)
{
using HttpClient httpClient = httpClientFactory.CreateClient();
await AddHeadersAsync(httpClient, queryInformation);
using HttpResponseMessage response = await httpClient.GetAsync(targetUri);
if (!response.IsSuccessStatusCode)
{
string errorContent = await response.Content.ReadAsStringAsync();
throw new Exception($"Error {response.StatusCode}: {response.ReasonPhrase} >> {errorContent}");
}
return await response.Content.ReadAsStringAsync();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Puffix.IoC (>= 3.3.0)
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Puffix.IoC (>= 3.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Extension for including Microsoft.Extensions.Configuration in the Puffix IoC library.