gpt4 book ai didi

asp.net - SharePoint 2013,自定义 IHttpModule NullReferenceException

转载 作者:行者123 更新时间:2023-12-05 08:00:11 26 4
gpt4 key购买 nike

更新:问题已解决。继续阅读。

知道为什么显然不能再添加自定义 IHttpModules 了吗?

我的问题是关于:HttpModule.Init - safely add HttpApplication.BeginRequest handler in IIS7 integrated mode然而,这个问题相当陈旧,没有答案并且没有 SharePoint 上下文。我可以将我的 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: Object reference not set to an instance of an object.] System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification, Boolean isPostEvent) +30
System.Web.PipelineStepManager.ResumeSteps(Exception error) +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)为了优化目的在内部做的事情。这是要记住的关键:)

最佳答案

问题解决了。编辑的问题。请参阅编辑 2013.09.20

关于asp.net - SharePoint 2013,自定义 IHttpModule NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18896066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com