0
// Created obj for wcf service
ServiceSummary.ImageService.ManagerServiceClient obj1 = new ServiceSummary.ImageService.ManagerServiceClient(); 

// Forming a request body
var request = new ImageService.GetImageRequest
                {
                    UserContextData = new ImageService.UserContextData
                    {
                        Country = Country.ToUpper(),
                        Region = Region.ToUpper()
                    },
                };

// Invoking GetImageResponse and storing result in response variable
var response = obj1.GetImageResponse(request);

The response is returned of type class - how to get the response in XML format instead?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
p995
  • 49
  • 8
  • Does this answer your question? [How do I get the XML SOAP request of an WCF Web service request?](https://stackoverflow.com/questions/5493639/how-do-i-get-the-xml-soap-request-of-an-wcf-web-service-request) – Crowcoder Nov 27 '19 at 17:12

1 Answers1

1

I am a little confused that why we need that primitive XML data. But we can completely get the source message, SOAP message envelops by using IClientMessageInspector.
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iclientmessageinspector?redirectedfrom=MSDN&view=netframework-4.8
Here is an example, assumed that you call the service by using a client proxy.

public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            Console.WriteLine(reply);
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            return null;
        }
    }
    public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
    {
        public Type TargetContract => typeof(IService);

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            return;
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
            return;
        }
}

Then apply the contract behavior on the automatically generated service contract.

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService")]
    [CustContractBehavior]
public interface IService {

Result.
enter image description here
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • 1
    Hi, Thanks for the response, above will fetch you the xml with all tags and i needed it in only xml format, so did it in blow format:var response = obj1.GetImageResponse(request); using (var stream = new MemoryStream()) { var serialOg = new XmlSerializer(typeof(ImageService.GetImageResponse()); var xtww = new XmlTextWriter(stream, new UTF8Encoding(false)); serialOgofCartsvc.Serialize(xtww, response); xtww.Close(); } – p995 Dec 01 '19 at 18:13
  • yes, you can serialize the results again to return data in `XML` format. – Abraham Qian Dec 02 '19 at 06:01