简体   繁体   中英

Autofac: How to pass constructor parameter for Register an Instance as a component

There is a service class in my project and I have injected my context class (ClientIntagrationContext) to the service class's constructor to pull data from DB at run time as below.

 public AppLogsDataWriterService(AppLogsDbConnectionFactory dbFactory, IClientIntagrationContext clientIntagrationContext)  
    {
        this.dbFactory = dbFactory;
        this.clientIntagrationContext = clientIntagrationContext;

        LogLevelThresholdValue = clientIntagrationContext.getApplogSettingsValue().ToString();
    }   

And while resolving the service class (AppLogsDataWriterService) in Startup.cs file, I have to pass the ClientIntagrationContext class instance as a parameter to the "AppLogsDataWriterService" constructor and I tried with creating a default constructor but it's throwing the NullReference Exception for DB Context. So how to pass the parameter while registering for my injecting class?

registering the class in Startup.cs,

 ...
 //IClientIntagrationContext clientIntegrationContext = new ClientIntagrationContext();
        IAppLogsDataWriterService logWriterService = new AppLogsDataWriterService(logDbfactory, _________________?);
        builder.RegisterInstance(logDbfactory);
        builder.RegisterInstance(logWriterService);


        builder.RegisterType<PlatformUserIdProvider>().As<IUserIdProvider>();
        builder.RegisterHubs(typeof(UserNotificationsHub).Assembly);
        var logLevelThreshold = RuntimeConfig.LogLevelThreshold;
        var infoLevel = string.IsNullOrEmpty(logLevelThreshold) ? Level.Info : Level.All.Parse(logLevelThreshold);
        var logWriter = RuntimeConfig.GetConfigValue<bool>("In8.Common.Logging.UseDirectWriter") ? (ILogWriter)new SqlWriter(logWriterService) : new WebWriter();
        builder.RegisterAndSetupLog4NetAppLogsForWebWriter("Core", logWriter: logWriter, threshold: infoLevel); 

You can use ContainerBuilder.Register to resolve the dependency during service instantiation.

For example:

builder.RegisterType<ClientIntegrationContext>().As<IClientIntegrationContext>();
builder.Register(x => new AppLogsDataWriterService(logDbfactory, x.Resolve<IClientIntegrationContext>()).AsSelf();

We can create a new instance for the parameter class instantly and pass the instance as a parameter at runtime.

 Ex:  var parameter= new ClientIntegrationsLogService(anyParameterInstance);

So I modified my code as below, its working fine.

IExtClientIntegrationsContext iext = new ExtClientIntegrationsContext(TenantInfo.GetTenantInfoForRequest());
        IClientIntegrationsLogService clientIntegrationContext = new ClientIntegrationsLogService(iext);
        IAppLogsDataWriterService logWriterService = new AppLogsDataWriterService(logDbfactory, clientIntegrationContext, TenantInfo.GetTenantInfoForRequest());

Or else we can create a new instance for the service class in "AppLogsDataWriterService" class. For this approach no need to resolve or register our service class.

Ex:
private readonly ClientIntegrationsLogService clientIntegrationsLogService;

Constructor()
{
  clientIntegrationsLogService = new ClientIntegrationsLogService(new 
  ExtClientIntegrationsContext(tenant));
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM