Click here to Skip to main content
15,896,201 members
Articles / Desktop Programming / Windows Forms

Windows Services Made Simple

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
27 Jun 2007CPOL10 min read 94.5K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
using System;
using System.Reflection;
using System.Text;

using Pegasus.Diagnostics;

namespace Pegasus
{
	/// <summary>
	/// Summary description for ExceptionHelper.
	/// </summary>
	public class ExceptionHelper
	{
		/// <summary>
		/// Determines whether the exception is serializable or not.  This method scans not only the 
		/// exception passed in but all inner exceptions as well.
		/// </summary>
		/// <param name="e">The exception to check.</param>
		/// <returns>
		/// 	<c>true</c> if the exception is serializable; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsExceptionSerializable( Exception e )
		{
			ParamCode.AssertNotNull( e, "e" );

			Exception ex = e;
			while( ex != null )
			{
				if( !ex.GetType().IsSerializable )
				{
					return false;
				}

				ex = ex.InnerException;
			}

			return true;
		}

		/// <summary>
		/// Build the exception string by revieing the public the object properties.
		/// For not only this exception but for all inner exceptions as well.
		/// </summary>
		/// <param name="e">The exception to dump.</param>
		/// <returns>String dump of the exception</returns>
		public static string DumpExceptionAll( Exception e )
		{
			// Check Params
			ParamCode.AssertNotNull( e, "e" );

			Exception ex = e;
			StringBuilder ret = new StringBuilder();

			// Loop through all the inner exceptions
			while( ex != null )
			{
				ret.Append( DumpException( e ) );
				ex = ex.InnerException;
			}

			return ret.ToString();
		}

		/// <summary>
		/// Build the exception string by revieing the public the object properties.
		/// </summary>
		/// <param name="e">The exception to dump.</param>
		/// <returns>String dump of the exception</returns>
		public static string DumpException( Exception e )
		{
			// Check Params
			ParamCode.AssertNotNull( e, "e" );

			StringBuilder ret = new StringBuilder();

			// Get the public properties for this exception
			Type type = e.GetType();

			// Get all the public properties for this exception.
			PropertyInfo[] properties = type.GetProperties( BindingFlags.Instance | BindingFlags.Public );
			//DebugCode.AssertNotNull( properties );

			ret.AppendFormat( "{0}\n", type.Name );
			ret.AppendFormat( "  Type: {0}\n", type.FullName );

			if( properties.Length > 0 )
			{
				foreach( PropertyInfo property in properties )
				{
					ret.AppendFormat( "  {0}: {1}\n", property.Name, property.GetValue( e, null ) ); 
				}
			}
			else
			{
				ret.Append( "  No public properties for this exception\n" );
			}

			ret.Append( "\n" );
			return ret.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions