locked
How to get Soap Request and Response for multipile API RRS feed

  • Question

  • User-171003712 posted

    Hello,

    I am Use a SOAP Extension for getting a Request and Response and its working fine

    but the problem is how to maintain a diff log for each API and Each Request and Response

    I mean 1API for Airline and 2 API for Hotel

    now each Request and Response i have to maintain a Log system so we can easily understand which log is belongs to which API

    This is my Code

    namespace XmlStreamSoapExtension
    {
        public class TraceExtension : SoapExtension
        {
            Stream oldStream;
            Stream newStream;
            string filename;
     
            // Save the Stream representing the SOAP request or SOAP response into a local memory buffer.
            public override Stream ChainStream(Stream stream)
            {
                oldStream = stream;
                newStream = new MemoryStream();
                return newStream;
            }
     
            public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
            {
                return null;
            }
     
            public override object GetInitializer(Type WebServiceType)
            {
                return null;
            }
     
            // Set the log file
            public override void Initialize(object initializer)
            {
                filename = "c:\\log.txt";
            }
     
            //  Save SoapRequest or SoapResponse to the log file.
            public override void ProcessMessage(SoapMessage message)
            {
                switch (message.Stage)
                {
                    case SoapMessageStage.BeforeSerialize:
                        break;
                    case SoapMessageStage.AfterSerialize:
                        WriteOutput(message);
                        break;
                    case SoapMessageStage.BeforeDeserialize:
                        WriteInput(message);
                        break;
                    case SoapMessageStage.AfterDeserialize:
                        break;
                    default:
                        throw new Exception("invalid stage");
                }
            }
     
            public void WriteOutput(SoapMessage message)
            {
                newStream.Position = 0;
                FileStream fs = new FileStream(filename, FileMode.Append,
                    FileAccess.Write);
                StreamWriter w = new StreamWriter(fs);
     
                string soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
                w.WriteLine("-----" + soapString + " at " + DateTime.Now);
                w.Flush();
                Copy(newStream, fs);
                w.Close();
                newStream.Position = 0;
                Copy(newStream, oldStream);
            }
     
            public void WriteInput(SoapMessage message)
            {
                Copy(oldStream, newStream);
                FileStream fs = new FileStream(filename, FileMode.Append,
                    FileAccess.Write);
                StreamWriter w = new StreamWriter(fs);
     
                string soapString = (message is SoapServerMessage) ? "SoapRequest" : "SoapResponse";
                w.WriteLine("-----" + soapString + " at " + DateTime.Now);
                w.Flush();
                newStream.Position = 0;
                Copy(newStream, fs);
                w.Close();
                newStream.Position = 0;
            }
     
            void Copy(Stream from, Stream to)
            {
                TextReader reader = new StreamReader(from);
                TextWriter writer = new StreamWriter(to);
                writer.WriteLine(reader.ReadToEnd());
                writer.Flush();
            }
        }
     
        // Create a SoapExtensionAttribute for the SOAP Extension that can be applied to an XML Web service method.
        [AttributeUsage(AttributeTargets.Method)]
        public class TraceExtensionAttribute : SoapExtensionAttribute
        {
            private int priority;
     
            public override Type ExtensionType
            {
                get { return typeof(TraceExtension); }
            }
     
            public override int Priority
            {
                get { return priority; }
                set { priority = value; }
            }
        }
    }
    Wednesday, June 18, 2014 5:39 AM

Answers

  • User-417640953 posted

    now each Request and Response i have to maintain a Log system so we can easily understand which log is belongs to which API

    Hi dharmeshshar,

    Thanks for your post.

    If you want to log message to different files according to different web service API, I suggest you

    try to use the SoapExtension.GetInitializer() method to complete it like below. "The following code

    demonstrates how one can save SOAP extension specific data on a per XML Web service basis.

    If the SOAP extension is configured using a configuration file instead of an attribute, the SOAP extension

    can store data for each class to which the SOAP extension is applied."

        // The extension was configured to run using a configuration file instead of an attribute applied to a  
        // specific XML Web service method. Return a file name based on the class implementing the XML Web service's type. 
        public override object GetInitializer(Type WebServiceType) 
    	{
    	   // Return a file name to log the trace information to based on the passed in type. 
    	   return "C:\\" + WebServiceType.FullName + ".log";    
        }
    

    For more information, please follow below document.

    http://msdn.microsoft.com/en-us/library/7xbtd2s0(v=vs.110).aspx

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2014 10:40 PM

All replies

  • User103196646 posted

    Hello! Thanks for your post!

    According to Microsoft, SOAP Extension is a legacy technology - http://msdn.microsoft.com/en-us/library/vstudio/7w06t139(v=vs.100).aspx.

    XML Web services and XML Web service clients should now be created using  Windows Communication Foundation.

    Instead of using SOAP Extensio can you add a service reference to each of the 3 web services in Visual Studio - and then create a separate client for each web service?

    Regards!

    Wednesday, June 18, 2014 3:16 PM
  • User-417640953 posted

    now each Request and Response i have to maintain a Log system so we can easily understand which log is belongs to which API

    Hi dharmeshshar,

    Thanks for your post.

    If you want to log message to different files according to different web service API, I suggest you

    try to use the SoapExtension.GetInitializer() method to complete it like below. "The following code

    demonstrates how one can save SOAP extension specific data on a per XML Web service basis.

    If the SOAP extension is configured using a configuration file instead of an attribute, the SOAP extension

    can store data for each class to which the SOAP extension is applied."

        // The extension was configured to run using a configuration file instead of an attribute applied to a  
        // specific XML Web service method. Return a file name based on the class implementing the XML Web service's type. 
        public override object GetInitializer(Type WebServiceType) 
    	{
    	   // Return a file name to log the trace information to based on the passed in type. 
    	   return "C:\\" + WebServiceType.FullName + ".log";    
        }
    

    For more information, please follow below document.

    http://msdn.microsoft.com/en-us/library/7xbtd2s0(v=vs.110).aspx

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2014 10:40 PM
  • User-171003712 posted

    Hello csharpgreg

    Thanks for reply .

    Yes i m doing same and create a SOAP extention for get log and add tags into web.config file and its get all logs for me

    but problem is each API wise i have to create a diff logs for each request .

    I hope you understand my question!

    Thursday, June 19, 2014 12:00 AM
  • User-171003712 posted

    Hello Fuxiang,

    Thanks for reply.

    Yes i have done in web.config file as you said let me use your code

    Thanks

    Thursday, June 19, 2014 12:03 AM