在我的网站范围功能的功能接收器中使用事件处理程序,我将我的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()方法永远不会触发。当应该我希望它能够解雇时,为什么不发生这种情况?
答案 0 :(得分: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()立即被激活。