【问题标题】:Generically calculate WCF operation execution times一般计算 WCF 操作执行时间
【发布时间】:2014-06-04 18:43:49
【问题描述】:

使用 .NET 3.5 C#,我有一个托管在 IIS 中的 WCF Web 服务,其中包含多个 ServiceContract,每个服务都有多个 OperationContract。最近我看到了性能问题,并希望一般地记录每个操作的执行时间,而不必向每个方法添加代码。实现这一目标的最佳方法是什么。这是我的服务合同接口之一:

[ServiceContract]
public interface IAuthentication
{        
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "AuthBegin", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply AuthBegin(AuthBeginRequest authBegin);

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "AuthComplete", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply AuthComplete(AuthCompleteRequest authComplete);

    [OperationContract]
    [WebGet(UriTemplate = "Logout", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply LogOut();

}

这是一个示例实现,我目前在操作本身中放置了一个秒表,我想更改它:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]   
public class Authentication : IAuthentication
{              
    public ServiceReply Auth1(ClientConfig _clientConfig)
    {            
        ServiceReply ret = new ServiceReply();
        Stopwatch sw = Stopwatch.StartNew();                        

        try
        {

        }
        catch (Exception exc)
        {

        }

        sw.Stop();
        //Log.InfoFormat("Method {0} --> Elapsed time={1}", methodName, sw.Elapsed);

        return ret;
    }
}

【问题讨论】:

    标签: c# .net wcf generics


    【解决方案1】:

    Implement IOperationInvoker.这个接口用来调用你的服务方法。您可以使用秒表和异常处理来包装您的服务方法。你也可以在那里写日志。

    有一种将调用程序应用于所有服务方法的中心方法。请参阅链接的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2021-06-21
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多