简体   繁体   English

自定义AuthorizeAttribute Ninject属性注入不起作用(注入属性具有需要注入的子依赖服务)

[英]Custom AuthorizeAttribute Ninject Property Injection doesn't work (injected property have sub dependant services which need to be injected)

I think the specifics of my question are very much different than the other similar questions which I have red. 我认为我的问题的细节与其他类似的问题非常不同。

I know that when I have custom AuthorizeAttribute I can't inject dependencies with the constructor. 我知道当我有自定义AuthorizeAttribute时,我无法使用构造函数注入依赖项。 This is because the constructor will take the parameters - in my case the permission strings. 这是因为构造函数将获取参数 - 在我的例子中是权限字符串。

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class UserAllCSPermissionBasedAuthFilter : AuthorizeAttribute
    {

I am depending on authorization service, that's why I am injected that using property injection. 我依赖于授权服务,这就是我注入使用属性注入的原因。

[Inject]
  public IAuthorizationService _authorizationService { get; set; }

The problem is that this service depends on another service - userservice which talks directly with repository and dbcontext. 问题是这个服务依赖于另一个服务 - 直接与repository和dbcontext对话的用户服务。 I have specify for my db context to live in request scope. 我已指定我的db上下文生活在请求范围内。 This is cousing an exception - "The operation cannot be completed because the DbContext has been disposed." 这是一个例外 - “由于已经处理了DbContext,因此无法完成操作。” When I looked at the code this is happening when the autorization service calls userservice which asks the dbcontext for some data. 当我查看代码时,当自动化服务调用usersservice时会发生这种情况,这会向dbcontext询问某些数据。 How should I avoid this happening? 我该如何避免这种情况发生?

  public class AuthorizationService : IAuthorizationService
    {
        private readonly ICommonRepository _commonRepository;
        private readonly IRepositoryBase<UsersInRolesEntity> _repositoryUsersInRoles;
        private readonly IRepositoryBase<UserCustomerRolesEntity> _repositoryCurstomerRoleEntities;
        private readonly ISqlCustomersRepository _sqlCustomerRepository;
        private readonly IRoleService _roleService;
        private readonly IUserService _userService;
        private readonly IPermissionService _permissionService;

        public AuthorizationService(
            IRepositoryBase<UsersInRolesEntity> repositoryUsersInRoles,
            IRepositoryBase<UserCustomerRolesEntity> repositoryCurstomerRoleEntities,
            ICommonRepository commonRepository,
            ISqlCustomersRepository sqlCustomerRepository,
            IRoleService roleService,
            IUserService userService,
            IPermissionService permissionService
            )
        {

Steven, thanks for the support, I was looking at the articles you suggested and the Ninject manual. 史蒂文,感谢您的支持,我正在查看您建议的文章和Ninject手册。 I totally agree with you that Property Binding is not a nice idea. 我完全同意你的说法,Property Binding并不是一个好主意。 But couldn't get why we are doing all of the things in the article ( https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98 ). 但无法理解我们为什么要做文章中的所有内容( https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98 )。 I suppose that was written before the Ninject authors added a way to configure attribute binding using their framework ( https://github.com/ninject/Ninject.Web.Mvc/wiki/Filter-configurations ). 我想这是在Ninject作者添加了一种使用框架配置属性绑定的方法之前编写的( https://github.com/ninject/Ninject.Web.Mvc/wiki/Filter-configurations )。

I had a look at my Ninject configuration. 我看了一下我的Ninject配置。 For my authorization filters I had something like this: 对于我的授权过滤器,我有这样的事情:

#region UserAllCSPermissionBasedAuthFilter

kernel.BindFilter<UserAllCSPermissionBasedAuthFilter>(FilterScope.Action, 0)
    .WhenActionMethodHas<UserAllCSPermissionBasedAuthFilter>()
    .WithConstructorArgumentFromActionAttribute<UserAllCSPermissionBasedAuthFilter>("permissionEnums", att => att.PermissionEnums);

kernel.BindFilter<UserAllCSPermissionBasedAuthFilter>(FilterScope.Controller, 0)
    .WhenActionMethodHas<UserAllCSPermissionBasedAuthFilter>()
    .WithConstructorArgumentFromControllerAttribute<UserAllCSPermissionBasedAuthFilter>("permissionEnums", att => att.PermissionEnums);

#endregion

I have a couple of these and then the light-bulb moment came :) I just saw a small mistake in my configuration. 我有几个,然后灯泡时刻来了:)我刚看到我的配置中的一个小错误。 Instead of using: 而不是使用:

kernel.BindFilter<UserAllCSPermissionBasedAuthFilter>(FilterScope.Controller, 0)
    .WhenActionMethodHas<UserAllCSPermissionBasedAuthFilter>()
    .WithConstructorArgumentFromControllerAttribute<UserAllCSPermissionBasedAuthFilter>("permissionEnums", att => att.PermissionEnums);

That should actually be: 那应该是:

kernel.BindFilter<UserAllCSPermissionBasedAuthFilter>(FilterScope.Controller, 0)
    .WhenControllerHas<UserAllCSPermissionBasedAuthFilter>()
    .WithConstructorArgumentFromControllerAttribute<UserAllCSPermissionBasedAuthFilter>("permissionEnums", att => att.PermissionEnums);

WhenActionMethodHas -> WhenControllerHas. WhenActionMethodHas - > WhenControllerHas。

That miraculously fixed everything. 奇迹般地修复了一切。 Now works perfectly and the code looks fine to me without additional coding changes. 现在工作得很完美,代码看起来很好,没有额外的编码更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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