Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / August 2004

Tip: Looking for answers? Try searching our database.

WSE 2.0 and structured Exceptionhandling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henning Krause - 20 Aug 2004 08:38 GMT
Hello,

I've built a soap.tcp based webservice using WSE 2.0, which runs quite
smoothly.

When I throw an exception on the server side, the exception in propagated to
the client as SoapException.

If I turn "detailedErrors" on, I get the message of the original exception
somewhere in the message of the SoapException.

But the innerException is empty, so I cannot access the original exception.

Here is the declaration of my Exception:
[Serializable]
[XmlRoot(Namespace=http://nospamproxy.netatwork.de/WebServices/ProxyConfigur
ation/ConfigurationException)]
public class CustomException: Exception
{
   public CustomException(string message): base(message) {}
   public CustomException(SerializationInfo info, StreamingContext
context):base(info, context) {}
   public CustomException(string message, Exception innerException):
base(message, innerException) {}

}

In the client app, I've included a reference to the assembly where the above
exception is declared.

Is it possible to get full exception handling with WSE 2.0 or do I have to
go back to the bad old days and return status-codes from my webservice?

Any help would be appreciated.

Greegings,
Henning Krause
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/?page=products)
William Stacey [MVP] - 20 Aug 2004 17:41 GMT
I have the same question Henning.  Not sure detailedErrors what right
either.  Maybe all your reply documents have a shared enum at the top for
return status and also share the exceptions so you can just "switch" the
enum (or if it) and throw the exception locally - not sure.

Signature

William Stacey, MVP

> Hello,
>
[quoted text clipped - 11 lines]
> Here is the declaration of my Exception:
> [Serializable]

[XmlRoot(Namespace=http://nospamproxy.netatwork.de/WebServices/ProxyConfigur
> ation/ConfigurationException)]
> public class CustomException: Exception
[quoted text clipped - 21 lines]
> Try my free Exchange Explorer: Mistaya
> (http://www.infinitec.de/?page=products)
Henning Krause - 23 Aug 2004 09:26 GMT
Hello,

I've solved the problem this way:

On the server side, all Webservice inherit from a base webservice class
which in turn inherits from SoapService.

In this class, I override the Receive(SoapEnvelope) method:

protected override void Receive(SoapEnvelope envelope)
{
   try
       {
           base.Receive(envelope);
       }
       catch (Exception ex)
       {
           throw new SoapException(ex.Message,
SoapException.ClientFaultCode, "", WrapException(ex.InnerException));
       }
   }
}

The WrapException method takes the type and all public properties (via
reflection) and creates an XmlDocument with these informations.

This Document is then passed along to the SoapException. The XmlDocument is
now available on the client via the SoapException.Details property.

On the client, I've created a WebServiceInvokationException which unwraps
the SoapException and makes the public properties of the original excption
available via a NameValue Collection.

Greetings,
Henning Krause
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/?page=products)

> I have the same question Henning.  Not sure detailedErrors what right
> either.  Maybe all your reply documents have a shared enum at the top for
[quoted text clipped - 18 lines]
> > Here is the declaration of my Exception:
> > [Serializable]

[XmlRoot(Namespace=http://nospamproxy.netatwork.de/WebServices/ProxyConfigur
> > ation/ConfigurationException)]
> > public class CustomException: Exception
[quoted text clipped - 22 lines]
> > Try my free Exchange Explorer: Mistaya
> > (http://www.infinitec.de/?page=products)
William Stacey [MVP] - 23 Aug 2004 13:51 GMT
Thanks Henning.  Can I see WrapException and the client side unwrap
method(s)?  TIA

Signature

William Stacey, MVP

> Hello,
>
[quoted text clipped - 60 lines]
> > > Here is the declaration of my Exception:
> > > [Serializable]

[XmlRoot(Namespace=http://nospamproxy.netatwork.de/WebServices/ProxyConfigur
> > > ation/ConfigurationException)]
> > > public class CustomException: Exception
[quoted text clipped - 23 lines]
> > > Try my free Exchange Explorer: Mistaya
> > > (http://www.infinitec.de/?page=products)
Henning Krause - 23 Aug 2004 14:45 GMT
Hello,

here we go:
============================================================================
WebserviceInvokationErrorException.cs:
============================================================================

public class WebServiceInvokationException: Exception, IEnumerable
{
 private NameValueCollection _Details;
 private string _Type;
 private string _Message;

 public string this[string name] { get { return _Details[name]; } }
 public string this[int index] {get { return _Details[index]; } }
 public string Type { get { return _Type; } }

 private WebServiceInvokationException(): base() {}
 private WebServiceInvokationException(string message, Exception
innerException): base(message, innerException) {}

 public static WebServiceInvokationException UnWrap(SoapException
innerException)
 {
  NameValueCollection details = new NameValueCollection();
  string type;
  string message;
  WebServiceInvokationException exception;

  if (innerException == null) throw new ArgumentException("innerException
must not be null", "innerException");

  if (innerException.Detail == null) throw new ArgumentException("Soap
exception does not contain any usable information", "innerException");

  message = "";
  type = "";
  foreach (XmlElement node in innerException.Detail.ChildNodes)
  {
   if (string.Compare(node.LocalName, "Type", true) == 0) type =
node.InnerText;
   else if (string.Compare(node.LocalName, "Message", true) == 0) message =
node.InnerText;
   else details.Add(node.LocalName, node.InnerText);
  }

  exception = new WebServiceInvokationException(message, innerException);
  exception._Details = details;
  exception._Type = type;

  return exception;
 }

 #region IEnumerable Members

 public IEnumerator GetEnumerator()
 {
  return _Details.GetEnumerator();
 }

 #endregion

============================================================================
ServiceBase.cs
============================================================================

using System;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Messaging;
using System.Web.Services.Protocols;
using System.Xml;
using System.Reflection;

using NAW.NoSpamProxy.WebServices;

namespace NAW.NoSpamProxy.WebServices.Implementation
{
/// <summary>
/// Summary description for ServiceBase.
/// </summary>
public abstract class ServiceBase: SoapService
{
 private void AddInfo(XmlElement parent, string name, string info)
 {
  XmlElement node;

  node = parent.OwnerDocument.CreateElement(name);
  node.AppendChild(parent.OwnerDocument.CreateTextNode(info));
  parent.AppendChild(node);
 }

 public XmlElement WrapException(Exception ex)
 {
  XmlDocument doc;
  XmlElement node;
  object value;

  doc = new XmlDocument();

  // Build the detail element of the SOAP fault.
  node = (XmlElement)
doc.CreateElement(SoapException.DetailElementName.Name,
SoapException.DetailElementName.Namespace);

  AddInfo(node, "Type", ex.GetType().FullName);

  foreach (PropertyInfo info in
ex.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
  {
   try
   {
    value = info.GetValue(ex, null).ToString();
    AddInfo(node, info.Name, (value != null) ? value.ToString() : "");
   }
   catch
   {
    AddInfo(node, info.Name, "");
   }
  }
  return node;
 }
 protected override void Receive(SoapEnvelope envelope)
 {
  try
  {
   base.Receive(envelope);
  }

  catch (Exception ex)
  {
   throw new SoapException(ex.Message, SoapException.ClientFaultCode, "",
WrapException(ex.InnerException));
  }
 }

}
}

============================================================================

Each webservice call from the client is wrapped like this:
============================================================================

 try
 {
  return (ProxyConfiguration)base.SendRequestResponse("MyMethod", new
SoapEnvelope()).GetBodyObject( typeof(MyMethodResponse));
 }
 catch (SoapException ex)
 {
  throw WebServiceInvokationException.UnWrap(ex);
 }

Greetings,
Henning Krause
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/?page=products)

> Thanks Henning.  Can I see WrapException and the client side unwrap
> method(s)?  TIA
[quoted text clipped - 65 lines]
> > > > Here is the declaration of my Exception:
> > > > [Serializable]

[XmlRoot(Namespace=http://nospamproxy.netatwork.de/WebServices/ProxyConfigur
> > > > ation/ConfigurationException)]
> > > > public class CustomException: Exception
[quoted text clipped - 25 lines]
> > > > Try my free Exchange Explorer: Mistaya
> > > > (http://www.infinitec.de/?page=products)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.