0

在我的站点范围功能的功能接收器中使用事件处理程序,我将我的 HttpHandler 添加到我的配置中(我是新手,所以代码有点脱节,因为我在这里和那里找到了它)。

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

当功能不活动时,我的 SharePoint 实例的 web.config httpModules 部分:

<httpModules>
</httpModules>

当它是:

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

所以看起来功能接收器事件处理程序正在做他们的工作(也许我错过了一些东西?)。

这是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;
        }

    }
}

我的 HttpModule 的 Init() 方法永远不会触发。我应该期待它什么时候触发,为什么没有发生呢?

4

1 回答 1

0

对 FeatureActivated() 的以下更改解决了我遇到的问题:

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

我在 web.config 的错误部分中注入了模块。

原文:configuration/system.web/httpModules
更改:configuration/system.webServer/modules

完成上述更改后,我正确添加了 httpModule

一旦我正确添加了 httpModule,我的 Init() 就会立即触发。

于 2012-05-10T18:25:43.973 回答