【发布时间】: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;
}
}
【问题讨论】: