简体   繁体   中英

Could not load an assembly from specific folder

So here is my problem - I have an application "A" which uses a common dll "Login.dll". This common dll is placed inside a folder "CommonDll" . This dll is supposed to be accessed by many application. To do so i have put following code in CONFIG of Calling Application "A"

<runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Login"  culture="neutral" publicKeyToken="105c4b5bbc9d3d16"/>
          <codeBase version="1.0.0.0" href="file:///C:/CommonDll/Login.dll"/>
        </dependentAssembly>
      </assemblyBinding>
    </runtime>

This is working perfectly fine on my local machine. However if i put the same folder structure on others machine and deploy this application, its giving me an error Could not load file or assembly 'Login, Version=1.0.0.0, Culture=neutral, PublicKeyToken=105c4b5bbc9d3d16' or one of its dependencies. The system cannot find the file specified.

But on clients machine if i run this

file:///C:/CommonDll/Login.dll

This is targeting the correct dll .

Any suggestion or view is highly appreciated...

Your problem shoul dbe solved if you use automatic assembly resolving. I like to use the following code:

var yourCommondDllFolder = new DirectoryInfo("C:/CommonDll/").GetFiles("*.dll");

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
    var dll = yourCommondDllFolder.FirstOrDefault(fi => fi.Name == args.Name);
    if (dll == null)
    {
        return null;
    }

    return Assembly.Load(dll.FullName);
};

Than your app domain try to resolve the assembly when it could not be loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM