0

In my IContainer I am trying to register my DBContext. And this approach works:

builder.Register(c =>
            {
                var opt = new DbContextOptionsBuilder<ProjectsDbContext>();
                opt.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=Portfolio_Strona;Trusted_Connection=True;MultipleActiveResultSets=true");

                return new ProjectsDbContext(opt.Options);
            }).AsSelf().InstancePerLifetimeScope();

And now I wanted to replace connection string with this:

string connString= ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
builder.Register(c =>
                {
                    var opt = new DbContextOptionsBuilder<ProjectsDbContext>();
                    opt.UseSqlServer(connString);
    
                    return new ProjectsDbContext(opt.Options);
                }).AsSelf().InstancePerLifetimeScope();

And I am getting exception:

Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating CV_Creator.Desktop.ViewModels.ProjectLoaderViewModel.'

2 inner exceptions:

DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(CV_Creator.DAL.IProjectRepository, CV_Creator.Services.IProjectCollectionDisplayService, CV_Creator.Services.IWindowManager)' on type 'ProjectLoaderViewModel'.

and

SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid. )

How can I register or resolve ConfigurationManager, that Autofac can see it?

Things I already tryed:

This

System.Configuration.ConfigurationBuilder does not contain definition for SetBasePath Microsoft.Extensions.Configuration.ConfigurationBuilder does not contain definition for SetBasePath

This

Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating CV_Creator.Desktop.ViewModels.ProjectLoaderViewModel -> CV_Creator.DAL.ProjectRepository -> λ:CV_Creator.DAL.ProjectsDbContext.' Inner exception: ComponentNotRegisteredException: The requested service 'Microsoft.Extensions.Configuration.IConfiguration' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

Is there any way to resolve ConfigurationManager with Autofac IoC container?

UPDATED New approach tried, in IoC Container:

builder.RegisterType<ProjectsDbContext>().InstancePerLifetimeScope();

and in DbContext:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(ConfigurationManager.AppSettings["connectionString"]);
        }

I receive exception:

Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating CV_Creator.Desktop.ViewModels.ProjectLoaderViewModel -> CV_Creator.DAL.ProjectRepository -> CV_Creator.DAL.ProjectsDbContext.' DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CV_Creator.DAL.ProjectsDbContext' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.EntityFrameworkCore.DbContextOptions1[CV_Creator.DAL.ProjectsDbContext] options' of constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions1[CV_Creator.DAL.ProjectsDbContext])'.

bakunet
  • 559
  • 8
  • 25

0 Answers0