0

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?

Community
  • 1
  • 1
  • Do you have a valid endPoint? Do you have a request? For code to work you need to initialize the request by using the HttpRequest or HttpWebRequest using the Create() method. – jdweng Jun 14 '16 at 11:05
  • Yes, I have both a valid endpoint (the other methods on the client works as intended) and a request. I'm assuming you are referring to the request variable in the call to the client method. – Kasper Lethan Jun 14 '16 at 12:23
  • You are writing to the same file twice. Do you want to append the data? I'm wondering if you are saving the request instead of the response. – jdweng Jun 14 '16 at 16:12
  • The request should be written to C:\temp\request.xml and the response should be written to C:\temp\response.xml. – Kasper Lethan Jun 15 '16 at 06:32
  • What port number are you using? Try port number above 10,000. Low port numbers below 1,000 are often blocked by virus checkers and firewalls. You can check the status of the ports from cmd.exe using : netstat -a. Make sure the port is not being used by another application. – jdweng Jun 15 '16 at 11:29

1 Answers1

1

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

Aleksey L.
  • 35,047
  • 10
  • 74
  • 84