.NET Framework Bookmark and Share   
 index > Common Language Runtime > How to control loading of a referenced assembly
 

How to control loading of a referenced assembly

My program (standalone Windows Forms app) references an assembly in a DLL file. When that DLL file is present in the same folder as the exe file, everything runs fine.

The classes in that referenced assembly are not used all the time. In fact, they are used only if a user decides to do certain optional things which most other users don't need most of the time. Hence I am thinking of removing that DLL from most users.

If I remove the DLL file, at some point in the program life cycle, an exception would arise with "Could not load file or assembly 'xxxxx, Version=.....' or one of its dependencies. The system cannot find the file specified." How can I trap the loading of the assembly so that I can give a better user experience by giving instructions to the user on how to get and install the DLL file for the few users who need the additional features or to simply ignore the absence of the assembly otherwise.
K.Kong
Actually, I am wrong.

ExternalIO.ReadFile is JIT compiled the first time it is called, ie at the lineMyData mydata = new ExternalIO().ReadFile("xxx");

  • Marked As Answer byK.Kong Saturday, September 12, 2009 8:37 AM
  •  
Wole Ogunremi
How about wrapping the relevant code in a try{}catch(FileNotFoundException){} block?
Wole Ogunremi
Thanks. My problem is that I don't know who/what loads the DLL at when. The exception occurs at some point before the actual reference to a class in that assembly.

I put a try and catch around those methods that reference that assembly. This didn't catch the exception.

I put a try and catch around the method that instantiates the class containing the method that references that assembly, yes it caught the exception. Will this always work?

Thanks.

K.Kong
>>I put a try and catch around the method that instantiates the class containing the method that references that assembly, yes it caught the exception

It sounds to me like the class also references types in the assembly either in it's constructor, or declares a field of type contained in the assembly, ie, the exception occurs when the class is initialized. Is this the case? If yes then your second solution should be fine.
Wole Ogunremi
Thanks. I checked but the only reference to the DLL is in a method. The code is as follows:


My application:

...
try
{
MyData mydata = new ExternalIO().ReadFile("xxx");
}
catch {FileNotFoundException F)
{
//catches the assembly not found exeption here
}

....

public class ExternalIO
{
public MyData ReadFile(string Filename)
{
try
{
...
OpenSource.Data data = ...;
...
}
catch
{
// exception notcaught here if OpenSource.dll is missing
}
}


-------------------------------------

The DLL file contains:

namespace OpenSource
{
public class Data
{
...
}
}

K.Kong
Actually, I am wrong.

ExternalIO.ReadFile is JIT compiled the first time it is called, ie at the lineMyData mydata = new ExternalIO().ReadFile("xxx");

  • Marked As Answer byK.Kong Saturday, September 12, 2009 8:37 AM
  •  
Wole Ogunremi

You can use google to search for other answers

Custom Search

More Threads

• Strong Naming Assemblies & 3rd party Controls (OCX/COM DLLs)
• System::Drawing::Image / EMF / Clipboard
• Finding value of "% Time in JIT"
• AccessControlEntry.Trustee and sidHistory
• System.Math.Round(Double, Integer) bug?
• SignedCms in VS.2005 is very slow, signed data is not compatible with CAPICOM 2
• AccessViolation Thrown when the server is started.
• Microsoft.NET Framework CAS Policy Manager
• Multiple Floating Images linked by lines
• Global structures between VB.net and C++.net or C# .net