473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

AppDomains and Exceptions

I'm writing a library to provide plugin capability to my applications. It
does this by loading DLL's into a new AppDomain for each plugin that is
loaded. Now obviously when I write a plugin, I can make sure that my
plugins don't throw any exceptions. But I certainly can't guarantee that
other people writing plugins won't throw an exception. The problem is that
if one of these other plugins throws an exception, it brings down the
entire application.

Is there anything I can do to prevent Exceptions in other AppDomains from
bringing down the entire app, when I don't own the code that is running in
that AppDomain? I thought I would be able to use
AppDomain.UnhandledException, but as is pointed out in the link below, this
is only a "notification" not a "handler".

UnhandledException is not a handler: (watch the wrap)
http://lab.msdn.microsoft.com/Produc...Feedback.aspx?
feedbackId=FDBK21092

-mdb
Mar 18 '06 #1
8 3750

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
I'm writing a library to provide plugin capability to my applications. It
does this by loading DLL's into a new AppDomain for each plugin that is
loaded. Now obviously when I write a plugin, I can make sure that my
plugins don't throw any exceptions. But I certainly can't guarantee that
other people writing plugins won't throw an exception. The problem is
that
if one of these other plugins throws an exception, it brings down the
entire application.

Is there anything I can do to prevent Exceptions in other AppDomains from
bringing down the entire app, when I don't own the code that is running in
that AppDomain? I thought I would be able to use
AppDomain.UnhandledException, but as is pointed out in the link below,
this
is only a "notification" not a "handler".


You might be able to get around it by using a bit of code in the appdomain
that can catch and gracefully handle exceptions, just place a little code
between the plugin client and the client itself that handles that.
Mar 18 '06 #2
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
news:#1*************@TK2MSFTNGP12.phx.gbl:
You might be able to get around it by using a bit of code in the
appdomain that can catch and gracefully handle exceptions, just place
a little code between the plugin client and the client itself that
handles that.


Hi Daniel,

I figured someone might answer with this, but I was hoping that there
would be a more straightforward answer...

The problem is that I (the hosting application) never call the plugin
code. I pass it a pointer to services that the hosting application
provides, like this:

interface IPluginHost
{
void SetHostTitle(string text);
}

interface IPlugin
{
Initialize(IPluginHost host);
}

public class MyPlugin : IPlugin
{
public void Initialize(IPluginHost host)
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(this.DoPluginWork)
);
}

public void DoPluginWork(object state)
{
Thread.Sleep(10000);
host.SetTitle("blah");

// This kills the entire application
throw new Exception();
}
}

public class Form1 : Form, IPluginHost
{
// normal windows form stuff
public void SetHostTitle(string text)
{
// In my host app, this is Invoked to the UI thread
this.Text = text;
}

public Form1()
{
InitializeComponent();
IPlugin myPlugin = new MyPlugin(this);
}
}

Obviously things are more complicated than I show here, but you get the
idea.... Now if I was calling IPlugin functions from my host, then I
could wrap them in a try/catch, but as you can see, that's not the way I
want to do it. I want the plugins to be in charge of their own destiny,
and not have to wait on a 'polling' mechanism from the host to affect
changes in the host.

As you can see, I have no way of putting a try/catch around the work
that the client does from the code in Form1. Of course I could put it
around the code INSIDE DoPluginWork, but that's something I can't
control if other programmers are writing an IPlugin for my application.

Thus I'm looking for some way to prevent THEIR bad programming from
destroying my entire application. I thought that AppDomains would do
this, but that's not what my experience is so far - an exception thrown
in another AppDomain still kills the entire application.

-mdb
Mar 18 '06 #3
"Vadym Stetsyak" <va*****@ukr.net> wrote in
news:##**************@TK2MSFTNGP11.phx.gbl:
Hello, Michael!

You can establish a proxy object that will load plugins on the app
domain, and will start it own thread method there, where you can
place try/catch block. This will give you the possibility to catch
all exceptions from plugin worker...


Yes. That's exactly what I do. But Exceptions thrown in the proxy cause
my application to fail. That's what I'm trying to prevent.

-mdb
Mar 18 '06 #4
"Vadym Stetsyak" <va*****@ukr.net> wrote in
news:##**************@TK2MSFTNGP11.phx.gbl:
Hello, Michael!

You can establish a proxy object that will load plugins on the app
domain, and will start it own thread method there, where you can
place try/catch block. This will give you the possibility to catch
all exceptions from plugin worker...


Sorry I didn't quite understand what you said, so my previous response was
a bit off... Your solution still suffers from the problem that I have to
rely on the programmer of the plugin to use my Delegate and the
PluginWorkProc function. There's nothing that forces them to do this.

What I'm looking for is a generic way to HANDLE (not just receive
notification of) Exceptions that are thrown in an AppDomain so that the
entire application doesn't die.

It's looking more and more like this can't be done. Disappointing.

-mdb
Mar 18 '06 #5
"Vadym Stetsyak" <va*****@ukr.net> wrote in
news:u6**************@TK2MSFTNGP14.phx.gbl:
From MSDN:
"The UnhandledExceptionEventHandler delegate for this event provides
default handling for uncaught exceptions. When this event is not
handled, the system default handler reports the exception to the user
and terminates the application. This event occurs only for the
application domain that is created by the system when an application
is started. If an application creates additional application domains,
specifying a delegate for this event in those applications domains has
no effect."

Didn't test it myself, but if you will subscribe to the
UnhandledExceptionEventHandler of main application domain?
Theoretically this should prevent main app domain from exiting....


Yes. In fact, I've even go one step further (or two) by setting the
'SetUnhandledExceptionMode'. These linese are in my Main(...) before
anything else occurs.

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler
(Application_ThreadException);

Application.SetUnhandledExceptionMode
(UnhandledExceptionMode.CatchException, true);

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);
-mdb
Mar 18 '06 #6
Hello Michael,
Are you absolutely sure you need to open a new AppDomain? Could you load
those plagins just in designated threads? I think this will solve the problem
of killing the application (although I don't know how many other problems
this could bring... ;)

Vadik

I'm writing a library to provide plugin capability to my applications.
It does this by loading DLL's into a new AppDomain for each plugin
that is loaded. Now obviously when I write a plugin, I can make sure
that my plugins don't throw any exceptions. But I certainly can't
guarantee that other people writing plugins won't throw an exception.
The problem is that if one of these other plugins throws an exception,
it brings down the entire application.

Is there anything I can do to prevent Exceptions in other AppDomains
from bringing down the entire app, when I don't own the code that is
running in that AppDomain? I thought I would be able to use
AppDomain.UnhandledException, but as is pointed out in the link below,
this is only a "notification" not a "handler".

UnhandledException is not a handler: (watch the wrap)
http://lab.msdn.microsoft.com/Produc...Feedback.aspx?
feedbackId=FDBK21092

-mdb

Mar 18 '06 #7
Vadik Vaksin <vj******@walla.co.il> wrote in
news:44*************************@msnews.microsoft. com:
Are you absolutely sure you need to open a new AppDomain? Could you
load those plagins just in designated threads? I think this will solve
the problem of killing the application (although I don't know how many
other problems this could bring... ;)


My overall design calls for the ability to dynamially load AND unload
plugins, for which I need AppDomains.

Actually I can boil my requirements down to three things:

1. Ability to dynamically load/unload plugins

2. Plugins make function calls to the host application (not the other way
around)

3. Exceptions thrown in the plugin don't kill the app. Preferably, I
should be able to know about the exception so I can shut down that plugin.

There are plenty of examples out there that show how to dynamically load
plugins, but very few do it in AppDomains, so they can't unload them (in
the sense of the DLL being removed from memory).

Here's another option I would consider: If there is a Code Access Security
setting that I could use to specify that the plugin cannot create new
threads, then I would create a new thread for it and run a plugin function
in a try/catch. Anyone know if such a CAS setting exists?

-mdb
Mar 19 '06 #8
Michael,

