1
Castle Windsor 및 WCF와 종속성 주입 및 차단에 대해 배우고 있는데 요격 된 메소드에 사용자 정의 속성 (LogAttribute)이 있는지 확인하고 싶습니다.메서드에 Castle Interceptor 속성이 있는지 확인하는 방법은 무엇입니까?
서비스 계약 :
[ServiceContract]
public interface IOrderService
{
[OperationContract]
Order GetOrder(int orderId);
}
[DataContract]
public class Order
{
[DataMember]
public string Id { get; set; }
// [...]
}
서비스 구현 :
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
{ }
간단한 데이터 액세스 레이어 :
public interface IDatabase
{
Order GetOrder(int orderId);
}
public class Database : IDatabase
{
public Order GetOrder(int orderId)
{
return new Order
{
Id = orderId
};
}
}
:
은 (
https://stackoverflow.com/a/2593091 내 예는이 대답을 기반으로)
종속성 주입 및 차단은 :
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 !");
}
}
}
이미 invocation.Method.GetCustomAttributes
및 Attribute.GetCustomAttribute
했지만, 그것은 LogAttribute을 찾을 수 없습니다. 어떤 아이디어?
"작동하지 않습니다"를 더 잘 정의 할 수 있습니까? 왜 작동하지 않습니까? –
@Steve : "작동하지 않습니다"는 것은 "메서드"가 "LogAttribute"라는 사실에도 불구하고 "if"코드가 실행되지 않는다는 것을 의미합니다. 의견을 주셔서 감사합니다, 나는 (나는 프랑스 인) 질문을 편집했습니다. –