Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Accessing app.config from a plugin #254

Open
ssteiner opened this issue Jun 20, 2022 · 5 comments
Open

[Question] Accessing app.config from a plugin #254

ssteiner opened this issue Jun 20, 2022 · 5 comments
Labels
question Further information is requested

Comments

@ssteiner
Copy link

ssteiner commented Jun 20, 2022

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 ssteiner added the question Further information is requested label Jun 20, 2022
@faridmehrhesam
Copy link

faridmehrhesam commented Mar 8, 2023

Have you tried using GetExecutingAssembly?

Assembly.GetExecutingAssembly().Location

@ssteiner
Copy link
Author

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.

@faridmehrhesam
Copy link

I am currently using it and it works for me

Copy link

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

@github-actions github-actions bot added the stale label Mar 10, 2024
@natemcmaster
Copy link
Owner

I'm not aware of any mechanism we can use to set the Location value when loading assemblies.

The base class we use in .NET to extend assembly loading is https://learn.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext. If there is something we have missed in how its implement that would help here, let me know. Otherwise, I don't know if there is anything we can change in our library to fix this for you.

@github-actions github-actions bot removed the stale label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants