2 using System.Reflection;
3 using System.Threading;
5 namespace UnhandledExceptionTest
7 class TestConfiguration {
8 private bool useDifferentThread = false;
11 return useDifferentThread;
14 private bool useDifferentApplicationDomain = false;
17 return useDifferentApplicationDomain;
20 private bool differentConfigurationIsLegacy;
23 return differentConfigurationIsLegacy;
26 private bool useDifferentThreadInDifferentApplicationDomain = false;
29 return useDifferentThreadInDifferentApplicationDomain;
32 private bool addHandlerToRootApplicationDomain = false;
35 return addHandlerToRootApplicationDomain;
38 private bool addHandlerToDifferentApplicationDomain = false;
41 return addHandlerToDifferentApplicationDomain;
45 private static bool ParseArgumentValue (string value) {
46 if ((value.Length == 1)) {
53 Console.WriteLine ("Invalid argument value {0}", value);
54 throw new ApplicationException ("Invalid argument value " + value);
57 Console.WriteLine ("Invalid argument value {0}", value);
58 throw new ApplicationException ("Invalid argument value " + value);
62 public TestConfiguration (string configuration) {
63 string [] arguments = configuration.Split (',');
64 foreach (string argument in arguments) {
65 string [] components = argument.Split (':');
66 if (components.Length == 2) {
67 switch (components [0]) {
69 useDifferentThread = ParseArgumentValue (components [1]);
72 useDifferentApplicationDomain = ParseArgumentValue (components [1]);
75 differentConfigurationIsLegacy = ParseArgumentValue (components [1]);
78 useDifferentThreadInDifferentApplicationDomain = ParseArgumentValue (components [1]);
81 addHandlerToRootApplicationDomain = ParseArgumentValue (components [1]);
84 addHandlerToDifferentApplicationDomain = ParseArgumentValue (components [1]);
87 Console.WriteLine ("Invalid argument {0}", argument);
88 throw new ApplicationException ("Invalid argument " + argument);
91 Console.WriteLine ("Invalid argument {0}", argument);
92 throw new ApplicationException ("Invalid argument " + argument);
96 private static string BoolToString (bool value) {
97 return value ? "T" : "F";
99 public void Print () {
100 Console.WriteLine ("Configuration: DT={0},DA={1},DCIL={2},DTDA={3},HRA={4},HDA={5}",
101 BoolToString (useDifferentThread),
102 BoolToString (useDifferentApplicationDomain),
103 BoolToString (differentConfigurationIsLegacy),
104 BoolToString (useDifferentThreadInDifferentApplicationDomain),
105 BoolToString (addHandlerToRootApplicationDomain),
106 BoolToString (addHandlerToDifferentApplicationDomain));
111 private string configurationDescription;
112 private TestConfiguration configuration;
113 public Test (string configurationDescription) {
114 this.configurationDescription = configurationDescription;
115 this.configuration = new TestConfiguration (configurationDescription);
118 private AppDomain CreateDiffrentAppDomain () {
119 AppDomainSetup ads = new AppDomainSetup();
120 ads.ApplicationBase = System.Environment.CurrentDirectory;
121 ads.DisallowBindingRedirects = false;
122 ads.DisallowCodeDownload = true;
123 ads.ConfigurationFile = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar +
124 (configuration.DCIL ? "unhandled-exception-legacy-configuration.config" : "unhandled-exception-base-configuration.config");
125 return AppDomain.CreateDomain("DifferentAppDomain", null, ads);
128 public void RunTest () {
129 if (configuration.DA) {
130 AppDomain differentAppDomain = CreateDiffrentAppDomain ();
131 if (configuration.HDA) {
132 differentAppDomain.UnhandledException += new UnhandledExceptionEventHandler (DifferentDomainUnhandledExceptionHandler);
134 DifferentDomainActor dda = (DifferentDomainActor) differentAppDomain.CreateInstanceAndUnwrap (
135 Assembly.GetEntryAssembly().FullName, typeof (DifferentDomainActor).FullName);
136 dda.Act (configurationDescription);
138 if (configuration.DT) {
139 Console.WriteLine ("Throwing ApplicationException in different thread");
141 Console.WriteLine ("Throwing ApplicationException in main thread");
143 throw new ApplicationException ("This exception is unhandled");
145 if (configuration.DT) {
146 Console.WriteLine ("Continuing in different thread after the exception was thrown");
150 static void Main (string [] args) {
151 if (args.Length != 1) {
152 Console.WriteLine ("Invalid arguments (number of) {0}", args.Length);
153 throw new ApplicationException ("Invalid arguments (number of) " + args.Length);
155 Test test = new Test (args [0]);
159 configuration.Print ();
160 Console.WriteLine ("Running under version {0}", Environment.Version);
162 if (configuration.HRA) {
163 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (RootDomainUnhandledExceptionHandler);
166 if (configuration.DT) {
167 Thread thread = new Thread (new ThreadStart (this.RunTest));
174 Console.WriteLine ("Continuing in main thread after the exception was thrown");
175 Console.WriteLine ("Continuing in root AppDomain after the exception was thrown");
176 Console.WriteLine ("MARKER-CONT");
179 public static void PrintUnhandledException (string caller, object sender, UnhandledExceptionEventArgs e) {
180 Exception ex = (Exception)e.ExceptionObject;
182 Console.WriteLine ("Running {0}", caller);
183 Console.WriteLine ("Handling exception type {0}", ex.GetType().Name);
184 Console.WriteLine ("Message is {0}", ex.Message);
185 Console.WriteLine ("IsTerminating is set to {0}", e.IsTerminating);
187 public static void RootDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
188 Console.WriteLine ("MARKER-RDUE");
189 PrintUnhandledException ("RootDomainUnhandledExceptionHandler", sender, e);
191 public static void DifferentDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
192 Console.WriteLine ("MARKER-DDUE");
193 PrintUnhandledException ("DifferentDomainUnhandledExceptionHandler", sender, e);
197 public class DifferentDomainActor : MarshalByRefObject {
198 //private string configurationDescription = null;
199 private TestConfiguration configuration = null;
201 public void RunTest () {
202 if (configuration.DTDA) {
203 Console.WriteLine ("Throwing ApplicationException in new thread (different appdomain)");
204 } else if (configuration.DT) {
205 Console.WriteLine ("Throwing ApplicationException in different thread (different appdomain)");
207 Console.WriteLine ("Throwing ApplicationException in main thread (different appdomain)");
209 throw new ApplicationException ("This exception is unhandled");
212 // Call this method via a proxy.
213 public void Act (string configurationDescription) {
214 //this.configurationDescription = configurationDescription;
215 this.configuration = new TestConfiguration (configurationDescription);
217 if (configuration.DTDA) {
218 Thread thread = new Thread (new ThreadStart (this.RunTest));