Load plugin DLL into WebAPI application

0

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?

c#
asp.net-web-api
dll
asked on Stack Overflow Nov 9, 2017 by Alexander • edited Nov 9, 2017 by Alexander

1 Answer

1

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);
answered on Stack Overflow Nov 9, 2017 by Riv

User contributions licensed under CC BY-SA 3.0