I'm not sure this will work in your application but this is how its work for
csUnit (http://www.csunit.org) :

here is how the test is executed:
public void RunTests(ITestSpec testSpec) {
try {
...
LoadAssembly();
...
RunTests(testSpec);
}
catch(AppDomainUnloadedException) {
...
}
}
Of course, the RunTest function could crash. This function could also open
another threads (at least we are doing this)

Here is the LoadAssembly function:
LoadAssembly() {
FileInfo fi = new FileInfo(_assemblyPathName);
...
String applicationDomainName = AppDomain.CurrentDomain.FriendlyName
+ ":TestExecutor-" + ++_loaderCount;
AppDomainSetup setup = new AppDomainSetup();

setup.ApplicationBase = fi.DirectoryName;
setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory;
setup.ApplicationName = fi.Name;
setup.ShadowCopyFiles = "true";
setup.ShadowCopyDirectories = fi.DirectoryName;

setup.ConfigurationFile = fi.Name + ".config";

_appDomain = AppDomain.CreateDomain(applicationDomainName, null,
setup);
...
try {
_remoteLoader = (RemoteLoader)
_appDomain.CreateInstanceFromAndUnwrap(
//csUnitCorePathFileName, remoteLoaderFullTypeName);
csUnitDll, remoteLoaderFullTypeName);
}
catch(Exception e) {
...
return null;
}

...
_assemblyFullName =
_remoteLoader.LoadAssembly(this._assemblyPathName, assemblyName, this);

...
}
--
Vadik
"Michael Bray" wrote:
Vadik Vaksin <vj******@walla.co.il> wrote in
news:44*************************@msnews.microsoft. com:
Are you absolutely sure you need to open a new AppDomain? Could you
load those plagins just in designated threads? I think this will solve
the problem of killing the application (although I don't know how many
other problems this could bring... ;)


My overall design calls for the ability to dynamially load AND unload
plugins, for which I need AppDomains.

Actually I can boil my requirements down to three things:

1. Ability to dynamically load/unload plugins

2. Plugins make function calls to the host application (not the other way
around)

3. Exceptions thrown in the plugin don't kill the app. Preferably, I
should be able to know about the exception so I can shut down that plugin.

There are plenty of examples out there that show how to dynamically load
plugins, but very few do it in AppDomains, so they can't unload them (in
the sense of the DLL being removed from memory).

Here's another option I would consider: If there is a Code Access Security
setting that I could use to specify that the plugin cannot create new
threads, then I would create a new thread for it and run a plugin function
in a try/catch. Anyone know if such a CAS setting exists?

-mdb

Mar 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daylor | last post by:
hi. i have mult thread vb.net application. it has 30 threads. each thread serves 1 phone caller. the question : is there a reason , to create appdomain for each Phone Service ? meaning 30...
4
by: Mountain Bikn' Guy | last post by:
I need some advice on this. I am working on a fairly complex calculator app (C#) with lots of functions (and these functions in turn use math functions from an unmanaged C DLL). A calculation takes...
0
by: Brian Takita | last post by:
Hello, I'm getting the following error at the end of this message when trying to run the ReportManager and the ReportServer: Assembly system.data.dll security permission grant set is...
1
by: billr | last post by:
hi there, I hope that someone will be able to shed some light on little old confused me. We are developing an application which will be deployed onto a Terminal Server machine. The application...
8
by: Fred Mertz | last post by:
I'm working towards an MCTS cert and I'm having to learn a bunch of stuff that I doubt I'd stumble across on my own. One such feature of .NET is AppDomains; programatically creating new AppDomains...
0
by: kayakyakr | last post by:
I'm working with a set of experiments in a large system that is using AppDomains for isolation and wants to make a call out to an umanaged dll. The test dll has two methods, SetNumber(int num) and...
3
by: | last post by:
If this is simple, forgive my ignorance, but I'm coming from the CompactFramework where we don't use AppDomains. I did a fair bit of archive searching and couldn't find an answer and I got no...
0
by: jeremyje | last post by:
I would like to create an application where I have many concurrent processes being managed by a monitoring process. Each process that is "managed" will be invoked from an assembly dll (think...
0
by: PRR | last post by:
Here is a code i found on "how to enumerate appdomains in a current process". The original code was posted by Thomas Scheidegger Add the following as a COM reference -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.