Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an Assembly from Bin in ASP.NET

I have a file name, like "Foo.dll," for a library that I know is in the bin directory. I want to create an Assembly object for it. I'm trying to instantiate this object from a class that's not a page, so I don't have the Request object to get the path. How do I get the path I need to use Assembly.Load()?

like image 396
Pete Michaud Avatar asked Jul 21 '09 23:07

Pete Michaud


People also ask

How CLR load assembly?

At runtime, when you use a type from a referenced project for the first time, the CLR looks in the application directory for the DLL file with the same name and version it expects. It then loads that assembly into the process. This is also called binding to the assembly.

What is the method to load assembly given its file name and its path?

LoadFrom(String) Loads an assembly given its file name or path.

Where are .NET dlls stored?

Dll files are located in C:\Windows\System32.


2 Answers

Assembly.Load should not require a file path, rather it requires an AssemblyName. If you know that your assembly is in the standard search path (i.e. the bin directory), you should not need to know the disk path of the assembly...you only need to know its assembly name. In the case of your assembly, assuming you don't need a specific version, culture, etc., the assembly name should just be "Foo":

Assembly fooAssembly = Assembly.Load("Foo");

If you do need to load a specific version, you would do the following:

Assembly fooAssembly = Assembly.Load("Foo, Version=1.1.2, Culture=neutral");

Generally, you want to use Assembly.Load, rather than Assembly.LoadFrom or Assembly.LoadFile. LoadFrom and LoadFile work outside of the standard fusion process, and can lead to assemblies being loaded more than once, loaded from insecure locations, etc. Assembly.Load performs a "standard" load, searching the standard assembly locations such as bin, the GAC, etc., and applies all the standard security checks.

like image 199
jrista Avatar answered Oct 18 '22 21:10

jrista


Does Assembly.LoadFile(...) work?

like image 36
DanDan Avatar answered Oct 18 '22 20:10

DanDan