简体   繁体   中英

How to get request before call to webservice

Problem: As part of a debugging a problem regarding some validation issues I would like to read the XML request of an WCF webservice.

Apparently, this is more difficult than it appears and any help in this regard would be much appreciated. Below are what I've tried already. Much like the answer to a similar question here on StackOverflow ( link ).

My solution: I've created the client setting the endpoint given by the provider of the webservice. I've added my client credentials as an endpoint behavior. Right before I make the call to service I add another endpoint behavior to write the request and response as XML-files. Alas, to no avail.

The simple call to the webservice:

public SaveAvailabilityAssessmentResponseType SaveAvailabilityAssessment(SaveAvailabilityAssessmentRequestType request)
{
   Client.Endpoint.Behaviors.Add(new CustomEndpointBehavior());

   return Client.SaveAvailabilityAssessment(_ocesCertHeader, _activeOrganisationHeader, request);
}

And here are the CustomEndpointBehavior class (simplified a bit):

public class CustomEndpointBehavior : IEndpointBehavior
{
    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new MessageExpector());
    }
}

And here's the MessageExpector class:

internal class MessageExpector : IClientMessageInspector
{
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        using (var sw = new StreamWriter(@"C:\temp\response.xml"))
        {
            sw.WriteLine(reply);
        }
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        using (var sw = new StreamWriter(@"C:\temp\request.xml"))
        {
            sw.WriteLine(request);
        }
        return new object();
    }
}

Can anyone tell me what I'm missing?

Edit: Further debugging has showed, that the code in the CustomEndpointBehavior hasn't been activated. It is as if the customendpoint hasn't been added to the client's endpoint behaviors. But how can that be?

You can configure message logging without modifying your code. Here's a link to documentation . You can use SvcTraceViewer.exe for viewing this logs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM