Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

The Razor Framework :: Part 1 :: Plugins/Extensibility

Rate me:
Please Sign up or sign in to vote.
4.93/5 (127 votes)
11 Mar 2005CPOL36 min read 351.7K   1.4K   446  
An extensible dependency based plugin framework for .NET Applications.
using System;
using System.Windows.Forms;
using Razor.SnapIns;

namespace Razor
{	
	/// <summary>
	/// The Startup class contains a single static method called Main which should be used as the applications main entry point.
	/// </summary>
	public class Startup
	{
		/// <summary>
		/// The main application entry point, thru which command line arguments will be passed
		/// </summary>
		/// <param name="args">An array of strings representing any command line arguments that were passed at startup</param>
		[STAThread()]
		public static void Main(string[] args)
		{		
			bool tracedExceptionThrown = false;
			try
			{	
				// define the data paths
				string subPath = @"CodeReflection\Razor";
				
				// safely use a hosting engine configured with an additional common data path and additional local user data path
				using(SnapInHostingEngine host = new SnapInHostingEngine(subPath, subPath))
				{
					try
					{
						// run the hosting engine using the command line and the currently executing assembly (aka. the exe for the process)
						host.Run(args, System.Reflection.Assembly.GetExecutingAssembly());					
					}
					catch(System.Exception systemException)
					{
						// flag the fact that we are going to trace this exception and rethrow it
						tracedExceptionThrown = true;
						
						// give the loggers a chance to catch it if they have been successfully loaded before the 
						// host is disposed of and the logging sub system detached from the Trace and Debug output
						System.Diagnostics.Trace.WriteLine(systemException);
						
						// rethrow the exception so that it may be displayed for the user
						throw systemException;
					}
				}		
			}
			catch(System.Exception systemException)
			{
				// if the exception hasn't already been traced
				if (!tracedExceptionThrown)
				{
					tracedExceptionThrown = true;
					// trace it now
					System.Diagnostics.Trace.WriteLine(systemException);
				}
				
				// also, since it's more likely we'll not have a debugger attached, display the exception to the user
				string info = string.Format("The following exception was thrown by '{0}'.\n\nPlease refer to the log files for further information.\n\n{1}", Application.ProductName, systemException.ToString());

				System.Windows.Forms.MessageBox.Show(null, info, "Application Exception");

				// exit the current thread to force safe application shutdown
				Application.ExitThread();
			}
			finally
			{
				// one final trace to let everyone know we have shutdown completely
				System.Diagnostics.Trace.WriteLine("'" + Application.ProductName + "' has " + (tracedExceptionThrown ? "terminated because of an exception." : "exited gracefully."));
			}
		}
	}
}

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
Software Developer (Senior)
United States United States
Senior Application Developer specializing in Windows desktop and network development.

Professional Experience
- B.S. of Computer Science (Graduated 2001 - PSU)
- Senior Application Developer (8+ yrs)
- Microsoft Certified Professional

Primary Interests
- C#, C++, HTML, Javascript
- XML, ASP.NET, Web Services, SOAP, UDDI
- Socket programming and anything network related
- Reflection, Serialization, and Plugin Frameworks
- Owner-drawn controls and GDI+ goodness

Comments and Discussions