Registering DbContext in ASP.NET MVC Application as InstancePerRequest. (IoC Autofac)

builder.RegisterType<ADbContext>().As<IADbContext>().InstancePerRequest();

Using inside BService

public class BService : IBService
{
    readonly IADbContext _dbContext;
    public BService(IADbContext dbContext)
    {
        _dbContext = dbContext;
    }
}

Trying to register IBService as Singleton.

builder.RegisterType<BService>().As<IBService>().SingleInstance();

Obviously, this gives me an error

No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

Simplest solution is to register IBService as InstancePerRequest, but there is no reason having PerRequest IBService rather than error message mentioned above.

How can i use PerRequest DbContext inside Singleton service ?

Related posts

Recent Viewed