0

tldr; I need to be able to reference a dll, update the dll and then reference the new dll

We have a service that takes weeks to update on our production system, and one use case that will require an update cycle of less than a day, our solution to this is to have the system load .dll files directly into itself and then use the classes/methods from those assemblies. The problem is that I can only seem to reference the first dll, any attempt at using a new one gets ignored.

The Functional code I have is

Assembly myAssembly1 = Assembly.LoadFrom("path.dll");
Type myType = myAssembly1.GetType("ClassName");
var myObject = Activator.CreateInstance(myType);
dynamic test = myType.InvokeMember("methodName", BindingFlags.InvokeMethod, null, myObject, null);
await test;

This method get's referenced a few times, and apparently the the first 'LoadFrom' is the only one that actually changes the app domain.

I understand creating a new App domain each time will solve the issue, but I can't seem to get it to work;

AppDomain domain = AppDomain.CreateDomain("MyDomain");
Assembly myAssembly1 = domain.Load(AssemblyName.GetAssemblyName(path));
//or = domain.Load(path);
//or = domain.Load("assemblyname");
Type myType = myAssembly1.GetType("ClassName");
var myObject = Activator.CreateInstance(myType);
dynamic test = myType.InvokeMember("methodName", BindingFlags.InvokeMethod, null, myObject, null);
await test;
AppDomain.Unload(domain);

So far any attempt at loading the .dll file into my app domain has resulted in the error 'Error: Could not load file or assembly 'assemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.', I'm assuming this is either me not putting the .dll file in the right location, or not handling the dependencies properly.

Any help would be greatly appreciated, I keep trying different things and seem to keep running into walls

thanks in advance

Cuan

Edit: windows 10, .net standard 2.0

Stefan
  • 652
  • 5
  • 19
The Lemon
  • 1,211
  • 15
  • 26
  • What edition of .net and on which OS are you running? things are a little different based on that – zaitsman Oct 30 '19 at 03:12
  • 4
    Possible duplicate of [Difference between LoadFile and LoadFrom with .NET Assemblies?](https://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies) – Hasan Emrah Süngü Oct 30 '19 at 03:17
  • You need to run your assembly-loading code in a method that runs in the newly created AppComain. So create a class containing the `Assembly.Load` plus all code accessing this assembly and instantiate it using [CreateInstanceAndUnwrap](https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.createinstanceandunwrap?view=netframework-4.8) – Klaus Gütter Oct 30 '19 at 04:25

1 Answers1

0

Difference between LoadFile and LoadFrom with .NET Assemblies? this does partially answer my question (thanks Hasan)

Slight caveat of this method - once either LoadFile or LoadFrom have been used -windows views the file as being in use by the application, and so you can't delete the dll file, you have to rename it and then add a new file

Another solution found was: changing the assembly name of the dll each time (versioning it) will work, as LoadFrom no longer sees the dll libraries as being the same. The benefit here is saving you from worrying about dependencies, as the assembly name is different each time you can use LoadFrom repeatedly. With this method you still need to rename old dll files though, or have a DB script that updates the file locations with each version.

The Lemon
  • 1,211
  • 15
  • 26