问题描述
我想注入的动作过滤器是这样开始的
The action filter I want to inject into starts like this
public class UserAuthorisation : AuthorizeAttribute
{
public IWcfClientProxy<IAppFrameworkServiceChannel>
FrameworkServiceProxy { get; set; }
我已经像这样设置了我的容器:
I have setup my container like this:
container.Register<IWcfClientProxy<IAppFrameworkServiceChannel>>(
()=> new WcfClientProxy<IAppFrameworkServiceChannel>());
container.RegisterInitializer<UserAuthorisation>(handler =>
{
handler.FrameworkServiceProxy = container
.GetInstance<IWcfClientProxy<IAppFrameworkServiceChannel>>();
});
当我运行它时,FrameworkServiceProxy
属性为空.
When I run this the FrameworkServiceProxy
property is null.
我读过这篇文章:简单注射器:注入一个基类中的属性并遵循答案.我还阅读了此页面中的示例 Simple Injector Documentation.
I have read this post: Simple Injector: Injecting a property in a base class and followed the answer. I have also read example in this page Simple Injector Documentation.
我没有注入基类,也许这就是问题所在?
I am not injecting into a base class and maybe that is the issue?
我正在添加更多信息,因为我认为它应该与史蒂文斯回答中所说的相符.
I am adding more information as I think it should be working from what has been said in Stevens answer.
我正在使用 MVC 3 的 NuGet 包.这会向应用程序添加以下内容:
I am using the NuGet package for MVC 3. This adds the following to the application:
public static class SimpleInjectorInitializer
{
/// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary>
public static void Initialize()
{
var container = new Container();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container container)
{
container.Register<IWcfClientProxy<IAppFrameworkServiceChannel>>(() => new WcfClientProxy<IAppFrameworkServiceChannel>());
container.RegisterInitializer<UserAuthorisation>(handler =>
{
handler.FrameworkServiceProxy = container.GetInstance<IWcfClientProxy<IAppFrameworkServiceChannel>>();
});
}
这包括 container.RegisterMvcAttributeFilterProvider();
,据我所知,它应该注册一个过滤器提供程序,并且应该意味着过滤器是通过容器创建的(这种理解可能是错误的)然后是属性自动连线.
This includes the container.RegisterMvcAttributeFilterProvider();
that as I now understand it should register a filter provider and should mean that filters are created through the container (this understanding might be wrong) and then properties are automatically wired-up.
我的过滤器在 Global.asax.cs 中注册如下:
My filter is registered in the Global.asax.cs like so:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new UserAuthorisation());
}
在我看来,过滤器不是由容器创建的,所以我认为我需要做其他事情来实现这一点?
It seems to me that the filter is not being created by the container so I think I need to do something else to get that to happen ?
推荐答案
您正在UserAuthorisation
属性上注册初始化程序.但是,初始化器仅在容器本身创建类型时由容器使用.由于属性是由 CLR 创建的,因此初始化程序不会关闭.
You are registering an initializer on your UserAuthorisation
attribute. Initializers however, are only used by the container when a type is created by the container itself. Since attributes are created by the CLR, the initializer won't go off.
SimpleInjector.Integration.Web.Mvc.dll(这个 NuGet 包)包含一个 RegisterMvcAttributeFilterProvider
扩展方法.这将注册一个 AttributeFilterProvider
,它将执行隐式属性注入(并调用 container.InjectProperties
方法).调用container.RegisterMvcAttributeFilterProvider()
后,会看到这个属性是自动注入的.
The SimpleInjector.Integration.Web.Mvc.dll (this NuGet package) contains a RegisterMvcAttributeFilterProvider
extension method. This will register an AttributeFilterProvider
that will do implicit property injection (and call into the container.InjectProperties
method). After calling container.RegisterMvcAttributeFilterProvider()
, you will see that this property is injected automatically.
这篇关于动作过滤器上的简单注入器属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!