MVC Hangfire 授权 Ninject
MVC Hangfire Authorization Ninject
我想将我的自定义服务注入到 Hangfire AuthorizationFilter 中并用它做一些事情,但我遇到了问题,我的服务始终为空。
Owin 启动 class :
public class Startup
{
public void Configuration(IAppBuilder app )
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("SomeContext")
.UseNinjectActivator(new Bootstrapper().Kernel);
app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new RestrictiveAuthorizationFilter() } });
app.UseHangfireServer();
}
}
自定义过滤器:
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
[Inject]
public IUserService _userService { get; set; }
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}
Ninject 内核:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(x => { x.From(typeof(BookService).Assembly).SelectAllClasses().EndingWith("Service").BindDefaultInterface(); });
kernel.Bind<LibraryDBContext>().ToSelf().InRequestScope();
kernel.Bind<IBackgroundJobClient>().To<BackgroundJobClient>();
}
我没有看到我的错误,你能帮我吗?
您可以将内核作为参数传递给 RestrictiveAuthorizationFilter 构造函数并请求 IUserService。
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
public IUserService _userService { get; set; }
public RestrictiveAuthorizationFilter(IKernel kernel)
{
_userService = kernel.Get<IUserService>()
}
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}
我想将我的自定义服务注入到 Hangfire AuthorizationFilter 中并用它做一些事情,但我遇到了问题,我的服务始终为空。
Owin 启动 class :
public class Startup
{
public void Configuration(IAppBuilder app )
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("SomeContext")
.UseNinjectActivator(new Bootstrapper().Kernel);
app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new RestrictiveAuthorizationFilter() } });
app.UseHangfireServer();
}
}
自定义过滤器:
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
[Inject]
public IUserService _userService { get; set; }
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}
Ninject 内核:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(x => { x.From(typeof(BookService).Assembly).SelectAllClasses().EndingWith("Service").BindDefaultInterface(); });
kernel.Bind<LibraryDBContext>().ToSelf().InRequestScope();
kernel.Bind<IBackgroundJobClient>().To<BackgroundJobClient>();
}
我没有看到我的错误,你能帮我吗?
您可以将内核作为参数传递给 RestrictiveAuthorizationFilter 构造函数并请求 IUserService。
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
public IUserService _userService { get; set; }
public RestrictiveAuthorizationFilter(IKernel kernel)
{
_userService = kernel.Get<IUserService>()
}
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}