Question

Using event handlers in my Site-scoped feature's Feature Receiver, I'm adding my HttpHandler to my configuration (I'm new to this, so the code is a bit disjointed, as I've found it here and there).

public override void FeatureActivated(SPFeatureReceiverProperties properties) {
    var site = (SPSite)properties.Feature.Parent;
    var webApp = site.WebApplication;
    if (!webApp.IsAdministrationWebApplication) {
        var modification = new SPWebConfigModification("add[@name='SharePointNinjectHttpModule']", "configuration/system.web/httpModules");
        modification.Owner = "addSharePointNinjectHttpModule";
        modification.Sequence = 0;
        modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        modification.Value = @"<add name=""SharePointNinjectHttpModule"" type=""Foo.Bar.SharePointNinjectHttpModule,Foo.Bar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=****************"" />";
        webApp.WebConfigModifications.Add(modification);
        try {
            webApp.WebService.ApplyWebConfigModifications();
            webApp.Update();
        }
        catch (SecurityException e) {
            // todo ApplyWebConfigModifications throws "Access Denied" SecurityException when activating via Site Settings
        }
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
    var site = (SPSite)properties.Feature.Parent;
    var webApp = site.WebApplication;
    if (!webApp.IsAdministrationWebApplication) {
        var oCollection = webApp.WebConfigModifications;
        var iStartCount = oCollection.Count;
        for (int c = iStartCount - 1; c >= 0; c--) {
            SPWebConfigModification oModification = oCollection[c];
            if (oModification.Owner == "addSharePointNinjectHttpModule") {
                oCollection.Remove(oModification);
            }
        }

        if (iStartCount > oCollection.Count) {
            try {
                webApp.WebService.ApplyWebConfigModifications();
                webApp.Update();
            }
            catch (SecurityException e) {
                // todo ApplyWebConfigModifications throws "Access Denied" SecurityException when deactivating via Site Settings
            }
        }
    }
}

My SharePoint instance's web.config httpModules section when the feature is not active:

<httpModules>
</httpModules>

And when it is:

<httpModules>
  <add name="SharePointNinjectHttpModule" type="Foo.Bar.SharePointNinjectHttpModule,Foo.Bar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=****************" />
</httpModules>

So it seems like the feature receiver event handlers are doing their job (maybe I'm missing something?).

And here's the HttpModule:

using System;
using System.Web;
using Foo.Bar.Models;
using Ninject;

namespace Foo.Bar {
    public class SharePointNinjectHttpModule : IHttpModule {
        public void Init(HttpApplication context) {
            if (Kernel == null) {
                Kernel = new StandardKernel();
                Kernel.Bind<IRepository>().To<Repository>();
            }
        }

        public static IKernel Kernel { get; private set; }

        public void Dispose() {}

        private static IKernel GetKernel() {
            IKernel result = new StandardKernel();
            result.Bind<IRepository>().To<Repository>();
            return result;
        }

    }
}

The Init() method of my HttpModule never fires. When should I expect it to fire, and why isn't that happening?

Was it helpful?

Solution

The following change to FeatureActivated() resolved the problem I was having:

var modification = new SPWebConfigModification("add[@name='SharePointNinjectHttpModule']", "configuration/system.webServer/modules");

I was injecting the module in the wrong section of my web.config.

Original: configuration/system.web/httpModules
Changed: configuration/system.webServer/modules

Once I made the above change, I was correctly adding the httpModule

Once I was correctly adding the httpModule, my Init() fired immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top