Is it possible to have multiple assemblies in a Application Domain

T

Tony Johansson

Hi!

I know that Application is about isolation code in an assembly.
It is useful because it can improve reliability and Efficiency.

But there is one thing that I still have some doubt about and that is if you
can have multiple assemblies in an Application Domain. For example in the
code example below I load three assemblies(AssemblyA,AssemblyB and
AssemblyC) into the Application Domain named New Domain.
I got the following answer to this question when I asked this question on a
thread.
"My reading of the docs, along with a simple experiment, show that the
code ExecuteAssemblyByName does not return until the executable (such as
AssemblyA.exe) is finished. There is no threading nor switching as they
are executed in order. They are not executed in parallel."
This mean that you can only have one and only assembly running in the
Application Domain at any given time.
The strange thing is that in the Microsoft Press(exam 70-536) book that I
read is two things that talk in favour that you can have multiple assemblies
in an Application Domain and that is the following two points.
1. There is a figure drawn in the book that shows two assemblies in an
Application Domain
2.There is a method in Appdomain that is named GetAssemblies that says "Gets
the assemblies that have been loaded into the execution context of this
application domain."

static void Main(string[] args)
{
AppDomain myAppDomain = AppDomain.CreateDomain("New Domain");
myAppDomain.ExecuteAssemblyByName("AssemblyA");
myAppDomain.ExecuteAssemblyByName("AssemblyB");
myAppDomain.ExecuteAssemblyByName("AssemblyC");
}

I have also tested this small main and it's true that AssemblyB will not
start executing until AssembyA has finished executing.

Normally books are correct so I want to be sure what is right in this matter
if an Application Domain can have multiple assemblies or not ?

//Tony
 
A

Alberto Poblacion

Tony Johansson said:
But there is one thing that I still have some doubt about and that is if
you can have multiple assemblies in an Application Domain.
I have also tested this small main and it's true that AssemblyB will not
start executing until AssembyA has finished executing.

Normally books are correct so I want to be sure what is right in this
matter if an Application Domain can have multiple assemblies or not ?

To better understand Application Domains, please remember that all .Net
code ALWAYS executes in an AppDomain, even if you didn't create any
explicitly. For instance, if you create a plain and ordinary .exe
application and you run it from Windows, a default appdomain will be created
and your application will load into it.
Can the AppDomain have multiple assemblies? Of course. That happens
every time your .exe calls a .dll. You then have the exe and the dll (which
are two assemblies) loaded into memory. It also happens if you call
Assembly.Load() or Assembly.LoadFrom(). These methods load a new assembly
into the same AppDomain that calls them. You don't even need to load the
assemlies from a different domain.
Can the AppDomain execute several assemblies in parallel? Sure. Just
call Thread.Start() to launch another thread, and invoke code from a
different assembly from within that thread.
Can several AppDomains execute in parallel? Yes. Use the same thread
that you started in the paragraph above to create a new appdomain and load
and execute an assembly in that appdomain. Voila! You have two threads
executing in two AppDomains.
 
P

Patrice

Hello,

It seems you are confusing assemblies and threads. An assembly is just a
deployment unit, most of the time a single file. So yes an application
domain can use multiple assemblies just as an old win32 app can load several
DLLs...

Now an assembly is just a file, it has nothing to do with how the code run.
So if you application have a single thread, only this thread will run. If
your application have multiple threads, you'll be able to run some code in
parallel (and the code running in parallel could be in a single assembly or
in multiple assemblies, assemblies and threads being unrelated)...
 
F

Family Tree Mike

Hi!

I know that Application is about isolation code in an assembly.
It is useful because it can improve reliability and Efficiency.

But there is one thing that I still have some doubt about and that is if you
can have multiple assemblies in an Application Domain. For example in the
code example below I load three assemblies(AssemblyA,AssemblyB and
AssemblyC) into the Application Domain named New Domain.
I got the following answer to this question when I asked this question on a
thread.
"My reading of the docs, along with a simple experiment, show that the
code ExecuteAssemblyByName does not return until the executable (such as
AssemblyA.exe) is finished. There is no threading nor switching as they
are executed in order. They are not executed in parallel."
This mean that you can only have one and only assembly running in the
Application Domain at any given time.
The strange thing is that in the Microsoft Press(exam 70-536) book that I
read is two things that talk in favour that you can have multiple assemblies
in an Application Domain and that is the following two points.
1. There is a figure drawn in the book that shows two assemblies in an
Application Domain
2.There is a method in Appdomain that is named GetAssemblies that says "Gets
the assemblies that have been loaded into the execution context of this
application domain."

static void Main(string[] args)
{
AppDomain myAppDomain = AppDomain.CreateDomain("New Domain");
myAppDomain.ExecuteAssemblyByName("AssemblyA");
myAppDomain.ExecuteAssemblyByName("AssemblyB");
myAppDomain.ExecuteAssemblyByName("AssemblyC");
}

I have also tested this small main and it's true that AssemblyB will not
start executing until AssembyA has finished executing.

Normally books are correct so I want to be sure what is right in this matter
if an Application Domain can have multiple assemblies or not ?

//Tony

I probably should have written: "They assemblies are not executed in
parallel in your example."

Certainly as the other have pointed out, you could have executed them in
parallel by putting each call into a separate thread.

As has been pointed out, several dlls can be in a single app domain.
You would not use (necessarily) ExecuteAssembly* to do this. You might
use AppDomain.Load("path to dll"), for example, to load a bunch of
assemblies that you later wish to remove.
 
A

Arne Vajhøj

I know that Application is about isolation code in an assembly.
It is useful because it can improve reliability and Efficiency.

But there is one thing that I still have some doubt about and that is if you
can have multiple assemblies in an Application Domain. For example in the
code example below I load three assemblies(AssemblyA,AssemblyB and
AssemblyC) into the Application Domain named New Domain.
I got the following answer to this question when I asked this question on a
thread.
"My reading of the docs, along with a simple experiment, show that the
code ExecuteAssemblyByName does not return until the executable (such as
AssemblyA.exe) is finished. There is no threading nor switching as they
are executed in order. They are not executed in parallel."
This mean that you can only have one and only assembly running in the
Application Domain at any given time.
The strange thing is that in the Microsoft Press(exam 70-536) book that I
read is two things that talk in favour that you can have multiple assemblies
in an Application Domain and that is the following two points.
1. There is a figure drawn in the book that shows two assemblies in an
Application Domain
2.There is a method in Appdomain that is named GetAssemblies that says "Gets
the assemblies that have been loaded into the execution context of this
application domain."

static void Main(string[] args)
{
AppDomain myAppDomain = AppDomain.CreateDomain("New Domain");
myAppDomain.ExecuteAssemblyByName("AssemblyA");
myAppDomain.ExecuteAssemblyByName("AssemblyB");
myAppDomain.ExecuteAssemblyByName("AssemblyC");
}

I have also tested this small main and it's true that AssemblyB will not
start executing until AssembyA has finished executing.

Normally books are correct so I want to be sure what is right in this matter
if an Application Domain can have multiple assemblies or not ?

Again. Application domains is for code separation - it is unrelated to
processes and threads.

My guess is that in most multiple app domains cases there will be
started multiple threads. So code can execute independently. But the
thread starting is not done automatically.

Arne
 

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