简单的喷油器属性注射行为过滤器 [英] Simple Injector property injection on action filter

查看:159
本文介绍了简单的喷油器属性注射行为过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该行为过滤我希望注入开始像这样

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 属性为null。

When I run this the FrameworkServiceProxy property is null.

我已经阅读这篇文章:<一个href="http://stackoverflow.com/questions/6227074/simple-injector-injecting-a-property-in-a-base-class">Simple注射:注射在基类属性和遵循的答案。我也看过例如在本页面<一个href="http://simpleinjector.$c$cplex.com/wikipage?title=Using%20the%20Simple%20Injector&referringTitle=Documentation"相对=nofollow>简单的注射器文档。

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.

我使用的NuGet包MVC 3,这增加了以下应用程序:

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.

这篇关于简单的喷油器属性注射行为过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