locked
SoapExtension is not logging RRS feed

  • Question

  • User1663786169 posted

    Here is my code, example from msdn ->

    1    namespace Logging
    2    {
    3      using System;
    4      using System.Web.Services;
    5      using System.Web.Services.Protocols;
    6      using System.IO;
    7      using System.Net;
    8    
    9      public class TraceExtension : SoapExtension
    10     {
    11       Stream oldStream;
    12       Stream newStream;
    13       string filename;
    14   
    15       public override Stream ChainStream(Stream stream)
    16       {
    17         System.Diagnostics.Debug.WriteLine("ChainStream");
    18         oldStream = stream;
    19         newStream = new MemoryStream();
    20         return newStream;
    21       }
    22   
    23       public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    24       {
    25         return ((TraceExtensionAttribute)attribute).Filename;
    26       }
    27   
    28       public override object GetInitializer(Type WebServiceType)
    29       {
    30         return "C:\\" + WebServiceType.FullName + ".log";
    31       }
    32   
    33       public override void Initialize(object initializer)
    34       {
    35         filename = (string)initializer;
    36       }
    37   
    38       public override void ProcessMessage(SoapMessage message)
    39       {
    40         switch (message.Stage)
    41         {
    42           case SoapMessageStage.BeforeSerialize:
    43             break;
    44           case SoapMessageStage.AfterSerialize:
    45             WriteOutput(message);
    46             break;
    47           case SoapMessageStage.BeforeDeserialize:
    48             WriteInput(message);
    49             break;
    50           case SoapMessageStage.AfterDeserialize:
    51             break;
    52           default:
    53             throw new Exception("invalid stage");
    54         }
    55       }
    56   
    57       public void WriteOutput(SoapMessage message)
    58       {
    59         newStream.Position = 0;
    60         FileStream fs = new FileStream(filename, FileMode.Append,
    61             FileAccess.Write);
    62         StreamWriter w = new StreamWriter(fs);
    63   
    64         string soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
    65         w.WriteLine("-----" + soapString + " at " + DateTime.Now);
    66         w.Flush();
    67         Copy(newStream, fs);
    68         w.Close();
    69         newStream.Position = 0;
    70         Copy(newStream, oldStream);
    71       }
    72   
    73       public void WriteInput(SoapMessage message)
    74       {
    75         Copy(oldStream, newStream);
    76         FileStream fs = new FileStream(filename, FileMode.Append,
    77             FileAccess.Write);
    78         StreamWriter w = new StreamWriter(fs);
    79   
    80         string soapString = (message is SoapServerMessage) ?
    81             "SoapRequest" : "SoapResponse";
    82         w.WriteLine("-----" + soapString +
    83             " at " + DateTime.Now);
    84         w.Flush();
    85         newStream.Position = 0;
    86         Copy(newStream, fs);
    87         w.Close();
    88         newStream.Position = 0;
    89       }
    90   
    91       void Copy(Stream from, Stream to)
    92       {
    93         TextReader reader = new StreamReader(from);
    94         TextWriter writer = new StreamWriter(to);
    95         writer.WriteLine(reader.ReadToEnd());
    96         writer.Flush();
    97       }
    98     }
    99   
    100    [AttributeUsage(AttributeTargets.Method)]
    101    public class TraceExtensionAttribute : SoapExtensionAttribute
    102    {
    103      private string filename = "c:\\log.txt";
    104      private int priority;
    105  
    106      public override Type ExtensionType
    107      {
    108        get { return typeof(TraceExtension); }
    109      }
    110  
    111      public override int Priority
    112      {
    113        get { return priority; }
    114        set { priority = value; }
    115      }
    116  
    117      public string Filename
    118      {
    119        get
    120        {
    121          return filename;
    122        }
    123        set
    124        {
    125          filename = value;
    126        }
    127      }
    128    }
    129  }
    130  
    

    And here is my webservice:

    1    using System;
    2    using System.Data;
    3    using System.Web;
    4    using System.Collections;
    5    using System.Web.Services;
    6    using System.Web.Services.Protocols;
    7    using System.ComponentModel;
    8    
    9    namespace WebService
    10   {
    11     /// <summary>
    12     /// Summary description for Service1
    13     /// </summary>
    14     [WebService(Namespace = "http://tempuri.org/")]
    15     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    16     [ToolboxItem(false)]
    17     public class Service1 : System.Web.Services.WebService
    18     {
    19   
    20       [WebMethod]
    21       [Logging.TraceExtensionAttribute(Filename="c:/test.log")]
    22       public string HelloWorld()
    23       {
    24         return "Hello World";
    25       }
    26     }
    27   }
    

    And my web.config:

    1    <?xml version="1.0"?>
    2    
    3    <configuration>
    4      
    5        <appSettings/>
    6        <connectionStrings/>
    7      
    8        <system.web>
    9            <compilation debug="true" />
    10           <authentication mode="Windows" />
    11         <webServices>
    12           <soapExtensionTypes>
    13             <add type="Logging.TraceExtension,SoapLogger,Version=1.0.0.0,Culture=neutral,PublicKeyToken=eddb91a0cc8c7ed2" group="High" priority="2"/>
    14           </soapExtensionTypes>
    15         </webServices>
    16       </system.web>
    17   </configuration>
    

     

    It's not logging anything, and i don't think it the TraceExtension class is accessed. In any case, i'm not able to debug the traceExtension class.

    Anybody?

    Wednesday, October 3, 2007 4:36 AM

All replies

  • User1663786169 posted

    Sorry, here is the missing link to msdn http://msdn.microsoft.com/en-us/library/7w06t139(vs.80).aspx

    Wednesday, October 3, 2007 4:57 AM
  • User758181324 posted

    are you hitting F5 and testing it? Or are you testing it from an application from which you have added a web reference?

    Friday, October 5, 2007 9:11 PM
  • User1663786169 posted

    Hi,

    Yes i was hitting F5 for testing, and that was the problem. You have to have an application calling the webservice or else nothing gets logged!

    Monday, October 8, 2007 2:13 AM
  • User-1623675128 posted

    all you need to do is, add soapextension at the client side config file so that client knows that it should make a call to soap extensions before making a call to Server else it wont even hit your trace calss whatosever.

     

    <soapExtensionTypes>
              <add type="Namesapce.className, NameSpace" priority="1" group="0" />
     </soapExtensionTypes>

    Thursday, February 13, 2014 1:23 AM
  • User-1776230271 posted

    Thank you! This was my problem also. Hitting F5 opens your web browser as a client and allows you to execute the web methods by clicking the links on the web page but does so only using HTTP POST and HTTP GET. It does not use the SOAP protocal so the SoapExtension subclass is never executed. Use SOAP UI or write your own little client (console application for example) so that the SOAP protocal is protocal is used.

    Tuesday, May 27, 2014 12:13 PM