WHEN to load assemblies dynamically

G

Guest

Hello,

I'm confused about WHEN to load assemblies dynamically. While I was studying
my Win MCTS I did some practice with small pieces of code, but not
understanding the WHAT FOR concept for it all: I will explain my little
problem:

I'm developing a little network app (APP1) that will be used by different
kind of users; some of them will be using additional functions contained in
another tiny assembly class library (DLL1), others not.

DLL1 itself, has a reference to a COM component installed on SOME PCs (not
all).

I started to put code on APP1 to load DLL1 dynamically with
"Assembly.LoadFrom(myAssemblyPath)"... and so on.

Why I thought doing this ? ...

*** First, I thought that if I added a reference for DLL1 into my APP1
project, and I run APP1 on a client pc that does not have that COM component
installed, initially, APP1 would crash.

But now I think this is false, as if the user for a client pc does cannot
use the functions contained in the DLL1 (controlled by simple program flow)
the program will not crash ... Is it true ?


*** Second, I thought that doing that load dynamically will make the
application more lightweight in case the user does not use the functions
contained in DLL1, but this is not worth it to program this way as it is more
painful than having intellisense if I add a reference for DLL1 into APP1....
Am I wrong?

So, what is for, all this dynamically DLL loading hell? 8-D

The only thing I can imagine now is when you cannot know in advance where
the DLL will be, or worse, which methods will contain.

But in a stable and not too big development, maybe I don't have the need to
use it in my case...

I hope not being tooooo far from understanding the reasons for dynamically
loaded code.

Thanks in advance,
 
J

Jeff Hopper

Let me preface by saying that the following response makes some assumptions
that may not be valid, but with limited information about your environment
and requirements, these are my thoughts: Why not simply deploy the necessary
components with your application? The size of a typcial DLL is usually a
small fraction of the size of a typical hard drive. Are your users' machines
really that limited in storage space and/or memory? While it is definitely a
good thing to design an application to have as small a footprint as
possible, you should make sure the effort of dynamic assembly loading is
justified.
 
J

Jim Rand

One reason to use dynamically loaded DLLs is to support polymorphism. Two
separate DLL's could implement the same interface. At run time, you would
load the DLL which meets your requirements.
 
J

Jeff Hopper

While that is a true statement, in general, polymorphism does not seem to be
one of Roger's reasons for attempting to use dynamic DLL loading.
 
G

Guest

Hi Jeff,

Thanks for your answer; you said:
Why not simply deploy the necessary components with your application?

I can't do this as one of the COM components must be used from its actual
network folder because of license stuff. Cannot be copied nor moved.

.... But my questions were (From the original post, re-explained):

FIRST: If I control what the user can do , (for example, hiding/disabling
the menus that access the functions from DLL1), the program will not crash
as the code from the dll will never be executed and the error saying COM
component not found will not be thrown for the pcs where this component is
not registered.

Is it true ? is a good approach ?

SECOND: If the above approach works and is nearly correct, I should not
consider to use reflection in my case. OK (Answered)

So, Reflection can be used for polymorphism as our friend Jim said, and the
other two cases I proposed on the final part of my initial post ?
The only thing I can imagine now is when you cannot know in advance where
the DLL will be, or worse, which methods will contain.

Thanks again,

Roger Tranchez
 
J

Jeffrey Tan[MSFT]

Hi Roger,

Based on my understanding, you want to know the purpose of using
Assembly.Load/LoadForm to dynamically load an assembly. If I have
misunderstood you, please feel free to tell me, thanks.

Yes, there are actual many scenarios for using Assembly.Load/LoadForm:
1. We normally use Assembly.Load by loading assembly into a separate
AppDomain to build a security sandbox for executing partial-trusted .Net
code. This technology leverages .Net Code Access Security for restricting
the permission assigned to the assembly, see the article below for details:
"Discover Techniques for Safely Hosting Untrusted Add-Ins with the .NET
Framework 2.0"
http://msdn.microsoft.com/msdnmag/issues/05/11/HostingAddIns/default.aspx

Also, this approach has another usage of supporting plug-in dlls as stated
in the article.

2. In .Net, assembly can not be unload at runtime. You can only unload an
entire AppDomain. So you may use Assembly.Load with creating a new
AppDomain to support the unloading function for assembly dll. See link
below:
http://blogs.msdn.com/suzcook/archive/2003/07/08/57211.aspx

3. We may also leverage Assembly.Load and AppDomain to support an
auto-update application model, which is a further usage of #2. For example,
Asp.net leverages this technology. There is a good article titled with
"AppDomains and Dynamic Loading" for this topic, however, it seems that it
is removed from online MSDN. If you have a copy of local MSDN, you may
search this article title in it.

4. Supporting some type of dynamic code generation like a script engine,
see the article below for details:
"Dynamically executing code in .Net"
http://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm

There may be more other usages, however, I think above is enough for seeing
its advantage.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Jeffrey, for such a detailed answer...

Finally I think I will not use dynamic code in my application as it does not
need this technology for the time being.

I will take note of all your explanations and will experiment them "when I
have the time for it".

There are so many things in NET that sometimes I think I could not learn all
them in a whole life cycle... The MCTS certification has been for me only the
start for a non-stop.


Thanks again,

--
Roger Tranchez
MCTS
..NET 2005 and DB developer


"Jeffrey Tan[MSFT]" said:
Hi Roger,

Based on my understanding, you want to know the purpose of using
Assembly.Load/LoadForm to dynamically load an assembly. If I have
misunderstood you, please feel free to tell me, thanks.

Yes, there are actual many scenarios for using Assembly.Load/LoadForm:
1. We normally use Assembly.Load by loading assembly into a separate
AppDomain to build a security sandbox for executing partial-trusted .Net
code. This technology leverages .Net Code Access Security for restricting
the permission assigned to the assembly, see the article below for details:
"Discover Techniques for Safely Hosting Untrusted Add-Ins with the .NET
Framework 2.0"
http://msdn.microsoft.com/msdnmag/issues/05/11/HostingAddIns/default.aspx

Also, this approach has another usage of supporting plug-in dlls as stated
in the article.

2. In .Net, assembly can not be unload at runtime. You can only unload an
entire AppDomain. So you may use Assembly.Load with creating a new
AppDomain to support the unloading function for assembly dll. See link
below:
http://blogs.msdn.com/suzcook/archive/2003/07/08/57211.aspx

3. We may also leverage Assembly.Load and AppDomain to support an
auto-update application model, which is a further usage of #2. For example,
Asp.net leverages this technology. There is a good article titled with
"AppDomains and Dynamic Loading" for this topic, however, it seems that it
is removed from online MSDN. If you have a copy of local MSDN, you may
search this article title in it.

4. Supporting some type of dynamic code generation like a script engine,
see the article below for details:
"Dynamically executing code in .Net"
http://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm

There may be more other usages, however, I think above is enough for seeing
its advantage.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Roger,

Thanks for your feedback.

Yes, .Net provided a lot of mechanisms and flexibilities for various
achitecture and design, so the overall class library is really big. In the
introducing of .Net3.0, WCP, WPF, WWP, actually, I do not think anyone can
understand very details of .Net :). My recommendation is choosing one or
two interested topics(Winform or Asp.net) and studying with them. Also,
studying the .Net CLR should be essential for digging into .Net. I would
recommend "C# via CLR" by Jeffrey Richter, although I only read its first
edition "Applied .Net Framework"

Anyway, if you need further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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