2

カスタムActionFilterAttributeを除いて、インジェクションは私のソリューションで正しく機能します。
以下に例を示します。次の ActionFilterAttribute への注入を使用して、プロパティ UserModel を設定したいと思います。

public class UserFilterAttribute : ActionFilterAttribute
{
    public IUserModel UserModel { get; set; }

    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        // here is my problem : this.UserModel is always null

        base.OnActionExecuting(actionContext);
    }
}

Global.asax.cs :

private void ConfigureDependencies()
{
    Guid userId = new Guid();

    // Register models
    IContainer container = new Container(cfg =>
    {
        cfg.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
        });

        cfg.For<IFilterProvider>().Use<StructureMapFilterProvider>();
        cfg.For<IUserModel>().Use<UserModel>().Ctor<Guid>().Is(userId);
        cfg.SetAllProperties(x => { x.OfType<IUserModel>(); });

    });

    ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
    DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
}

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    this.ConfigureDependencies();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}

FilterConfig.cs :

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new UserFilterAttribute());
    }
}
4

1 に答える 1

0

スキャナーに追加IControllerしてみてください

cfg.Scan(scan =>
{
    scan.TheCallingAssembly();
    scan.WithDefaultConventions();
    scan.AddAllTypesOf<IController>();
});
于 2013-09-17T09:08:45.367 に答える