Soap Extensions - Trying to log the soap messages (response/request).
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.webservices.

Post a new message to this list...

Bharath Sankaranarayan
Hello all:
I have been trying to use the Soap Extensions to try out a simple logging
application but it looks like the Soap Extension classes do not like File
IO. Here is my code in the following sequence
The issue is in the part b) Not sure why the logs are not written but it
looks like the SoapExtension class "narayan" containing the abc Method is
not working. if I comment out the code in the method the program works ( so
no logging done). I have tried the samples available on the net ( the one
by Rob Howard and it variants i.e various authors have tried to use this
code with minimal changes, changing the varialbles etc) and none of them
work. So I decided to write a simple one without the complications but still
does not work.
My environment is WinXP Pro with Visual Studio.Net Enterprise Architect and
with SP2 for .NET Framework applied.
Thanks for your comments or suggestions in advance.
Bharath

a) The Web Service - SoapEx.asmx
<%@ WebService Language="C#" class="SoapEx" %>
using System;
using System.Web.Services;
public class SoapEx{
[WebMethod][bharath()]
public string HelloWorld(string text){
return text;
}

}

b) The Soap Extension Class SoapAttribute.cs
using System;
using System.IO;
using System.Web.Services.Protocols;

[AttributeUsage(AttributeTargets.Method)]
public class bharathAttribute:SoapExtensionAttribute{
public override Type ExtensionType{
get{
return typeof(narayan);
}
}
public override int Priority{
get{
return 0;
}
set{
}
}

}
public class narayan:SoapExtension{

public void abc(string text){
FileStream fs = new
FileStream("c:\\soapex.txt",FileMode.Create,FileAccess.Write);
StreamWriter w = new StreamWriter(fs);
w.WriteLine(text);
w.Flush();
w.Close();
}

public override void Initialize(object o){
abc("Initialize");
}
public override object GetInitializer(LogicalMethodInfo m,
SoapExtensionAttribute a){
abc("GetInitializer 1");
return null;
}
public override object GetInitializer(Type s){
abc("GetInitializer 2");
return null;
}
public override void ProcessMessage(SoapMessage m){
abc("ProcessMessage " + m.Stage);
}

}

c) The proxy class generated by the WSDL utility called soapex.cs

//--------------------------------------------------------------------------
----
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.0.3705.288
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//--------------------------------------------------------------------------
----

//
// This source code was auto-generated by wsdl, Version=1.0.3705.288.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;

/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="SoapExSoap",
Namespace="http://tempuri.org/"" target="_blank">http://tempuri.org/";)]
public class SoapEx : System.Web.Services.Protocols.SoapHttpClientProtocol {

/// <remarks/>
public SoapEx() {
this.Url = "http://raid1wxp03/webservices/soapex.asmx";;
}

/// <remarks/>

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.o
rg/HelloWorld", RequestNamespace="http://tempuri.org/"" target="_blank">http://tempuri.org/";,
ResponseNamespace="http://tempuri.org/"" target="_blank">http://tempuri.org/";,
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string HelloWorld(string text) {
object[] results = this.Invoke("HelloWorld", new object[] {
text});
return ((string)(results[0]));
}

/// <remarks/>
public System.IAsyncResult BeginHelloWorld(string text,
System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("HelloWorld", new object[] {
text}, callback, asyncState);
}

/// <remarks/>
public string EndHelloWorld(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}

d)Test Client to test the Soap Extension
using System;
using System.IO;
class Test{
public static void Main(){
SoapEx myex = new SoapEx();
try{
Console.WriteLine(myex.HelloWorld("Good Day"));;
}
catch(Exception ex){
Console.WriteLine(ex.StackTrace);
}
}
}

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP]
Bharath,

With all of that detail, you would have thought that you would have put
the exception information that you get in there. =)

However, you should be aware that web services run under the local
ASP.NET account, which has a very limited permission set. I don't think (I
am not sure), that this permission set allows you to create files/write
files, etc, etc anywhere on the local machine (maybe in some areas).

If you change the default user for the web service to something that has
privledges, then that might work.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Bharath Sankaranarayan" <Click here to reveal e-mail address> wrote in message
news:eOgXxyHRCHA.2684@tkmsftngp08...
[Original message clipped]

Reply to this message...
 
    
Bharath Sankaranarayan
I think I found the issue and here is the fix.
It looks like
a) Under WinXP ASP.NET user ( anonymous user) can write to c:\windows\temp
but not to other directories.
b)The Soap Extension Class has a bug. When the processed response goes back
to the client the Content Type goes as a blank string ('') so the proxy
class complains.
To fix this you have to Add the Content Header manually like in this method.
public override void ProcessMessage(SoapMessage m){
switch(m.Stage)
{
case SoapMessageStage.BeforeSerialize:
{
m.ContentType="'text/xml'";
}
break;
}
WriteLog("ProcessMessage " + m.Stage);
return;

It does take the ASP.Net a while to notice the change before it will start
logging due to the caching.
Thanks
Bharath

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote
in message news:Om4Q7lIRCHA.4136@tkmsftngp08...
[Original message clipped]

Reply to this message...
 
    
Bharath Sankaranarayan
Sorry for posting but the error is not due to the Framework not setting the
"Content Type" but due to the fact that the framework does not thow the
correct SoapFaultString when there is an exception.
In otherwords if you had an incorrect path then the error that you get is
obscure and not informative. You will get an exception informing that the
response Content Header was ''. This is because the framework does not throw
the right exception.
So the old code with the ritght directory to write will work and you dont
have to set the ContentHeader manually
Cheers
Bharath

"Bharath Sankaranarayan" <Click here to reveal e-mail address> wrote in message
news:uPRGTiJRCHA.4168@tkmsftngp08...
[Original message clipped]

Reply to this message...
 
 
System.AsyncCallback
System.AttributeTargets
System.ComponentModel.DesignerCategoryAttribute
System.Console
System.Diagnostics.DebuggerStepThroughAttribute
System.IAsyncResult
System.IO.FileAccess
System.IO.FileMode
System.IO.FileStream
System.IO.StreamWriter
System.Runtime.Remoting.Metadata.SoapAttribute
System.Runtime.Serialization.Formatters.SoapMessage
System.Web.Services.Description.SoapBindingUse
System.Web.Services.Protocols.LogicalMethodInfo
System.Web.Services.Protocols.SoapDocumentMethodAttribute
System.Web.Services.Protocols.SoapExtension
System.Web.Services.Protocols.SoapExtensionAttribute
System.Web.Services.Protocols.SoapHttpClientProtocol
System.Web.Services.Protocols.SoapMessage
System.Web.Services.Protocols.SoapMessageStage
System.Web.Services.Protocols.SoapParameterStyle
System.Web.Services.WebService
System.Web.Services.WebServiceBindingAttribute




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com