Home All Groups Group Topic Archive Search About

Exceptions thrown by Web Services

Author
24 Feb 2006 11:46 AM
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

}

Author
24 Feb 2006 12:14 PM
Dmytro Lapshyn [MVP]
Hi,

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

Show quote
"Water Cooler v2" <wtr_***@yahoo.com> wrote in message
news:1140781580.529097.43020@u72g2000cwu.googlegroups.com...
> 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
>
> }
>
Author
24 Feb 2006 2:00 PM
Water Cooler v2
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.
Author
24 Feb 2006 3:29 PM
Andy
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
Author
25 Feb 2006 5:58 AM
Water Cooler v2
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");
                }

AddThis Social Bookmark Button