Search code examples
c#web-servicesasmx

Saving all posts to .NET WebService in XML to DB


I am sure this is an easy question however I am not finding the solution. I want to save the XML files posted to my .NET Web Service. I am sure it is just a simple method call when the service is invoked but I am not finding it. I would like to save the full XML posted to the service

Any help would be greatly appreciated. Thank you in advance.


Solution

  • Thank you for your help however I found what I was looking for at http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx

    To help anyone else that is new to tring to implement this via WebService's I included the steps that I performed to implement it and modifications so I could save it to the db as well as the file system I performed the following steps. If you have any questions please feel free to aks and I will be happy to answer.

    1. Created a code file in my web service project with the code listed in the in the article
    2. Created a few properties to store values to save to the db

      private string _requestXml;
      private DateTime _start;
      
    3. Then I modified the WriteInput method to save values to those variables.

      public void WriteInput(SoapMessage message)
      {
          //Begin Edit
          oldStream.Position = 0;
          _requestXml = new StreamReader(_oldStream).ReadToEnd();
          _start = DateTime.UtcNow;
          //End Edit
          //Begin Original Code
          oldStream.Position = 0;
          Copy(oldStream, newStream);
          var fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
          var w = new StreamWriter(fs);
          var 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;
      }
      
    4. Then I modified the WriteOutput to

      public void WriteOutput(SoapMessage message)
      {
          //Begin Edit
          var responseXml = new StreamReader(newStream).ReadToEnd();
          newStream.Position = 0;
          //Start process for saving to DB 
          //"_requestXml" = Original Request Soap Message
          //"responseXml" = Service Returned Response
          //"_start" = Request Start Time
          //message.MethodInfo.Name = I save this so I know what method from     
          //message.Url = I save this so I know the original ASMX that was hit
          //End Edit
          //Begin Original Code
          newStream.Position = 0;
          var fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
          var w = new StreamWriter(fs);
          var 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);
      }
      
    5. Now all that's left is to add the following to your service call and you should be good to go

      [WebMethod, NameSpace.OfyourTraceExtesionClass.TraceExtension]
      public void WebMethod1()
      {
          //DoSomeStuff
      }