我在操作过滤器(属性)中获取 IBus 实例时遇到问题。
从在 MVC Filter 中设置 IBus 属性,我知道 DI 和动作过滤器不能很好地发挥作用,我使用接受的答案让它们发挥得很好。问题是让 DI 和动作过滤器发挥出色会破坏 NServiceBus。
同样的问题,John 建议查看音像店解决方案,看看如何完成它。这个答案有两个问题:
- 它完全忽略了我正在使用 StructureMap 的事实。
- 更重要的是,使用该示例,当我添加自己的派生授权过滤器(见下文)时,IBus 实例仍未被填充。
这是我的猜测,但我不认为 NServiceBus 具有的烘焙 DI 容器可以将 DI 处理为动作过滤器。
//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;
}
}
}
当我按照Setting IBus Property in MVC Filter 中指定的步骤操作时,我收到此错误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