I'm using Simple Injector in my project. For integration Simple Injector with Castle.DynamicProxy I'm using this example.
I have following attributes:
public class MyLogAttribute : Attribute { // some code. }
public class MyTimerAttribute : Attribute { // some code. }
Then these attributes apply to method in my service.
public interface IMyService
{
void Do();
}
public class MyService : IMyService
{
[MyLog, MyTimer]
public void Do() { // some code. }
}
In my interceptors i am trying to get a custom attribute that applied to method, using the following code:
public class MyLogIntercept : Castle.DynamicProxy.IInterceptor
{
public void Intercept(Castle.DynamicProxy.IInvocation invocation)
{
var method = invocation.GetConcreteMethod();
method = invocation.InvocationTarget.GetType().
GetMethod(method.Name);
var attribute = method.GetCustomAttribute<MyLogAttribute>();
if(attribute != null)
{
// some code.
}
else
invocation.Proceed();
}
}
public class MyTimerIntercept : Castle.DynamicProxy.IInterceptor
{
public void Intercept(Castle.DynamicProxy.IInvocation invocation)
{
var method = invocation.GetConcreteMethod();
method = invocation.InvocationTarget.GetType().
GetMethod(method.Name);
var attribute = method.GetCustomAttribute<MyTimerAttribute>();
if(attribute != null)
{
// some code.
}
else
invocation.Proceed();
}
}
These interceptors registered using following code:
container.InterceptWith<MyLogIntercept>(
type => type == typeof(IMyService));
container.InterceptWith<MyTimerIntercept>(
type => type == typeof(IMyService));
My problem is that when i am trying in Intercept()
method get a custom attribute i get null
(attribute == null
). How can i get my a custom attributes?
P.S. If registered one intercept (MyTimerIntercept
or MyLogIntercept
it doesn't metter) for my service, in Intercept()
method i can get a custom attribute successfully (attribute != null
), but if both interceptor registered i have problem (attribute == null
).
P.S. I am using Castle.Core 3.3.3