How to get information about error from HttpContext in WCF services

546 views Asked by At

I need to log information about contract invocation (following info: request, client address, invoked method name) when client passes invalid SOAP request to WCF service contract (NetDispatcherFaultException is thrown). I can get all needed info from HttpContext object in Global class (Global.asax), for example in Application_EndRequest event handler. My code (it's work fine):

protected void Application_EndRequest(object sender, EventArgs e)
{
    const string InfoMessageTemplate = "Client [address='{0}'] called following contract: '{1}'. Request data: {2}";

    var currentRequest = Context.Request;
    var calledContract = currentRequest.Headers["SOAPAction"] ?? string.Empty;
    string clientAddress = currentRequest.UserHostAddress;

    var requestInputStream = new StreamReader(currentRequest.InputStream);
    string requestString = HttpUtility.UrlDecode(requestInputStream.ReadToEnd());

    Logger.Info(InfoMessageTemplate, clientAddress, calledContract, requestString);
}

But I don't want to log all requests, only when NetDispatcherFaultException is thrown. I suggested that HttpContext.Error and/or HttpContext.AllErrors properties should contain information about occurred exception but they are always empty (equal NULL).

I have implemented IErrorHandler (global exception handling) and connected it to my WCF service. HandleError method is called each time when any error occurred. But I cannot get access to HttpContext inside this method (because this method executed in another thread) and I cannot get all needed info for logging.

So, I think my last hope is event handlers in Global.asax. If I could to get information about occurred error it would have helped me a lot. Can I do this somehow?

0

There are 0 answers