Reputation: 35
Using .NET 3.5 C#, I have a WCF web service hosted in IIS with multiple ServiceContracts and each service having multiple OperationContracts. Recently I have seen performace issues and would like to generically log the execution time of each operation without having to add code to each method. What is the best way to achieve this. Here is one of my service contract interface:
[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();
}
And here is an example implementation where I have currently put a Stopwatch in the operation itself which I would like to change:
[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;
}
}
Upvotes: 2
Views: 1054
Reputation: 171206
Implement IOperationInvoker
. This interface is used to call your service method. You can wrap your service method with a stopwatch and exception handling. You can write the log item there as well.
There is a central way to apply the invoker to all service methods. See the linked question.
Upvotes: 1