I'm working on a project that uses several WCF calls to perform a procedure. The first call returns a unique ID (Guid) which all of the following calls must supply. Server-side, each call is logged together with that unique ID so that the course of a single procedure can be watched (suppose that a client can begin several of those procedures at the same time and they are thread-safe).
A client-side example would be something like the following:
string clientName = GetClientName();
Guid uniqueId = wcfClient.Begin(clientName);
object data = wcfClient.GetData(clientName, uniqueId);
object processedData = ProcessDataLocally(data);
bool success = wcfClient.confirmDataValidity(clientName, uniqueId, processedData);
// ...
wcfClient.End(clientName, uniqueId);
If something goes wrong on the server side, I'd like that to be logged as well, using the uniqueId given to the call that went wrong. For that purpose, I made all the methods of the service use a function like the following:
private T DoProcess<T>(Guid uniqueId, Func<T> process)
{
try
{
return process();
}
catch(Exception ex)
{
Log(ex, uniqueId);
throw; // or throw the same thing every time, if the client
// shouldn't know the details
}
}
public object GetData(string clientName, Guid uniqueId)
{
return DoProcess(uniqueId, () =>
{
object data;
// Generate data
return data;
});
}
However, it seems that doing so doesn't catch all possible errors (for example, if the connection times out).
I read about implementing the IErrorHandler
interface and attaching it to the service. While this seems to catch all errors, there seems to be no way to pass information to it other than the exception, such as the uniqueId. Therefore there is no way to log the ID.
Is it possible to catch all exceptions and include data that has been passed to the method in which the exception was thrown?