c# - 如何检查方法是否具有 CaSTLe 拦截器的属性?

标签 c# wcf dependency-injection castle-windsor castle-dynamicproxy

我正在学习 CaSTLe Windsor 和 WCF 的依赖注入(inject)和拦截,我想检查拦截的方法是否具有自定义属性 (LogAttribute)。

(我的示例基于此答案:https://stackoverflow.com/a/2593091)

服务契约(Contract):

[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
        };
    }
}

依赖注入(inject)和拦截:

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.GetCustomAttributesAttribute.GetCustomAttribute,但没有找到 LogAttribute。有什么想法吗?

最佳答案

(Windsor) 服务用于 IOrderService 接口(interface),因此 invocation.Method 将指向接口(interface)上的方法,它没有属性。

使用invocation.MethodInvocationTarget获取类的方法

关于c# - 如何检查方法是否具有 CaSTLe 拦截器的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702125/

相关文章:

c# - 两次存储相同列表的内存使用,按不同标准排序?

c# - 如何获取子元素的Renderer?

c# - 当 PC 脱离底座时,WPF/WCF 应用程序会导致蓝屏

dependency-injection - 在 MVC6 和 EF7 的 BaseController 中初始化 DbContext?

javascript - 如何在模块导入/配置中设置 .env 变量

C# XMPP X-FACEBOOK-PLATFORM SASL 实现

c# - Protocol Buffer#3 将消息从 c++ 发送到 c#

c# - 发送大量消息时出现 SocketException(代码 10048)

c# - NHibernate 使用 WCF 序列化延迟加载的实体

java - Micronaut:如何映射 HashMap 中的所有属性值?