martijnboland / apptext

AppText is a content management system for applications. Application developers can use it to replace static resources in applications with dynamic content and delegate content management to non-developers.
https://apptext.io
Apache License 2.0
23 stars 5 forks source link

Admin app DI error when not setup #53

Open vallieresc opened 3 years ago

vallieresc commented 3 years ago

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 commented 3 years ago

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

vallieresc commented 3 years ago

Agree, thanks.

vallieresc commented 2 years ago

@martijnboland Any development on this solution?

martijnboland commented 2 years ago

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

martijnboland commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.