How to use basic ASP.NET application parameters with a simple injector

I am new to ASP.NET core and need some direction. I am trying to figure out a simple scenario using the application settings in ASP.NET Core, as well as using Simple Injector.

First I set Strongly-Type configuration options as described here by Rick Stral. This works great.

  public class MySettings
  {
    public string ApplicationName { get; set; }
  }

appsetting.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "MySettings": {
    "ApplicationName": "Test Service"
  }
}

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
      // Add framework services.
      services.AddMvc();

      // Add Options
      services.AddOptions();

      // Add our Config object so it can be injected
      services.Configure<MySettings>(Configuration.GetSection("MySettings"));
      services.AddSingleton(GetTestBroker(container));

      //Simple Injector       
      //services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(container));
      //services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(container));


  }

Our other projects used Simple Injector for DI. After adding the Simple Injector package and configuring it according to the instructions, I see that my IOptions configuration is interrupted.

What I would like to know is what would be best implemented to implement the configuration in ASP.NET Core and together with other DI libraries like Simple Injector?

+4
2

ASP.NET Core :

ASP.NET Core , IOption<T>. IOption<T> . Singleton. , , .

IOptions<T> . , , . , , . IOptions<T> , . .

IOptions<T> . , , IOptions<T>.Value . , - IOptions<T>.Value. , , . . , .

, ( services.Configure<T>) ( Configuration.GetSection(name)), , ! , .

, , IOption . IOptions<T> , .

, , , :

MyMailSettings mailSettings =
    config.GetSection("Root:SectionName").Get<MyMailSettings>();

// Verify mailSettings here (if required)

container.Register<IMessageSender>(() => new MailMessageSender(mailSettings));
+2

ExistAll.SimpleConfig. SimpleConfig - , / DI.

, -

0

Source: https://habr.com/ru/post/1668673/


All Articles