.NET Loading Assemblies at Run Time Again

I posted a similar question a while ago. I need to load the assembly at runtime.

This is easy if I know the absolute path of the DLL at runtime.

But I do not want :( Assembly assembly failed .Load () or LoadFromFile () if the file is not in the root of the application.

The only thing I have is the name dll. DLL can be located in the root, system32 or even in the GAC.

Is it possible .net to automatically determine where the dll is located, for example, for example: it must first look at the root. If not, go to system folders, try the GAC.

EDITED

I am using pluggable architecture. I do not need to register dll. I have a multi-user application. I have an application table containing information about applications. Each application has a dll path associated with it, containing certain algorithms associated with this application. Hope this helps.

+4
source share
9 answers

You can connect to the AppDomain.CurrentDomain.AssemblyResolve event and manually download the assembly for your application on demand.

+2
source

When the current application is looking for assemblies, it looks in several places (bin folder, gac, etc.), if it cannot find it, then the developer must manually specify the application where to look. You can do this by intercepting the AssemblyResolve event and using the event arguments to tell the CLR where your assembly is located.

 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; .................... Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var strTempAssmbPath= Path.GetFullPath("C:\\Windows\\System32\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"); var strTempAssmbPath2= Path.GetFullPath("C:\\Windows\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"); if (File.Exists(strTempAssmbPath)) return Assembly.LoadFrom(strTempAssmbPath); if (File.Exists(strTempAssmbPath2)) return Assembly.LoadFrom(strTempAssmbPath2); } 
+2
source

A disk will be found automatically if it is in any place referenced by the PATH system variable (i.e. system32), or if the DLL is registered. You cannot find it if it can be anywhere on the disk, but no one needs this function anyway.

EDIT: Is it possible to register a dll? how do you get the assembly name if you don't know about the assembly ahead of time? Are you creating some kind of plug-in architecture? I think this will help if you explain your situation a little more.

EDIT2: If so, why not provide the user with the option to register their plugin? You can get all the necessary information from the open file dialog box. This or just make them dump them into the folder you specify.

+1
source

Yes, there is a way, and it depends on whether your dll is weak (without a public key token) or strong (with a public key token). If it is poorly named and you added a link to the assembly with dll in VS, then VS will first look in the root of the application folder, and if it does not find it, it will look in the subdirectories that you specify as the value of the privatePath attribute in the XML configuration file.

If its strongly named dll, the CLR will search the GAC, so you need to install the DLL in the GAC. The CLR may also look in the application directory if you install an XML configuration file that has a codeBase element pointing to the dll path.

To specify an assembly in the GAC, you can use the / reference: [DLL name] switch when compiling the assembly.

+1
source

I hope you read the following. My suggestion...

  • How the runtime finds assemblies
  • You can give the assembly. LoadWithPartialName vortex .. may work. he says that he will look for the application folder and the GAC , unlike Assembly.Load. (However, it is less secure. If you did not specify all 4 parts of the assembly name, you may get the wrong version of the DLL.
  • Also try AppDomainSetup.PrivateBinPath (AppDomain.AppendPrivatePath is deprecated in favor of this) to add subfolders of the application root to the list of folders for probing assemblies. You can also try copying files from other places to the [AppFolder] \ MySandboxForDLLsToLoad folder, which is added to PrivateBinPath.
+1
source

You have three options.

  • Install an assembly in the global assembly cache (GAC)
  • Use the application configuration file (.config) with tags
  • Use AssemblyResolve Event

You can find the details here.

+1
source

The resolution algorithm used by .NET to find assemblies and their dependents is straightforward.

  • .NET identifies the required version. Usually, information about dependent assemblies is present in the application assembly manifest.
  • .NET only searches for a GAC ​​(global assembly cache) if the build is strong.
  • If not found in the GAC and if the .config presnt file, then .NET looks for the location specified in the configuration file, otherwise .NET looks for the directory containing the executable file (.EXE)
  • After that, if the assembly is still not found, the application fails.

Click here for a video explaining the .net resolution algorithm, and click here for a video on later binding assemblies

+1
source

Register this assembly with the GAC. How to do it

0
source

Use this Assembly.LoadWithPartialName (AssemblyName);

0
source

All Articles