second AppDomain and trouble catching exception to restart second domain

L

Lothar Behrens

Hi,

I have created a plugin system that put each plugin into a separate
domain (monitoring stuff). A test with the following code shows up
that I catch the exception from a thread in the second domain, but
after the handler the application still keeps stopping.

I even can stop and restart the services, but after that the
application reports about the exception.

Why?

Thanks, Lothar

public bool startupServives(string pluginManagerModule, string
pluginManagerClass)
{
// Service 0
AppDomain appDomainPlugin =
AppDomain.CreateDomain("PluginDomain-" + plm.PluginModule + "-" +
plm.PluginClass);

appDomainPlugin.UnhandledException +=
OnUnhandledException;

IMonitoringPlugin monitoringPlugin =
(IMonitoringPlugin)appDomainPlugin.CreateInstanceAndUnwrap(
"MyPlugin",
"MyPluginNS.Plugin1",
true, // ignoreCase
0, // bindingAttr
null, // binder
null, // args
null, // culture
null, // activationAttributes
null // securityAttributes
);

monitoringPlugin.Initialize();
runningServices.Add(monitoringPlugin);
runningDomains.Add(appDomainPlugin);

// Service 1
// ...

monitoringPlugin1.Initialize();
runningServices.Add(monitoringPlugin1);
runningDomains.Add(appDomainPlugin1);
}

public bool shutdownServices()
{
if (runningServices == null) return true;
if (runningDomains == null) return true;

foreach (IMonitoringPlugin pl in runningServices)
{
pl.Dispose();
}

runningServices = null;

foreach (AppDomain ap in runningDomains)
{
AppDomain.Unload(ap);
}

runningDomains = null;

GC.Collect();

return true;
}

// Handler gets called upon timed out thread in second domain
void OnUnhandledException(object sender,
UnhandledExceptionEventArgs eventArgs)
{
shutdownServices();
startupServives(_pluginManagerModule,
_pluginManagerClass);
}

The plugin that tests the exception in thread issue:

public class PluginExceptionSample : MarshalByRefObject,
IMonitoringPlugin, IDisposable
{
public string Name
{
get
{
return "PluginExceptionSample";
}
}

public string Description
{
get
{
return "Sample plugin exception sample";
}
}

public string Author
{
get
{
return "Lothar Behrens";
}
}

public string Version
{
get
{
return "1.0.0.0";
}
}

public void Initialize()
{
System.Console.WriteLine("Plugin " + Name + ", " + Version
+ ", " + Author + " initialized.");
System.Console.Beep();

ExceptionThread = new Thread(ExceptionAction);

ExceptionThread.Start();
}

public void Dispose()
{
System.Console.WriteLine("Plugin " + Name + ", " + Version
+ ", " + Author + " disposing.");
System.Console.Beep();

}

void ExceptionAction()
{
Thread.Sleep(5000);
throw new Exception("ExceptionAction() throwed and
exception.");
}

Thread ExceptionThread;
}
 
L

Lothar Behrens

Lothar said:
I have created a plugin system that put each plugin into a separate
domain (monitoring stuff). A test with the following code shows up
that I catch the exception from a thread in the second domain, but
after the handler the application still keeps stopping. [...]

There's no handler at all.  You've subscribed to the UnhandledException
event, not handled an exception.  By definition, any exception that you
receive notification for in that event handler is unhandled.

You have to catch the event in the AppDomain itself, with a try/catch block.

Pete

Yes, the sample thread function has to be wrapped. Then I am able to
catch exceptions in the wrapped thread method.
So the later plugins in my 'architecture' have to implement an
abstract thread function the wrapper calls.

Then the server will not crash upon one plugin and therefore does not
stop other plugins working. It's only a matter to not starting
a plugin again if it repeadetly crashes.

Thats what I want - in general.

Lothar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top