I have an assembly in a shared folder (only a UNC path, no mapped drive). When I try to register it programmatically through RegistrationServices , I get a strange error.
Here is the code:
using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace BLRegisterAssembly { public static class BlRegisterAssembly { public static void Register() { var asm = Assembly.LoadFile(@"\\myUNCPath\myAssembly.dll"); var rs = new RegistrationServices(); rs.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);
This is the error I get:
“Failed to load file or assembly” [XXXXXXXXXXXXXX], Version = 1.0.0.0, Culture = Neutral, PublicKeyToken = [xxxxxxxxxxxxxx] 'or one of its dependencies. The system cannot find the specified file. "
(This file is the assembly referenced by the main assembly).
A few more points:
- The referenced assembly is in the same folder as the main assembly that I am trying to register.
- The folder cannot be displayed as a logical drive. Due to access to network folders, users in different groups have different drive mappings with the same network folders, and they cannot be changed in accordance with IT policy ...
Can someone point me in the right direction to solve the problem?
ANSWERED
Since I used Assembly.LoadFile , dependent assemblies must be manually resolved through AssemblyResolve . The following code update fixed my problems:
public void Register() { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); var asm = Assembly.LoadFile(Path.Combine(m_path, assemblyName)); var rs = new RegistrationServices(); rs.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase); } static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
source share