How to serialize an object to a string \ xml with its headers

I am using a third-party web service client (created using the "Add Service Link") to get some data.

After filling in the web service objects with the relevant data, we need to add some data to the headers (encrypted password and some other predefined data)

Then we serialize each request sent to the web service using the standard .net XmlSerializer .

However, as a result of serialization, I do not see the request headers. I searched for a long time and could not find a way to "print" them either.

Here is a sample code:

 Ibooking proxy = new BookingManager(); /* Init proxy Data...*/ GetAvailabilityRequest request = new GetAvailabilityRequest(); /*Fill more data on the request...*/ GetAvailabilityResponse response = proxy.GetAvailability(request); //Send request to the web service var xmlString2 = response.Serialize(); //only body, no headers in the XML /* Extension class to Serialize any object */ public static class ExtensionUtil { public static string Serialize<T>(this T value) { try { XmlSerializer xmlserializer = new XmlSerializer(typeof(T)); var stringWriter = new StringWriter(); using (var writer = XmlWriter.Create(stringWriter)) { xmlserializer.Serialize(writer, value); return stringWriter.ToString(); } } catch (Exception ex) { throw new Exception("An error occurred", ex); } } } 

I excluded the code that adds more data to the request, since it is long and complex (you need to implement IEndpointBehavior and IClientMessageInspector to "catch" the request before sending it), but for now, as a workaround, I put BreakPoint in the Message object and convert it to string using Visual Studio. This way I see the headers, but obviously this is bad practice since I want it to be automated in serialization.

+6
source share
2 answers

I would like to see an example of how you add these headers.

In most web services, the message body is the part that is serialized in XML or JSON - the headers are not.

You may be able to verify the service call using Fiddler and the proxy made by a small change to your web.config, as described in this article: http://weblog.west-wind.com/posts/2008/Mar/14/ Debugging-Http-or-Web-Services-Calls-from-ASPNET-with-Fiddler .

In short, add the following to your web.config or app.config:

  <system.net> <defaultProxy> <proxy proxyaddress="http://127.0.0.1:8888" /> </defaultProxy> </system.net> 

Download and run Fiddler during a service call, and you will see and be able to test the call in Fiddler.

If you want to check and / or change the headers in your code base, you can study the implementation of IClientMessageInspector or IDispatchMessageInspector. Here are a couple of related articles:

https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector(v=vs.100).aspx http://weblogs.asp.net/paolopia/writing-a-wcf- message-inspector

Here is the implementation I made. I don’t need access to the headers, but rather to change the xml namespaces created by the service client, but it should give you an idea of ​​how to implement the implementation: How can I create custom XML namespace attributes when using the legacy SOAP service?

0
source

OperationContext is your friend here. Use OperationContextScope to transfer the service call, and then use OperationContext.Current to get all the hidden useful properties you need.

https://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontextscope(v=vs.110).aspx

Please note that you need to know the specific types of headers you want to receive, and I had some problems getting the values, not just the names of the headers if they are not marked as serializable when using the XmlSerializer

0
source

All Articles