Get SOAP Message before sending it to the WebService in .NET

后端 未结 6 1815
粉色の甜心
粉色の甜心 2020-12-03 05:26

I\'m calling an external HTTPS webservice.

In order to check what is wrong, the owner needs the SOAP request I\'m sending.

I have a web reference and the gen

6条回答
  •  佛祖请我去吃肉
    2020-12-03 05:59

    You can use IClientMEssageInspector and IEndpointBehavior to fullfill this. I found using this way can capture the exact soap request likely as the fiddler one:

    Create a class like this in the same project:

    public class ClientMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
        {
            #region IClientMessageInspector Members
            public string LastRequestXml { get; private set; }
    
            public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
    
            }
    
            public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
            {
                string requestHeaderName = request.Headers.Action.Replace("urn:#",string.Empty);
                LastRequestXml = request.ToString();
                string serializedRequestFile = string.Format(requestHeaderName + "_request_{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmss"));
                string exportedFolder = ConfigurationManager.AppSettings["SubmittedRequestXmLocation"];
                printSoapRequest(request, exportedFolder, serializedRequestFile);
    
                return request;
            }
    
            public void printSoapRequest(System.ServiceModel.Channels.Message request, string exportedFolder, string fileName)
            {
                if (exportedFolder.Equals(string.Empty))
                    return;
    
                if (!Directory.Exists(exportedFolder))
                {
                    Directory.CreateDirectory(exportedFolder);
                }
                string exportedFile = string.Format("{0}\\{1}", exportedFolder, fileName);
                if (File.Exists(exportedFile))
                {
                    File.Delete(exportedFile);
                }
    
                string strRequestXML = request.ToString();
                XDocument xDoc = XDocument.Parse(strRequestXML);
                XmlWriter xw = XmlWriter.Create(exportedFile);
                xDoc.Save(xw);
                xw.Flush();
                xw.Close();
                LogOutput("Request file exported: " + exportedFile);
    
            }
    
        }
    
        public class CustomInspectorBehavior : IEndpointBehavior
        {
            private readonly ClientMessageInspector clientMessageInspector = new ClientMessageInspector();
    
            public string LastRequestXml
            {
                get { return clientMessageInspector.LastRequestXml; }
            }
    
            public string LastResponseXml
            {
                get { return clientMessageInspector.LastRequestXml; }
            }
    
            public void AddBindingParameters(
                ServiceEndpoint endpoint,
                System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(clientMessageInspector);
            }
        }
    

    Then you can call it like the following:

    ProxyClass _class = new ProxyClass();
    var requestInterceptor = new CustomInspectorBehavior();
               _client.Endpoint.Behaviors.Add(requestInterceptor);
    

    When you call the service method, it will automatically execute the interceptor and print the output. Using this way you can also manipulate the soap message before sending to the server!

提交回复
热议问题