473,781 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Unhan dledException, but as is pointed out in the link below, this
is only a "notificati on" not a "handler".

UnhandledExcept ion is not a handler: (watch the wrap)
http://lab.msdn.microsoft.com/Produc...Feedback.aspx?
feedbackId=FDBK 21092

-mdb
Mar 18 '06 #1
8 3785

"Michael Bray" <mbray@makeDInt oDot_ctiusaDcom > wrote in message
news:Xn******** *************** *****@207.46.24 8.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.Unhan dledException, but as is pointed out in the link below,
this
is only a "notificati on" 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******** *****@TK2MSFTNG P12.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(st ring text);
}

interface IPlugin
{
Initialize(IPlu ginHost host);
}

public class MyPlugin : IPlugin
{
public void Initialize(IPlu ginHost host)
{
ThreadPool.Queu eUserWorkItem(
new WaitCallback(th is.DoPluginWork )
);
}

public void DoPluginWork(ob ject state)
{
Thread.Sleep(10 000);
host.SetTitle(" blah");

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

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

public Form1()
{
InitializeCompo nent();
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.ne t> wrote in
news:##******** ******@TK2MSFTN GP11.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.ne t> wrote in
news:##******** ******@TK2MSFTN GP11.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.ne t> wrote in
news:u6******** ******@TK2MSFTN GP14.phx.gbl:
From MSDN:
"The UnhandledExcept ionEventHandler 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
UnhandledExcept ionEventHandler 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
'SetUnhandledEx ceptionMode'. These linese are in my Main(...) before
anything else occurs.

Application.Thr eadException += new
System.Threadin g.ThreadExcepti onEventHandler
(Application_Th readException);

Application.Set UnhandledExcept ionMode
(UnhandledExcep tionMode.CatchE xception, true);

AppDomain.Curre ntDomain.Unhand ledException += new
UnhandledExcept ionEventHandler (CurrentDomain_ UnhandledExcept ion);
-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.Unhan dledException, but as is pointed out in the link below,
this is only a "notificati on" not a "handler".

UnhandledExcept ion is not a handler: (watch the wrap)
http://lab.msdn.microsoft.com/Produc...Feedback.aspx?
feedbackId=FDBK 21092

-mdb

Mar 18 '06 #7
Vadik Vaksin <vj******@walla .co.il> wrote in
news:44******** *************** **@msnews.micro soft.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(ITestS pec testSpec) {
try {
...
LoadAssembly();
...
RunTests(testSp ec);
}
catch(AppDomain UnloadedExcepti on) {
...
}
}
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(_assem blyPathName);
...
String applicationDoma inName = AppDomain.Curre ntDomain.Friend lyName
+ ":TestExecu tor-" + ++_loaderCount;
AppDomainSetup setup = new AppDomainSetup( );

setup.Applicati onBase = fi.DirectoryNam e;
setup.PrivateBi nPath = AppDomain.Curre ntDomain.BaseDi rectory;
setup.Applicati onName = fi.Name;
setup.ShadowCop yFiles = "true";
setup.ShadowCop yDirectories = fi.DirectoryNam e;

setup.Configura tionFile = fi.Name + ".config";

_appDomain = AppDomain.Creat eDomain(applica tionDomainName, null,
setup);
...
try {
_remoteLoader = (RemoteLoader)
_appDomain.Crea teInstanceFromA ndUnwrap(
//csUnitCorePathF ileName, remoteLoaderFul lTypeName);
csUnitDll, remoteLoaderFul lTypeName);
}
catch(Exception e) {
...
return null;
}

...
_assemblyFullNa me =
_remoteLoader.L oadAssembly(thi s._assemblyPath Name, assemblyName, this);

...
}
--
Vadik
"Michael Bray" wrote:
Vadik Vaksin <vj******@walla .co.il> wrote in
news:44******** *************** **@msnews.micro soft.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
1922
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 appdomains ? or , the purpose for creating another appdomains, is diffrent ?
4
2954
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 a lot of time (up to hours), and should run on a separate thread from the one that the GUI uses. The GUI also needs to display various properties for each function (such as parameters that can be set). It does this with property grid and other...
0
1771
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 incompatible between appdomains. I had to apply KB 887787 (http://support.microsoft.com/?kbid=887787) to fix my previous error, which stated... Assembly microsoft.web.validatepathmodule.dll security permission grant
1
2570
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 will be used concurrently by multiple users. We have a static object (which as you well know is only static per AppDomain), -I think I've just figured out the answer to my question, but perhaps some confirmation wouldn't go amiss-
8
1702
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 and programmatically loading/unloading assemblies in them. Question: What are some scenarios where I'd want to... 1 - programmatically create or destroy AppDomains 2 - load and unload assemblies in AppDomains?
0
1607
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 GetNumber(), and a global variable. The DllImport and getting and setting the numbers are fine. The problem comes when I make the calls to the libraries from separate AppDomains. Calling SetNumber with 3 different values across 3 different...
3
3760
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 responsed in the remoting group after a week, so I'm throwing a little wider net this time. I have a desktop app (FFx 2.0) developed with Studio 05 that loads assemblies in a separate AppDomains from the primary UI. I'd like to be able to hook up...
0
1868
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 reflection). I want a way to invoke these processes in parallel utilizing multi-core processors but I'd like to have the protection that AppDomains provide. I was doing some research where I found that there is no true isolation between threads and...
0
8722
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 - ~\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscoree.tlb using mscoree; using System.Runtime.InteropServices;
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.