mardi 29 octobre 2019

C# load a dll from file, then update that dll and then re-load the dll from file

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





Aucun commentaire:

Enregistrer un commentaire