Load dependent assemblies from another location.

Load dependent assemblies from another location.

Mats_Sarapik
Participant Participant
924 Views
2 Replies
Message 1 of 3

Load dependent assemblies from another location.

Mats_Sarapik
Participant
Participant

Hi,

I have managed dll with a bunch of dependencies.

If my dll and all dependencies are in the same folder everything works fine.

But after cleaning up and moving those to another place, my dll just refuses to load.

 

Is there a way so I can keep my  dll and the dependencies separated?

Autocad suppport paths does not work.

Preloading them using Assemly.LoadFile does not work.

 

/Mats

0 Likes
Accepted solutions (1)
925 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

.NET framework provides ways for its runtime to locate assemblies (assmbly binding, probing...). These articles would give you some useful information:

 

https://docs.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies 

https://weblog.west-wind.com/posts/2016/dec/12/loading-net-assemblies-out-of-seperate-folders 

 

Unless your said dependent assemblies are signed and is placed in GAC (Global Assembly Chache), it is very likely you need to make some configuration in the app.exe.config file. In the case of AutoCAD plugin, it is acad.exe.config. I am not sure it would be less troublesome than simply place the depedent assemblies in the same folder as your plugin DLL(s). That is, while it is doable, it only cause more trouble than good, unless you have very convincing reasons to take pains to seperate your plugin DLL from the said dependent assemblies.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

gleeuwdrent
Advocate
Advocate

When you add the desired assemblies as Embedded Resource, you can use the followings static method to load them on runtime:

 

static YourClassName()
{
		AppDomain.CurrentDomain.AssemblyResolve += (sender, bargs) =>
		{
				String dllName = new AssemblyName(bargs.Name).Name + ".dll";
				var assem = System.Reflection.Assembly.GetExecutingAssembly();
				String resourceName = assem.GetManifestResourceNames().FirstOrDefault(rn => rn.EndsWith(dllName));
				if (resourceName == null) return null; // Not found, maybe another handler will find it
				using (var stream = assem.GetManifestResourceStream(resourceName))
				{
						Byte[] assemblyData = new Byte[stream.Length];
						stream.Read(assemblyData, 0, assemblyData.Length);
						return System.Reflection.Assembly.Load(assemblyData);
				}
		};
}
0 Likes