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

Admin app DI error when not setup #53

Open
vallieresc opened this issue May 21, 2021 · 8 comments
Open

Admin app DI error when not setup #53

vallieresc opened this issue May 21, 2021 · 8 comments

Comments

@vallieresc
Copy link

If the AppText.AdminApp reference is added but no .AddAdmin() is set the result is InvalidOperationException: Unable to resolve service for type 'AppText.AdminApp.Configuration.AppTextAdminConfigurationOptions' while attempting to activate 'AppText.AdminApp.Controllers.AdminController'.

My setup has options in the appsettings file for the dev to disable the admin feature in their environment so it would be great to be able to disable admin for security reasons. Not sure what the best approach would be for this? Removing the Admin package is a way but when releasing the app in other environments it's not great. A quick appsettings change would be ideal for us = no .AddAdmin() set or .AddAdmin(o => o.Enable = false).

@martijnboland
Copy link
Owner

I think the best solution would be to not crash if AddAdmin is not set. I'll have a look at this.

@vallieresc
Copy link
Author

Agree, thanks.

@vallieresc
Copy link
Author

@martijnboland Any development on this solution?

@martijnboland
Copy link
Owner

martijnboland commented Nov 25, 2021

Unfortunately I've been swamped with work the last months, but I'll try to fix this soon. Thanks for the heads up.

@martijnboland
Copy link
Owner

I did a quick attempt, but this turns out to be quite difficult. Internally we're using IServiceCollection.AddMvcCore() and that scans all referenced assemblies and automatically adds AppText.Admin as ApplicationPart (which registers a controller) which we did not expect.

@vallieresc
Copy link
Author

I appreciate your effort on this. I don't require this fix at the moment but in the near future. 1 month would be nice but 3 months can be ok.

@martijnboland
Copy link
Owner

Found a workaround: when you don't call AddAdmin(), you have to remove the AdminApp application part as well. This is fairly easy (in Startup.cs):

    services.AddControllersWithViews()
        .ConfigureApplicationPartManager(pm => 
                {
                    var adminAppApplicationPart = pm.ApplicationParts.FirstOrDefault(ap => ap.Name == "AppText.AdminApp");
                    if (adminAppApplicationPart != null)
                    {
                        pm.ApplicationParts.Remove(adminAppApplicationPart);
                    }
                });

@vallieresc
Copy link
Author

Hmmm simple but difficult to implement in my case. I encapsulate AppText in a library with its own startup.cs. The client application has its startup.cs with the services.AddControllersWithViews(). Each app would have to add this parameter. I'd like to manage this in the library. It's a good start, let me know if you find anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants