Autofac: None of the constructors found on type 'DataContext' can be invoked with the available services and parameters:


Title:
First time using AutoFac – Cannot resolve IDataContext in HomeController

Question:
I’m new to AutoFac and I’m facing an issue resolving dependencies for my HomeController which depends on IDataContext. Despite registering both HomeController and IDataContext, I get the following runtime error:

None of the constructors found on type 'Cpd.iPlanSap.Entities.DataContext.DataContext' can be invoked with the available services and parameters:Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String)'.Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String, Cpd.iPlanSap.Core.Interfaces.IClaimsProvider, Boolean)'.Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String, Boolean, Cpd.iPlanSap.Core.Interfaces.IClaimsProvider, Int32, Boolean)'.See https://autofac.rtfd.io/help/no-constructors-bindable for more info.

Full Error Message:

Autofac.Core.DependencyResolutionException: None of the constructors found on type 'Cpd.iPlanSap.Entities.DataContext.DataContext' can be invoked with the available services and parameters:Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String)'.Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String, Cpd.iPlanSap.Core.Interfaces.IClaimsProvider, Boolean)'.Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String, Boolean, Cpd.iPlanSap.Core.Interfaces.IClaimsProvider, Int32, Boolean)'.See https://autofac.rtfd.io/help/no-constructors-bindable for more info.

Stack Trace:

[DependencyResolutionException: None of the constructors found on type 'Cpd.iPlanSap.Entities.DataContext.DataContext' can be invoked with the available services and parameters:Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(System.String)'....

What I’ve Tried:

I’ve tried registering the DataContext in various ways, including using a DataContextFactory:

builder.Register(c => c.Resolve<DataContextFactory>().CreateWithDependencies())    .As<IDataContext>()    .InstancePerRequest();builder.Register(c => new DataContextFactory(    iPlanConnectionString,    claimsProvider,    databaseQueryTimeout,    true).CreateWithDependencies())    .As<IDataContext>()    .InstancePerRequest();builder.Register(c => new DataContext(    iPlanConnectionString,    false,    claimsProvider,    databaseQueryTimeout,    true))    .As<IDataContext>()    .InstancePerRequest();builder.RegisterType<DataContext>()    .WithParameter(new NamedParameter("connectionString", iPlanConnectionString))    .WithParameter(new NamedParameter("migrateToLatestVersion", false))    .WithParameter(new ResolvedParameter(        (pi, ctx) => pi.ParameterType == typeof(IClaimsProvider),        (pi, ctx) => claimsProvider))    .WithParameter(new NamedParameter("commandTimeout", databaseQueryTimeout))    .WithParameter(new NamedParameter("loggingEnabled", true))    .As<IDataContext>();

I also tried:

builder.RegisterInstance(connectionString).Named<string>("connectionString");

I’m using .NET Framework 4.8 and Autofac 8.0.0.

Any help would be appreciated!

Thanks!



Source link

Post a Comment

Previous Post Next Post