首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使SNAP AOP框架能够与Ninject MVC 3协同工作

使SNAP AOP框架能够与Ninject MVC 3协同工作
EN

Stack Overflow用户
提问于 2011-10-20 11:47:43
回答 1查看 509关注 0票数 0

我想在我的MVC3应用程序中尝试使用Snap进行SOA风格的日志记录。我使用的是IoC版的Ninject,所以我通过Nuget安装了Ninject.MVC和Snap.Ninject,看看GitHub版Snap.Ninject版的示例代码。我还阅读了Getting SNAP(AOP), NInject and ASP.Net MVC 3 working together,它似乎正在做我想要的事情。

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

代码语言:javascript
复制
    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 { }

和控制器

代码语言:javascript
复制
 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属性

代码语言:javascript
复制
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.CreatePseudoProxy(IMasterProxy (类型代理,对象instanceToWrap,IInterceptor[]拦截器) +29 Snap.AspectUtility.CreatePseudoProxy(IMasterProxy proxy,类型IInterceptor[],对象instanceToWrap) +184 Snap.Ninject.AspectProxyActivationStrategy.Activate(IContext上下文,上下文引用) +376 Ninject.Activation.<>c__DisplayClass2.b__0(IActivationStrategy s)在c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Pipeline.cs:58 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable1 series, Action1动作)中)在c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:23 Ninject.Activation.Pipeline.Activate(IContext上下文,c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:375 System.Linq.<>c__DisplayClass123.<CombineSelectors>b__11(TSource x) +32 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +151 System.Linq中c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs:182 Ninject.KernelBase.b__7(IContext context)中的InstanceReference Ninject.Activation.Context.Resolve()中的Ninject.KernelBase.b__7引用)。Enumerable.SingleOrDefault(IEnumerable1 source) +4178557 Ninject.Planning.Targets.Target1.GetValue(Type服务,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()中的IContext parent) +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 source) +4178557 Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType)在c:\Projects\Ninject\Maintenance2.2\ninject.web.mvc中\mvc3\src\Ninject.Web.Mvc\NinjectDependencyResolver.cs:56 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,controllerType标牌) +51

提前谢谢。我想要它失败的工作演示,然后让我知道。

EN

回答 1

Stack Overflow用户

发布于 2011-10-21 06:01:41

感谢Tyler Brinks发现了我的打字错误!

将命名空间引用更新为

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

一切都很好。

希望有人觉得这很有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7830921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档