I have to load a plugin DLL into a WebAPI application. So I made a custom IPlugin interface that exposes the actions a plugin can react to inside the existing controllers, plus I want to allow the plugin DLL to provide more routes. So I cannot add the DLL into the References list, I have to load it dynamically based on a config file entry.
Therefore, I added ExamplePlugin.DLL to my WebAPI's bin directory (C:\users\Alexander\Source\Repos\Testbed\WebAPI\bin
) and referenced it from web config's AppSettings:
<add key="ExamplePlugin" value="ExamplePlugin.dll" />
and try to load it like this:
string dllRelativePath = ConfigurationManager.AppSettings[possiblePlugin];
FileInfo dllFileInfo = new FileInfo(dllRelativePath);
Assembly dllAssembly = Assembly.LoadFile(dllFileInfo.FullName);
foreach (Type type in dllAssembly.GetExportedTypes())
{
if (typeof(IPlugin).IsAssignableFrom(type))
{
IPlugin i = (IPlugin)Activator.CreateInstance(type);
PluginManager.Add(i);
}
}
However, the error message says:
System.IO.FileNotFoundException:
Could not load file c:\windows\system32\inetsrv\ExamplePlugin.dll: Das System kann die angegebene Datei nicht finden. (Ausnahme von HRESULT: 0x80070002)
Why is my WebAPI DLL searching in the wrong folder and how can I fix this?
You need to map your path to your application folder otherwise it will assume the file is in System32.
string dllPlugin = "ExamplePlugin.dll";
FileInfo dllFileInfo = new FileInfo(MapPath("~/bin/" + dllPlugin));
Assembly dllAssembly = Assembly.LoadFile(dllFileInfo.FullName);
User contributions licensed under CC BY-SA 3.0