【问题标题】:Getting SNAP AOP Framework working with Ninject MVC 3让 SNAP AOP 框架与 Ninject MVC 3 一起工作
【发布时间】:2011-12-11 11:15:05
【问题描述】:

我想在我的 MVC3 应用程序中使用 Snap 尝试 SOA 风格的日志记录。我将 Ninject 用于 IoC,因此通过 Nuget 安装了 Ninject.MVC 和 Snap.Ninject 都查看了 GitHub 中 Snap.Ninject 的示例代码。我也读过Getting SNAP(AOP), NInject and ASP.Net MVC 3 working together 这似乎正是我想要的。

我已经相应地更新了我的 NinjctMVC3.cs,但是当我将拦截器属性添加到我的方法时,我从 Snap AspectUtility 收到一个对象引用错误。这是我的 NinjectMVC3.cs

    public static class NinjectMVC3 {
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
        DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop() {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel() {
        //var kernel = new StandardKernel();
        NinjectAopConfiguration.NinjectAopConfigure();
        var kernel = NinjectAopConfiguration._container.Kernel;
        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel) {

        kernel.Bind<ILogger>().To<NLogger>()
            .WithConstructorArgument("currentClassName", x => x.Request.ParentContext.Request.Service.FullName); ;
        kernel.Bind<ISomeDataFactory>().To<SomeDataFactory>();
    }
}

public static class NinjectAopConfiguration {
    public readonly static NinjectAspectContainer _container;

    static NinjectAopConfiguration() {
        _container = new NinjectAspectContainer();
    }
    public static void NinjectAopConfigure() {
        SnapConfiguration.For(_container).Configure(c => {
            c.IncludeNamespace("TestAopLogging.Model.*");
            c.Bind<MyMethodInterceptor>().To<MyInterceptorAttribute>();
        });
    }
}

public class MyMethodInterceptor : MethodInterceptor {
    public override void InterceptMethod(Castle.DynamicProxy.IInvocation invocation, MethodBase method, System.Attribute attribute) {
        var logger = new NLogger(method.DeclaringType.ToString());
        logger.LogInfo("Hello AOP Logger. Your method (" + method.Name + ") has been intercepted");
        invocation.Proceed();
    }

    public override void BeforeInvocation() {
        var logger = new NLogger("How do I work out what class I'm in?");
        base.BeforeInvocation();
    }

    public override void AfterInvocation() {
        var logger = new NLogger("How do I work out what class I'm in?");
        logger.LogInfo("Hello AOP Logger. After Invocation");
        base.AfterInvocation();
    }
}

public class MyInterceptorAttribute : MethodInterceptAttribute { }

还有控制器

 public class HomeController : Controller
{
    private ILogger _logger;
    private ISomeDataFactory _someDataFactory;

    public HomeController(ILogger logger, ISomeDataFactory someDataFactory) {
        _logger = logger;
        _someDataFactory = someDataFactory;
    }


    public ActionResult Index()
    {
        _logger.LogInfo("I've hit the index action");
        _someDataFactory.GetStuffAndLogTheOldWay();
        _someDataFactory.GetStuffAndLogUsingAOP();
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

以及带有intercept属性的方法的工厂类

public interface ISomeDataFactory {
    string GetStuffAndLogTheOldWay();
    string GetStuffAndLogUsingAOP();
}

public class SomeDataFactory : ISomeDataFactory {
    private ILogger _logger;

    public SomeDataFactory(ILogger logger) {
        _logger = logger;
    }

    public string GetStuffAndLogTheOldWay() {
        _logger.LogInfo(MethodBase.GetCurrentMethod().Name + " was called");
        return "I called GetStuffAndLogTheOldWay";
    }

    [MyInterceptor] // If I comment this out, then all is good
    public string GetStuffAndLogUsingAOP() {
        return "I called GetStuffAndLogUsingAOP";
    }
}

这会导致以下异常

[NullReferenceException:对象引用未设置为对象的实例。] Snap.AspectUtility.CreateProxy(类型 interfaceType, Object instanceToWrap, IInterceptor[] 拦截器) +29 Snap.AspectUtility.CreatePseudoProxy(IMasterProxy proxy, Type interfaceType, Object instanceToWrap) +184 Snap.Ninject.AspectProxyActivationStrategy.Activate(IContext 上下文,InstanceReference 参考)+376 C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Pipeline.cs:58 中的 Ninject.Activation.c__DisplayClass2.b__0(IActivationStrategy s) C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:23 中的 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable1 series, Action1 操作) C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Pipeline.cs:58 中的 Ninject.Activation.Pipeline.Activate(IContext 上下文,InstanceReference 参考) C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs:182 中的 Ninject.Activation.Context.Resolve() C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:375 中的 Ninject.KernelBase.b__7(IContext 上下文) System.Linq.c__DisplayClass123.<CombineSelectors>b__11(TSource x) +32 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +151 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 source) +4178557 Ninject.Planning.Targets.Target1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Planning\Targets\Target.cs:179 Ninject.Planning.Targets.Target1.ResolveWithin(IContext parent) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Planning\Targets\Target.cs:147 Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:97 Ninject.Activation.Providers.<>c__DisplayClass2.<Create>b__1(ITarget target) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:81 System.Linq.WhereSelectArrayIterator2.MoveNext() +85 System.Linq.Buffer1..ctor(IEnumerable1 源)+325 System.Linq.Enumerable.ToArray(IEnumerable1 source) +78 Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:81 Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs:157 Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:375 System.Linq.<>c__DisplayClass123.b__11(TSource x) +32 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +151 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 来源)+4178557 C:\Projects\Ninject\Maintenance2.2\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectDependencyResolver.cs:56 中的 Ninject.Web.Mvc.NinjectDependencyResolver.GetService(键入 serviceType) System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +51

提前致谢。我想要一个失败的工作演示,然后告诉我。

【问题讨论】:

    标签: asp.net-mvc-3 aop ninject.web.mvc


    【解决方案1】:

    感谢 Tyler Brinks 发现我的错字!

    将命名空间引用更新为

    c.IncludeNamespace("TestAopLogging.Models.*");

    一切都很好。

    希望有人觉得这很有用。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2014-06-15
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多