Reputation: 1607
I am learning dependency injection and interception with Castle Windsor and WCF,and I would like to check if an intercepted method has a custom attribute (LogAttribute).
(My example is based on this answer : https://stackoverflow.com/a/2593091 )
Service contract :
[ServiceContract]
public interface IOrderService
{
[OperationContract]
Order GetOrder(int orderId);
}
[DataContract]
public class Order
{
[DataMember]
public string Id { get; set; }
// [...]
}
Service implementation :
public class OrderService : IOrderService
{
private readonly IDatabase _database;
public OrderService(IDatabase database)
{
_database = database;
}
[Log] // <- my custom attribute
public Order GetOrder(int orderId)
{
return _database.GetOrder(orderId);
}
}
public class LogAttribute : Attribute
{ }
Simple data access layer :
public interface IDatabase
{
Order GetOrder(int orderId);
}
public class Database : IDatabase
{
public Order GetOrder(int orderId)
{
return new Order
{
Id = orderId
};
}
}
Dependency injection and interception :
public class Global : HttpApplication
{
public static WindsorContainer Container { get; private set; }
protected void Application_Start(object sender, EventArgs e)
{
BuildContainer();
}
private static void BuildContainer()
{
if (Container != null)
return;
Container = new WindsorContainer();
Container.AddFacility<WcfFacility>();
Container.Register(Component.For<IInterceptor>().ImplementedBy<MyInterceptor>().LifestyleTransient());
Container.Register(Component.For<IDatabase>().ImplementedBy<Database>().LifestylePerWcfOperation());
Container.Register(Component.For<IStringReverser>().ImplementedBy<StringReverser>().Interceptors<MyInterceptor>());
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
DoSomeWorkBefore(invocation);
invocation.Proceed();
}
private static void DoSomeWorkBefore(IInvocation invocation)
{
if (Attribute.IsDefined(invocation.Method, typeof(LogAttribute)))
{
// This part of the code is never executed
Debug.WriteLine("Method has Log attribute !");
}
}
}
I already tried invocation.Method.GetCustomAttributes
and Attribute.GetCustomAttribute
, but it does not find the LogAttribute. Any ideas ?
Upvotes: 1
Views: 1628
Reputation: 27374
the (Windsor) service is for IOrderService
interface and so invocation.Method
will point to the method on the interface, which doesn't have the attribute.
use invocation.MethodInvocationTarget
to get the class's method
Upvotes: 6