通常计算WCF操作执行时间

时间:2014-06-04 10:04:12

标签: c# .net wcf generics

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

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

1 个答案:

答案 0 :(得分:1)

Implement IOperationInvoker.此界面用于调用您的服务方法。您可以使用秒表和异常处理来包装服务方法。您也可以在那里编写日志项目。

有一种将调用程序应用于所有服务方法的核心方法。请参阅相关问题。