Skip to main content
added 150 characters in body
Source Link
Nkosi
  • 249.4k
  • 38
  • 472
  • 506

If using IOptions<T> then update Service constructor to useexplicitly depend on IOptions<MyOptions> so that it can be injected into the class.

public class Service: IService {
     
    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configurationConfiguration can be simplified to

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming appsettings.json contains

{
   "MyOptions": {
       "arg1": value1,
       "arg2": value2
    }
}

If unable to change service class constructor then resolve option in object factory delegate

services.AddTransient<IService, Service>(serviceProvider => {
    var options = serviceProvider.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});

Reference Options pattern in ASP.NET Core

If using IOptions<T> then update Service constructor to use IOptions<MyOptions>

public class Service: IService {
 
    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configuration can be simplified

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming appsettings.json contains

{
   "MyOptions": {
       "arg1": value1,
       "arg2": value2
    }
}

If unable to change service class constructor then resolve option in object factory delegate

services.AddTransient<IService, Service>(serviceProvider => {
    var options = serviceProvider.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});

If using IOptions<T> then update Service constructor to explicitly depend on IOptions<MyOptions> so that it can be injected into the class.

public class Service: IService {    
    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

Configuration can be simplified to

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming appsettings.json contains

{
   "MyOptions": {
       "arg1": value1,
       "arg2": value2
    }
}

If unable to change service class constructor then resolve option in object factory delegate

services.AddTransient<IService, Service>(serviceProvider => {
    var options = serviceProvider.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});

Reference Options pattern in ASP.NET Core

added 1 character in body
Source Link
Nkosi
  • 249.4k
  • 38
  • 472
  • 506

If using IOptions<T> then update Service constructor to use IOptions<MyOptions>

public class Service: IService {

    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configuration can be simplified

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming appsettings.json contains

{
   "MyOptions": {
       "arg1": 1value1,
       "arg2": 2value2
    }
}

If unable to change service class constructor then resolve option in object factorfactory delegate

services.AddTransient<IService, Service>(xserviceProvider => {
    var options = XserviceProvider.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});

If using IOptions<T> then update Service constructor to use IOptions<MyOptions>

public class Service: IService {

    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configuration can be simplified

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming

{
   "MyOptions": {
       "arg1": 1,
       "arg2": 2
    }
}

If unable to change service class constructor then resolve option in object factor delegate

services.AddTransient<IService, Service>(x => {
    var options = X.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});

If using IOptions<T> then update Service constructor to use IOptions<MyOptions>

public class Service: IService {

    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configuration can be simplified

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming appsettings.json contains

{
   "MyOptions": {
       "arg1": value1,
       "arg2": value2
    }
}

If unable to change service class constructor then resolve option in object factory delegate

services.AddTransient<IService, Service>(serviceProvider => {
    var options = serviceProvider.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});
Source Link
Nkosi
  • 249.4k
  • 38
  • 472
  • 506

If using IOptions<T> then update Service constructor to use IOptions<MyOptions>

public class Service: IService {

    public Service(IOptions<MyOptions> options) {
        this.arg1 = options.Value.arg1;
        this.arg2 = options.Value.arg2;
    }
}

So that configuration can be simplified

services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();

Assuming

{
   "MyOptions": {
       "arg1": 1,
       "arg2": 2
    }
}

If unable to change service class constructor then resolve option in object factor delegate

services.AddTransient<IService, Service>(x => {
    var options = X.GetService<IOptions<MyOptions>>();
    return new Service(options.Value.arg1, options.Value.arg2);
});