将dbcontext注入owin中间件

时间:2017-01-16 21:27:20

标签: c# entity-framework asp.net-web-api owin autofac

我实施了OAuthAuthorizationServerProvider。我将IOAuthService(我的身份验证服务)注入OAuthAuthorizationServerProvider的实现。 IOAuthService的实现通过contructor的参数给出了DbContext。因此,OAuthAuthorizationServerProvider将DbContext作为单例提供。

如何根据每个网络请求范围使用DbContext向我的实施OAuthAuthorizationServerProvider注入Autofac

如果我尝试使用以下代码注册dbcontext

builder.RegisterType<OAuthProviderContext>().As<DbContext>().InstancePerWebRequest();

然后我会收到一个错误:新范围无法与HttpRequest相关联。

public class ADAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        private readonly IAppSettings _appSettingsProvider;
        private readonly IOAuthServiceProvider _oauthServiceProvider;

    public ADAuthorizationServerProvider(IAppSettings appSettingsProvider, 
        IOAuthServiceProvider oauthServiceProvider)
    {
        _appSettingsProvider = appSettingsProvider;
        _oauthServiceProvider = oauthServiceProvider;
    }

    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
       //code
    }

public class OAuthServiceProvider : IOAuthServiceProvider
    {
        private readonly IRefreshTokenRepository _refreshTokenRepository;
        private readonly IClientRepository _clientRepository;
        private readonly IUserRepository _userRepository;
        private readonly IUnitOfWork _unitOfWork; 



    public OAuthServiceProvider(IRefreshTokenRepository refreshTokenRepository,
        IClientRepository clientRepository,
        IUserRepository userRepository,
        IUnitOfWork unitOfWork)
    {
        _refreshTokenRepository = refreshTokenRepository;
        _clientRepository = clientRepository;
        _userRepository = userRepository;
        _unitOfWork = unitOfWork;            
    }

    public async Task AddRefreshToken(RefreshToken token)
    {
      //code...            
    }

    public async Task DeleteRefreshToken(string tokenId)
    {
       //code...
    }

    public async Task<Client> FindClient(string clientName)
    {
     //code...
    }

    public async Task<RefreshToken> FindRefreshToken(string tokenId)
    {
      //code...
    }

    public async Task<User> FindUser(string username)
    {
    //code...
    }
}

public class ClientRepository : BaseReadonlyRepository<Guid, Client>,       IClientRepository
{
   public ClientRepository(DbContext dbContext) : base(dbContext)
    {
    }

    public async Task<Client> GetByName(string name)
    {
       //code...
    }
}

public class RefreshTokenRepository : BaseRepository<string, RefreshToken>, IRefreshTokenRepository
{
    public RefreshTokenRepository(DbContext dbContext) : base(dbContext)
    {
    }
}

public class UserRepository : BaseReadonlyRepository<Guid, User>, IUserRepository
{
    public UserRepository(DbContext dbContext) : base(dbContext)
    {
    }

    public async Task<User> GetByName(string username)
    {
       //code...
    }
}

builder.RegisterType<OAuthProvider>().As<DbContext> ().InstancePerLifetimeScope() //I give DbContext as singleton here.

`

1 个答案:

答案 0 :(得分:0)

您可以使用Autofac提供的功能 wrap OAuthAuthroizationServerMiddleware。这样,OAuth中间件及其所有依赖项将由Autofac为每个请求实例化,因此不涉及单例。

请仔细阅读relevant documentation以及我在this GitHub repository汇总的示例。