我想在我的MVC3应用程序中尝试使用Snap进行SOA样式日志记录。我正在使用Ninject for IoC所以已经通过Nuget安装了Ninject.MVC和Snap.Ninject都看了GitHub for Snap.Ninject中的示例代码。我也读过Getting SNAP(AOP), NInject and ASP.Net MVC 3 working together 这似乎完全符合我的要求。
我相应地更新了我的NinjctMVC3.cs但是当我将interceptor属性添加到我的方法时,我从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();
}
}
和带有拦截属性
的方法的工厂类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代理,Type interfaceType,Object instanceToWrap)+184
Snap.Ninject.AspectProxyActivationStrategy.Activate(IContext context,InstanceReference reference)+376
Ninject.Activation。&lt;&gt; c__DisplayClass2.b__0(IActivationStrategy s)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ Activation \ Pipeline.cs:58
Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable 1 series, Action
1动作)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ Infrastructure \ Language \ ExtensionsForIEnumerableOfT.cs:23
Ninject.Activation.Pipeline.Activate(IContext context,InstanceReference reference)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ Activation \ Pipeline.cs:58
Ninject.Activation.Context.Resolve()在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ Activation \ Context.cs:182
Ninject.KernelBase.b__7(IContext context)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ KernelBase.cs:375
System.Linq。&lt;&gt; c__DisplayClass12 3.<CombineSelectors>b__11(TSource x) +32
System.Linq.WhereSelectEnumerableIterator
2.MoveNext()+151
System.Linq.Enumerable.SingleOrDefault(IEnumerable 1 source) +4178557
Ninject.Planning.Targets.Target
1.GetValue(类型服务,IContext父级)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject \ src \ Ninject \ Planning \ Targets \ Target.cs中: 179
Ninject.Planning.Targets.Target 1.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.WhereSelectArrayIterator
2.MoveNext()+85
System.Linq.Buffer 1..ctor(IEnumerable
1来源)+325
System.Linq.Enumerable.ToArray(IEnumerable 1 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__DisplayClass12
3.b__11(TSource x)+32
System.Linq.WhereSelectEnumerableIterator 2.MoveNext() +151
System.Linq.Enumerable.SingleOrDefault(IEnumerable
1 source)+4178557
Ninject.Web.Mvc.NinjectDependencyResolver.GetService(类型serviceType)在c:\ Projects \ Ninject \ Maintenance2.2 \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectDependencyResolver.cs:56
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,Type controllerType)+51
提前致谢。我想要一个失败的工作演示然后让我知道。
答案 0 :(得分:0)
感谢Tyler Brinks发现我的拼写错误!
将名称空间引用更新为
c.IncludeNamespace( “TestAopLogging.Models *”);
一切都很顺利。
希望有人觉得这很有用。