更新:问题解决了。请继续阅读。
知道为什么显然不再可能添加自定义IHttpModules了吗?
我的问题与:HttpModule.Init - safely add HttpApplication.BeginRequest handler in IIS7 integrated mode有关 但是,这个问题相当陈旧,没有答案,也没有SharePoint上下文。我 CAN 将我的HttpModule添加到任何标准的ASP.NET WebForms页面。
SharePoint在IIS 8中托管.AppPool在集成模式下运行。框架级别为4.0 +。
namespace My.Namespace
{
using System;
using System.Web;
public class CustomHttpModule : IHttpModule
{
private static readonly object mutex = new object();
private static bool _isInitialized;
public void Init(HttpApplication context)
{
if (!_isInitialized)
{
lock (mutex)
{
if (_isInitialized) return;
context.BeginRequest += BeginRequest;
_isInitialized = true;
}
}
}
private void BeginRequest(object sender, EventArgs e)
{
}
public void Dispose()
{
}
}
}
结果:
[NullReferenceException:对象引用未设置为对象的实例。] System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification,Boolean isPostEvent)+30
System.Web.PipelineStepManager.ResumeSteps(异常错误)+1098
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context,AsyncCallback cb)+135
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)+604
web.config如下所示:
<system.webServer>
<!-- further elements omitted -->
<modules runAllManagedModulesForAllRequests="true">
<remove name="AnonymousIdentification" />
<remove name="FileAuthorization" />
<remove name="Profile" />
<remove name="WebDAVModule" />
<remove name="Session" />
<add name="SPNativeRequestModule" preCondition="integratedMode" />
<add name="SPRequestModule" preCondition="integratedMode" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="SharePoint14Module" preCondition="integratedMode" />
<add name="StateServiceModule" type="Microsoft.Office.Server.Administration.StateModule, Microsoft.Office.Server, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="PublishingHttpModule" type="Microsoft.SharePoint.Publishing.PublishingHttpModule, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="DesignHttpModule" preCondition="integratedMode" type="Microsoft.SharePoint.Publishing.Design.DesignHttpModule, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="FederatedAuthentication" type="Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="SessionAuthentication" type="Microsoft.SharePoint.IdentityModel.SPSessionAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="SPWindowsClaimsAuthentication" type="Microsoft.SharePoint.IdentityModel.SPWindowsClaimsAuthenticationHttpModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="SPApplicationAuthentication" type="Microsoft.SharePoint.IdentityModel.SPApplicationAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
<add name="CustomModule" type="My.Namespace.CustomHttpModule, My.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=066b2229567b6747" />
</modules>
<!-- further elements omitted -->
</system.webServer>
只要我不再附加到BeginRequest事件,页面就会再次运行。但显然我的http模块变得毫无用处。
编辑2013.09.19:在应用程序启动时调用Init()两次。如果我仅在第二次调用时附加我的事件,则应用程序可以正常运行,但事件不会触发。
编辑2013.09.20:问题可能已经解决了。我的Init()方法被触发两次(并且之间没有调用Dispose()这一事实)导致我假设实际上可能存在我的IHttpModule的两个共存实例。虽然我之前的观察显示第二个实例可以附加事件(不触发,但也没有对应用程序产生负面影响) - 显然不是真的反之亦然(我的静态_isInitialized“锁定”就是这种情况)
**因此IHttpModule的两个实例都需要具有相同的“配置”(附加了事件处理程序)。运行多个HttpApplication实例是完全正常的。这就是ASP.NET(或IIS)在内部进行优化的目的。这是要记住的关键事项:)
答案 0 :(得分:0)
问题解决了。编辑问题。请参阅修改2013.09.20 。