apptext icon indicating copy to clipboard operation
apptext copied to clipboard

Admin app DI error when not setup

Open vallieresc opened this issue 3 years ago • 8 comments

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).

vallieresc avatar May 21 '21 14:05 vallieresc

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

martijnboland avatar May 24 '21 10:05 martijnboland

Agree, thanks.

vallieresc avatar May 25 '21 13:05 vallieresc

@martijnboland Any development on this solution?

vallieresc avatar Nov 19 '21 16:11 vallieresc

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

martijnboland avatar Nov 25 '21 08:11 martijnboland

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.

martijnboland avatar Nov 25 '21 19:11 martijnboland

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.

vallieresc avatar Nov 25 '21 19:11 vallieresc

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);
                    }
                });

martijnboland avatar Nov 25 '21 22:11 martijnboland

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.

vallieresc avatar Nov 26 '21 13:11 vallieresc