DotNetCorePlugins icon indicating copy to clipboard operation
DotNetCorePlugins copied to clipboard

[Question] Accessing app.config from a plugin

Open ssteiner opened this issue 3 years ago • 5 comments

I'm returning to your plugin package for my plugins because I want to implement plugin reloading.

All my plugins have a config file, an ap.config that gets build with the plugin and that contains settings. Every plugin has an Initialize method, which inside the plugin performs this code to fetch the right app.config:

	string configFile = $"{type.Assembly.Location}.config";
	if (!File.Exists(configFile))
		throw new FileNotFoundException($"Could not find config file {configFile}");
        .... //load config and extract variables here

I've been using the native plugin loading mechanism of .NET Core 3.1+ before (and similar approach for the old .NET framework) where this works fine. However, if I make plugins unloadable, this.GetType().Assembly.Location for each plugin is null. this.GetType().Assembly.CodeBase is does have a value, but it is: file:///C:/Program Files (x86)/dotnet/shared/Microsoft.NETCore.App/6.0.6/System.Private.CoreLib.dll.

Is there any way to get the Location property filled for the plugin assembly?

ssteiner avatar Jun 20 '22 15:06 ssteiner

Have you tried using GetExecutingAssembly?

Assembly.GetExecutingAssembly().Location

faridmehrhesam avatar Mar 08 '23 12:03 faridmehrhesam

No, because Assembly.GetExecutingAssembly().Location() generally returns the location of the executing assembly (so e.g. the .exe file), and that would then be the program that loads and uses the plugin, and not the location of the plugin itself.

I ended up storing the path of the dll in my plugin discovery mechanism.

ssteiner avatar Mar 10 '23 17:03 ssteiner

I am currently using it and it works for me

faridmehrhesam avatar Mar 10 '23 17:03 faridmehrhesam