Exceptions thrown by Web Services

  • Thread starter Thread starter Water Cooler v2
  • Start date Start date
W

Water Cooler v2

When a Web Service throws an exception to the caller/client, the
exception is always cast to SoapException regardless of the actual type
of the exception thrown. In such a scenario, how would you think it is
appropriate for a Web services client to determine the exact type of
exception?


Consider this example.


CLIENT
class Client
{
[STAThread]
static void Main(string[] args)
{
using(localhost.ExceptionThrowerService ets = new
ExceptionCatcher.localhost.ExceptionThrowerService())
{

try
{
ets.throwCustomException();
}
catch(System.Exception e)
{
/*How would you determine here whether the type of e is
MyCustomException or some other one
Given that MyCustomException is given inside the Web service.*/
Console.WriteLine(e.GetType().ToString());
}
Console.ReadLine();
}
}
}



















SERVER
namespace ExceptionThrower
{
public class ExceptionThrowerService : System.Web.Services.WebService
{

[WebMethod]
public void throwCustomException()
{
Trace.WriteLine("Throwing custom exception now.");
throw new MyCustomException();
}
} //End of class ExceptionThrower


public class MyCustomException: System.Exception
{
private string mDetail = "I am the best guy.";

public MyCustomException()
{
mDetail = "I am *still* the best guy.";
}

public MyCustomException(string lDetail)
{
this.Detail += ("\n" + lDetail);
}

public string Detail
{
get
{
return mDetail;
}
set
{
mDetail = value;
Trace.WriteLine("Setting the value of mDetail...");
}
}

}//End of class MyCustomException

}
 
Hi,

Have you tried the InnerException property? I am not sure this gonna work,
but why wouldn't you give it a try?
 
Hi!

Thanks for your reply. I'd already tried the InnerException property
but it is null. Also, the exception recieved by a Web services client
is always invariably recieved as an object of the SoapException class
regardless of its actual type/class.
 
Wouldn't the SoapException.InnerException contain the details you're
looking for?

If you're working with your own custom Exceptions, make sure to mark
them Serializable and implement the protected Exception(
SerializationContext, SerializationInfo ) constructror (just call the
base constructor..)

HTH
Andy
 
Hi Andy,

I've done what you said, but it doesn't get accross the boundaries yet.
Did you mean this?


SERVER
protected
MyCustomException(System.Runtime.Serialization.SerializationInfo si,
System.Runtime.Serialization.StreamingContext sc): base(si, sc)
{
//nothing
}



CLIENT
try
{
ets.throwCustomException();
}
catch(System.Exception e)
{
if (e.InnerException != null)
Console.WriteLine(e.InnerException.ToString());
else
Console.WriteLine("e.InnerException is null");
}
 
Back
Top