あなたはすでにあなたのインスタンスを持っている必要があります...
container.Register(Component.For<IInterceptor>().ImplementedBy<Logger>().Named("myinterceptor"));
次に、それぞれのクラスをインターセプタで属性でデコレートします。あなたがインターセプタを実装する場合
[Interceptor("myinterceptor")]
public class Logger : IInterceptor
{
public void Intercept(IInvocation invocation) {
Console.WriteLine(">> type in <<");
invocation.Proceed();
Console.WriteLine(">> type out <<");
}
}
あなたは、このような横断的関心事は、インターセプタによって対処されるように定義されているので、解決されたタイプを知ることができるようになります。
私は、インターセプタの指示にバインドされているすべてのタイプではなく、インターセプタとの契約が必要なすべてのタイプをインターセプタに知らせたいと思っています。私はこれが助けて欲しい!
EDITは:私はそれらを登録する方法
Public Class LoggingInterceptionBehavior
Implements IInterceptionBehavior
Public Function Invoke(input As IMethodInvocation, getNext As GetNextInterceptionBehaviorDelegate) As IMethodReturn Implements IInterceptionBehavior.Invoke
' Before invoking the method on the original target.
Dim icp As ClaimsPrincipal = TryCast(Thread.CurrentPrincipal, ClaimsPrincipal)
' Access IClaimsIdentity which contains claims
Dim claimsIdentity As ClaimsIdentity = DirectCast(icp.Identity, ClaimsIdentity)
Dim param = GetParam(input)
If claimsIdentity IsNot Nothing Then
If param IsNot Nothing Then
WriteLog([String].Format("{0} is invoking method {1} at {2} with a parameter of {3}", claimsIdentity.Name, input.MethodBase, DateTime.Now.ToLongTimeString(), param))
Else
WriteLog([String].Format("{0} is invoking method {1} at {2} without a parameter", claimsIdentity.Name, input.MethodBase, DateTime.Now.ToLongTimeString()))
End If
Else
'no claim
WriteLog([String].Format("NO CLAIM Invoking method {0} at {1} with a parameter of {2}", input.MethodBase, DateTime.Now.ToLongTimeString(), param))
End If
' Invoke the next behavior in the chain.
Dim result = getNext()(input, getNext)
' After invoking the method on the original target.
If result.Exception IsNot Nothing Then
WriteCriticalLog([String].Format("Method {0} threw exception {1} at {2}", input.MethodBase, result.Exception.Message, DateTime.Now.ToLongTimeString()))
Else
WriteLog([String].Format("Method {0} returned {1} at {2}", input.MethodBase, result.ReturnValue, DateTime.Now.ToLongTimeString()))
End If
Return result
End Function
End Class
::これは私がロガーにUnityの使用方法です
container.RegisterType(Of IXXXService, XXXService)(New Interceptor(Of InterfaceInterceptor)(),
New InterceptionBehavior(Of LoggingInterceptionBehavior))
ありがとう、まさに私が探していたものです。この場所のInvocationTargetへの参照を取得することも可能ですか? (インターセプトメソッドのIInvocation.InvocationTargetと同じ?)。私はComponentModelで同等のプロパティを見つけることができません。 – user2959547