【问题标题】:Instrumentation of method calls in a web app using PostSharp使用 PostSharp 在 Web 应用程序中检测方法调用
【发布时间】:2011-03-13 20:06:26
【问题描述】:

例子:

我为方法添加了一个 PostSharp 属性,以确保秒表在调用该方法之前启动,并在调用返回后停止。该方法用于web应用,所以会被多个线程调用。

我将秒表计时的结果存储在静态线程安全集合中,供所有线程使用。然后可以由另一个监视线程读取此集合以进行分析。

这会有效地强制所有方法调用在等待释放集合上的锁时阻塞吗? (以便他们可以完成 postsharp 代码块)。

使用 MSMQ 的异步消息传递是否可以为这个问题提供非阻塞解决方案?

PostSharp 属性代码:

//...

public static ThreadSafeCollection _collection;

public override void  OnInvocation(MethodInvocationEventArgs eventArgs)
{
  var start = DateTime.Now;
  eventArgs.Proceed();
  var timeSpent = (DateTime.Now - start).TotalMilliseconds;

  _collection.Add(timeSpent); //will this cause all 
                              //method calls to methods 
                              //decorated with this 
                              //attribute to block on the 
                              //_collection addition?
}

//...

【问题讨论】:

    标签: c# msmq postsharp multithreading


    【解决方案1】:

    这是否会有效地强制所有方法调用在等待释放集合上的锁时阻塞? (以便他们可以完成 postsharp 代码块)。

    是的

    使用 MSMQ 的异步消息传递是否可以为这个问题提供非阻塞解决方案?

    也许吧。 msmq 如何处理发送请求的内部细节没有记录(据我所知)。

    其他选项是使用特定于线程(非同步)的集合。并且仅当共享集合上的锁空闲时才将其内容移动到共享集合(不同步)。

    【讨论】:

      【解决方案2】:

      您应该使用 OnMethodBoundaryAspect,因为 OnMethodInvocationAspect 开销很大。鉴于你的方面的效果非常轻量级,PostSharp 2.0 专业版将生成更高效的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-22
        相关资源
        最近更新 更多