在我的站点范围功能的功能接收器中使用事件处理程序,我将我的 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() 方法永远不会触发。我应该期待它什么时候触发,为什么没有发生呢?