How to stop a feature enabling based on criteria

Topics: Core, Customizing Orchard
Aug 1, 2013 at 2:56 PM
Hi folks,

I'm trying to check when a use tries to enable a feature as to whether certain criteria are met (examples might be, a reg key on the server or app fabric being installed). If the criteria are not met, the feature should not enable.

I thought that using IFeatureEventHandler would help, and that I would hook into the "Enabling" event, but it doesn't quite work as expected. I've raised a bug relating to this already https://orchard.codeplex.com/workitem/19962, as items are already enabled by the time "Enabling" fires.

However, I'm just wondering if anyone has a current way of cancelling the enable of a feature. Throwing an exception on Enabling does not work. There is no "Cancel" on an event argument. Can I call a "feature disable" command within code somehow?

Any thoughts welcome
Aug 1, 2013 at 2:58 PM
At a guess the enabling not firing in time might be if this code was firing off events and not waiting on the response...?
               if (entry.FeatureState.EnableState == ShellFeatureState.State.Rising) {
                    Logger.Information("Enabling feature '{0}'", entry.Feature.Descriptor.Id);
                    _featureEvents.Enabling(entry.Feature);
                    _stateManager.UpdateEnabledState(entry.FeatureState, ShellFeatureState.State.Up);
                    _featureEvents.Enabled(entry.Feature);
                }
Developer
Aug 2, 2013 at 7:46 AM
You could create an issue suggesting that the Enabling event receives a context with a Cancel property. This way the team can discuss and others can upvote the request.
Alternatively, as you suggested, you could handle the Enabled event and disable the feature using IModuleService.DisableFeatures.
Aug 2, 2013 at 9:49 AM
yes - that is the issue that I created and listed in my first post Sipka ;) Upvote if you agree :)

I will use IModuleService then for now - I had not found that one - cheers :)