I have class:
public class FunctionExtender : IFunctionsExtender
{
private static IOptions<Settings> _settings;
private static ILogger<FunctionExtender> _logger;
public FunctionExtender(IOptions<Settings> settings, ILogger<FunctionExtender> logger)
{
_settings = settings;
_logger = logger;
}
}
where IOptions
is a Microsoft.Extensions.Options
type and ILogger
is a Microsoft.Extensions.Logging
type. How can I register this type in Autofac container with it parameters? I tried something like that:
var container = new ContainerBuilder();
container.Populate(services);
container.RegisterType<FunctionExtender>()
.WithParameter(new TypedParameter(typeof(IOptions<Settings>), ???));
but I don't know what to pass as second argument.
You just need to register FunctionExtender
as IFunctionsExtender
. Autofac will resolve the appropriate dependencies for you. It is one of the jobs of an inversion of control (IoC) container - register, resolve, release and dispose.
As of Autofac.Extensions.DependencyInjection Version 4.2.0
, here is all you need for ASP.Net Core 2
.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc();
...
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<FunctionExtender>().As<IFunctionsExtender>();
}
}
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddAutofac()) <======
.UseStartup<Startup>()
.Build();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.