ASP.NET MVC4 NServiceBus属性/过滤器StructureMap [英] ASP.NET MVC4 NServiceBus Attribute/Filter StructureMap

查看:103
本文介绍了ASP.NET MVC4 NServiceBus属性/过滤器StructureMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将IBus实例放入动作过滤器(属性)时遇到问题。

I'm having issues getting an instance of IBus in an action filter (attribute).

来自在MVC过滤器中设置IBus属性,我知道DI和动作过滤器的效果不佳,并且使用公认的方法,它们也可以正常发挥作用回答。问题在于,让DI和动作过滤器发挥良好的效果会中断NServiceBus。

From Setting IBus Property in MVC Filter, I know that DI and action filters don't play nice, and I got them play nice using the accepted answer. The problem is that getting DI and action filters to play nice breaks NServiceBus.

同一问题由John John提出,建议您查看视频商店解决方案以了解如何获取完成了。这个答案有两个问题:

That same question had a suggestion by John to look at the video store solution to see how to get it done. There are two problems with that answer:


  1. 它完全忽略了我正在使用StructureMap的事实。

  2. 更重要的是,使用该示例,当我添加了自己的派生授权过滤器时(请参见下文),IBus实例仍然没有被填充。

这是我的猜测,但我认为NServiceBus拥有的DI容器中的烘焙容器无法将DI处理为动作过滤器。

This is a guess on my part, but I don't think the baked in DI container that NServiceBus has can handle DI into an action filter, either.

//NOTE:
//I added a Filters folder to the project, and stuck this class in there
//This filter will disable authentication when debugging
//Add the attribute to the home controller, and
//If you put a break point at the beginning, IBus doesn't get filled

using NServiceBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace VideoStore.ECommerce.Filters
{
    public class AMSAuthorizeAttribute : AuthorizeAttribute
    {
        public IBus Bus { get; set; }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool accessGranted = false;

            accessGranted = base.AuthorizeCore(httpContext);

            //if(isAdmin)
            //  accessGranted = true;

#if DEBUG
            accessGranted = true;
#endif

            return accessGranted;
        }
    }
}

当我按照指定的步骤进行操作时在在MVC过滤器中设置IBus属性中,出现此错误 StructureMap异常代码:202
没有为PluginFamily NServiceBus.Unicast.Subscriptions.ISubscriptionStorage,NServiceBus.Core,Version = 3.3.0.0,Culture = neutral,PublicKeyToken = 9fc386479f8a226c

When I follow the steps specified in Setting IBus Property in MVC Filter, I get this error StructureMap Exception Code: 202 No Default Instance defined for PluginFamily NServiceBus.Unicast.Subscriptions.ISubscriptionStorage, NServiceBus.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c

推荐答案

由于过滤器属性在依赖项注入中不能很好地发挥作用,因此您可能会发现在过滤器中执行此操作会更容易:

Because filter attributes don't play nice with dependency injection, you may find it's easier just to do this within your filter:

private readonly IBus bus = Configure.Instance.Builder.Build<IBus>();

这篇关于ASP.NET MVC4 NServiceBus属性/过滤器StructureMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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