I have a开发者_如何转开发 unity container that's configured via a XML file. After it's configured I want to add some interception to certain types via code. how can this be done? I have the following behavior:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity.InterceptionExtension;
using NLog;
namespace WebDibaelsaMVC.Utils.Behaviors
{
public class LoggingBehavior : IInterceptionBehavior
{
private readonly Logger _log = LogManager.GetLogger("Unity");
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
var msg = getNext()(input, getNext);
if (msg.Exception != null)
_log.ErrorException("Error d'unity.", msg.Exception);
return msg;
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return new[] {typeof (IController)};
}
public bool WillExecute
{
get
{
return true;
}
}
}
}
and I want that all call to IController methods of types that are resolved through the container to pass through this behavior. How can I do it?
Simply call the configuration API after you've loaded the configuration. There's nothing magic about "configuration time"; the rule for Unity is "last configuration wins." So you can load from XML, do stuff with the API, and load a second XML section and they'll all get loaded together.
If you're using interception with MVC, be aware that really the only way to get it working correctly is to use the VirtualMethodInterceptor; using an instance interceptor would also require a custom action invoker to get everything working correctly (trust me, I've tried).
精彩评论