在 asp.net 开发服务器上调试 httpmodule

发布于 2024-07-15 04:13:00 字数 196 浏览 5 评论 0原文

我想在我的 ASP.NET 应用程序(v 3.5,Visual Studio 2008)中集成一些 http 模块,并且我不确定如何在运行 Web 时触发的 ASP.NET 开发服务器中调试或使用此类模块应用程序。

我是否需要在解决方案中包含模块源,或者我可以将 DLL 放入 BIN 中吗? 我来自 1.1 世界,还不习惯 asp.net 开发服务器。

I want to integrate some http modules in my asp.net application (v 3.5, visual studio 2008) and I'm not sure how to debug or use such modules while debugging in the asp.net development server that fires when I run the web app.

Do I need to include the module source in the solution or can I just drop the DLL into BIN? I'm from the 1.1 world and am not yet used to the asp.net development server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我恋#小黄人 2024-07-22 04:13:00

请按照以下步骤添加 HTTP 模块:

  1. 创建一个名为 MyModule 的新 Visual Studio .NET C# 类库项目。
  2. 设置对 System.Web.dll 程序集的引用。
  3. 将以下指令添加到类中:

    使用 System.Web;                     
      
  4. 重命名该类 SyncModule.cs,然后更改类定义以反映这一点。

  5. 实现IHttpModule接口。 您的类定义应如下所示:

    公共类SyncModule:IHttpModule                     
      
  6. 决定您将订阅哪些事件。 以下列表概述了您可以订阅的 HttpApplication 对象中的可用事件:

    • AcquireRequestState:调用此事件以允许模块获取或创建请求的状态(例如会话)。
    • AuthenticateRequest:当安全模块需要在处理请求之前对用户进行身份验证时,调用此事件。
    • AuthorizeRequest:当请求需要授权时,安全模块调用此事件。 身份验证后调用。
    • BeginRequest:调用此事件以通知模块新请求正在开始。
    • Dispose:调用此事件来通知模块应用程序由于某种原因正在结束。 允许模块执行内部清理。
    • EndRequest:调用此事件通知模块请求已结束。
    • Error:调用此事件以通知模块在请求处理过程中发生错误。
    • PostRequestHandlerExecute:调用此事件通知模块处理程序已完成对请求的处理。
    • PreRequestHandlerExecute:调用此事件通知模块即将调用请求的处理程序。
    • PreSendRequestContent:调用此事件通知模块内容即将发送到客户端。
    • PreSendRequestHeaders:调用此事件通知模块 HTTP 标头即将发送到客户端。
    • ReleaseRequestState:调用此事件以允许模块释放状态,因为处理程序已完成对请求的处理。
    • ResolveRequestCache:身份验证后调用此事件。 缓存模块使用此事件来确定请求是否应由其缓存处理,或者处理程序是否应处理该请求。
    • UpdateRequestCache:在处理程序响应后调用此事件。 缓存模块应使用响应更新其缓存。
  7. 实现IHttpModule接口的Init和Dispose方法如下:

    public void Init(HttpApplication 应用程序) 
      { 
         app.BeginRequest += new EventHandler(OnBeginRequest); 
      } 
      公共无效处理(){} 
      
  8. 为事件创建委托,如下所示:

    public delegate void MyEventHandler(Object s, EventArgs e);     
      
  9. 定义 MyEventHandler 类型的私有局部变量来保存对事件的引用:

    private MyEventHandler _eventHandler = null;                     
      
  10. 创建一个事件,将委托挂接到 Global.EventHandler 中的方法。继承自 HttpApplication 对象的 asax 文件或类:

    公共事件 MyEventHandler MyEvent 
      { 
         添加 { _eventHandler += 值;   } 
         删除 { _eventHandler -= 值;   } 
      } 
      
  11. 创建 OnBeginRequest 方法,它挂接到 HttpApplicationBeginRequest 事件:

    public void OnBeginRequest(Object s, EventArgs e) 
      { 
         HttpApplication app = s as HttpApplication; 
         app.Context.Response.Write("来自自定义模块中 OnBeginRequest 的问候。
    "); if(_eventHandler!=null) _eventHandler(this, 空); }
  12. 编译项目

源:http://support.microsoft.com/kb/307996

将 HTTP 模块添加到您的 web.config 中将如下所示:

<system.web>
    <httpModules>
       <add name="CustomHttpModule" type="MyCustomHttpModule"/>
    </httpModules>
</system.web>

Follow these steps to add an HTTP Module:

  1. Create a new Visual Studio .NET C# Class Library project named MyModule.
  2. Set a reference to the System.Web.dll assembly.
  3. Add the following directive to the class:

    using System.Web;                    
    
  4. Rename the class SyncModule.cs, and then change the class definition to reflect this.

  5. Implement the IHttpModule interface. Your class definition should appear as follows:

    public class SyncModule : IHttpModule                    
    
  6. Decide to which events you will subscribe. The following list outlines the available events from the HttpApplication object to which you can subscribe:

    • AcquireRequestState: Call this event to allow the module to acquire or create the state (for example, session) for the request.
    • AuthenticateRequest: Call this event when a security module needs to authenticate the user before it processes the request.
    • AuthorizeRequest: Call this event by a security module when the request needs to be authorized. Called after authentication.
    • BeginRequest: Call this event to notify a module that new request is beginning.
    • Disposed: Call this event to notify the module that the application is ending for some reason. Allows the module to perform internal cleanup.
    • EndRequest: Call this event to notify the module that the request is ending.
    • Error: Call this event to notify the module of an error that occurs during request processing.
    • PostRequestHandlerExecute: Call this event to notify the module that the handler has finished processing the request.
    • PreRequestHandlerExecute: Call this event to notify the module that the handler for the request is about to be called.
    • PreSendRequestContent: Call this event to notify the module that content is about to be sent to the client.
    • PreSendRequestHeaders: Call this event to notify the module that the HTTP headers are about to be sent to the client.
    • ReleaseRequestState: Call this event to allow the module to release state because the handler has finished processing the request.
    • ResolveRequestCache: Call this event after authentication. Caching modules use this event to determine if the request should be processed by its cache or if a handler should process the request.
    • UpdateRequestCache: Call this event after a response from the handler. Caching modules should update their cache with the response.
  7. Implement the Init and Dispose methods of the IHttpModule interface as follows:

    public void Init(HttpApplication app)
    {
       app.BeginRequest += new EventHandler(OnBeginRequest);
    }
    public void Dispose(){ }
    
  8. Create a delegate for an event as follows:

    public delegate void MyEventHandler(Object s, EventArgs e);    
    
  9. Define a private local variable of the type MyEventHandler to hold a reference to the event:

    private MyEventHandler _eventHandler = null;                    
    
  10. Create an event that hooks up the delegate to the method in the Global.asax file or class that inherits from the HttpApplication object:

    public event MyEventHandler MyEvent
    {
       add { _eventHandler += value; }
       remove { _eventHandler -= value; }
    }
    
  11. Create the OnBeginRequest method, which hooks up to the BeginRequest event of HttpApplication:

    public void OnBeginRequest(Object s, EventArgs e)
    {
       HttpApplication app = s as HttpApplication;
       app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
       if(_eventHandler!=null)
          _eventHandler(this, null);
    }        
    
  12. Compile the project

source: http://support.microsoft.com/kb/307996

Adding an HTTP Module to your web.config will look something like the following:

<system.web>
    <httpModules>
       <add name="CustomHttpModule" type="MyCustomHttpModule"/>
    </httpModules>
</system.web>
╰つ倒转 2024-07-22 04:13:00

在 HttpModule 中未命中断点的两个常见原因是:

断点位于模块构造期间调用的代码中。

这个总是让我着迷。 如果您想调试模块的构造您应该手动附加调试器
要在 Windows 7 上执行此操作(在 Vista 上类似):

  1. 调试 | 附加到进程...
  2. 选中在所有会话中显示进程
  3. 选择您的工作进程实例 (w3wp.exe)
  4. 单击附加

刷新浏览器,您现在应该到达断点。

由于配置问题,该模块实际上并未被加载。

如果您可以判断模块代码肯定正在执行,那么这不是问题。 但有时,虽然模块在其他计算机上加载正常,但它可能无法加载到您的开发盒上,因为您有不同版本的 IIS 并且没有必要的配置。

如果是这种情况,请确保该模块针对所有所需的 IIS 版本正确连接。

对于 IIS 5.1 和 IIS 6...

<system.web>
  <httpModules>
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </httpModules>
</system.web>

...以及 IIS 7+

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </modules>
</system.webServer>

Two common reasons for breakpoints not being hit in an HttpModule are:

The breakpoint is in code that is called during the module's construction.

This one always get's me. If you want to debug the module's construction you should manually attach the debugger.
To do this on Windows 7 (it'll be similar on Vista):

  1. Debug | Attach to Process...
  2. Check Show processes in all sessions.
  3. Select the your worker process instance (w3wp.exe)
  4. Click Attach.

Refresh your browser and you should now hit your breakpoint.

The module is not actually being loaded because of a configuration issue.

If you can tell that the module code is definitely being executed then this is not the problem. But sometimes, while a module is loading fine on other machines it might not be loading on your dev box because you have a different version of IIS and you don't have the necessary config.

If this is the the case then make sure the module is wired up correctly for all required versions of IIS.

For IIS 5.1 and IIS 6...

<system.web>
  <httpModules>
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </httpModules>
</system.web>

...and for IIS 7+

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </modules>
</system.webServer>
葬﹪忆之殇 2024-07-22 04:13:00

请注意,这似乎并不是开箱即用的。 确保不仅包含新模块的类名,还显式定义其命名空间和(为了完整性)程序集。 如果您未能做到这一点,则很可能最终会导致 IIS 抱怨无法加载您刚刚编码的新 HTTP 模块。

下面是一个例子。

而不是:

<add name="CustomHttpModule" type="MyCustomHttpModule"/>

使用:

<add name="CustomHttpModule" type="MyCustomRootNamespace.MyCustomHttpModule, MyCustomAssembly"/>

如果不这样做,您的项目可能无法加载。

您可能收到的具体错误取决于您定义模块的明确程度。 如果省略命名空间,您将看到:

Could not load type 'MyCustomHttpModule'.

省略命名空间并包含程序集,您将看到:

Could not load type 'MyCustomHttpModule' from assembly 'MyCustomAssembly'. 

如果您看到另一条错误消息,则您遇到的问题与此处讨论的问题不同。

Take note that this doesn't appear to work out of the box. Make sure to not just include the new module's class name but instead explicitly define its namespace and (for the heck of completeness) assembly. If you fail to do that, there's a good chance you'll end up with IIS complaining about not being able to load that new HTTP module you just coded.

An example follows.

Instead of:

<add name="CustomHttpModule" type="MyCustomHttpModule"/>

Use:

<add name="CustomHttpModule" type="MyCustomRootNamespace.MyCustomHttpModule, MyCustomAssembly"/>

Fail to do that and your project may fail to load.

Specific errors you may receive depend on how explicitly you've defined the module. If you leave out the namespace, you'll see:

Could not load type 'MyCustomHttpModule'.

Leave out the namespace and include the assembly and you'll instead see:

Could not load type 'MyCustomHttpModule' from assembly 'MyCustomAssembly'. 

If you instead see another error message, you're suffering from a different problem than the one discussed here.

沉鱼一梦 2024-07-22 04:13:00

您可以编写 System.Diagnostics.Debugger.Break

(); 行 //显式调用Break进行调试,

在HttpModule构建过程中显式调用断点。

它将要求附加进程(例如 w3wp.exe),然后调试点将命中。

谢谢

You can write the line

System.Diagnostics.Debugger.Break(); //Explicitly calling Break to debug

to explicitly call the break point during the HttpModule consturction .

It will ask to attach the process (e.g. w3wp.exe) then the debug point will hit .

Thanks

远昼 2024-07-22 04:13:00

您可能还想确保您使用的是 Web 应用程序项目(即您在 VS2003 中使用的项目),而不是网站“项目”,因为网站“项目”不是项目。

You may also want to make sure you are using web application projects, which is what you were using in VS2003, and not web site "projects", which are not projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文