在 asp.net 开发服务器上调试 httpmodule
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请按照以下步骤添加 HTTP 模块:
MyModule
的新 Visual Studio .NET C# 类库项目。将以下指令添加到类中:
重命名该类
SyncModule.cs
,然后更改类定义以反映这一点。实现
IHttpModule
接口。 您的类定义应如下所示:决定您将订阅哪些事件。 以下列表概述了您可以订阅的
HttpApplication
对象中的可用事件:AcquireRequestState
:调用此事件以允许模块获取或创建请求的状态(例如会话)。AuthenticateRequest
:当安全模块需要在处理请求之前对用户进行身份验证时,调用此事件。AuthorizeRequest
:当请求需要授权时,安全模块调用此事件。 身份验证后调用。BeginRequest
:调用此事件以通知模块新请求正在开始。Dispose
:调用此事件来通知模块应用程序由于某种原因正在结束。 允许模块执行内部清理。EndRequest
:调用此事件通知模块请求已结束。Error
:调用此事件以通知模块在请求处理过程中发生错误。PostRequestHandlerExecute
:调用此事件通知模块处理程序已完成对请求的处理。PreRequestHandlerExecute
:调用此事件通知模块即将调用请求的处理程序。PreSendRequestContent
:调用此事件通知模块内容即将发送到客户端。PreSendRequestHeaders
:调用此事件通知模块 HTTP 标头即将发送到客户端。ReleaseRequestState
:调用此事件以允许模块释放状态,因为处理程序已完成对请求的处理。ResolveRequestCache
:身份验证后调用此事件。 缓存模块使用此事件来确定请求是否应由其缓存处理,或者处理程序是否应处理该请求。UpdateRequestCache
:在处理程序响应后调用此事件。 缓存模块应使用响应更新其缓存。实现IHttpModule接口的Init和Dispose方法如下:
为事件创建委托,如下所示:
定义
MyEventHandler
类型的私有局部变量来保存对事件的引用:创建一个事件,将委托挂接到 Global.EventHandler 中的方法。继承自
HttpApplication
对象的 asax 文件或类:创建
OnBeginRequest
方法,它挂接到HttpApplication
的BeginRequest
事件:编译项目
源:http://support.microsoft.com/kb/307996
将 HTTP 模块添加到您的
web.config
中将如下所示:Follow these steps to add an HTTP Module:
MyModule
.System.Web.dll
assembly.Add the following directive to the class:
Rename the class
SyncModule.cs
, and then change the class definition to reflect this.Implement the
IHttpModule
interface. Your class definition should appear as follows: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.Implement the Init and Dispose methods of the IHttpModule interface as follows:
Create a delegate for an event as follows:
Define a private local variable of the type
MyEventHandler
to hold a reference to the event:Create an event that hooks up the delegate to the method in the Global.asax file or class that inherits from the
HttpApplication
object:Create the
OnBeginRequest
method, which hooks up to theBeginRequest
event ofHttpApplication
:Compile the project
source: http://support.microsoft.com/kb/307996
Adding an HTTP Module to your
web.config
will look something like the following:在 HttpModule 中未命中断点的两个常见原因是:
断点位于模块构造期间调用的代码中。
这个总是让我着迷。 如果您想调试模块的构造您应该手动附加调试器。
要在 Windows 7 上执行此操作(在 Vista 上类似):
刷新浏览器,您现在应该到达断点。
由于配置问题,该模块实际上并未被加载。
如果您可以判断模块代码肯定正在执行,那么这不是问题。 但有时,虽然模块在其他计算机上加载正常,但它可能无法加载到您的开发盒上,因为您有不同版本的 IIS 并且没有必要的配置。
如果是这种情况,请确保该模块针对所有所需的 IIS 版本正确连接。
对于 IIS 5.1 和 IIS 6...
...以及 IIS 7+
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):
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...
...and for IIS 7+
请注意,这似乎并不是开箱即用的。 确保不仅包含新模块的类名,还显式定义其命名空间和(为了完整性)程序集。 如果您未能做到这一点,则很可能最终会导致 IIS 抱怨无法加载您刚刚编码的新 HTTP 模块。
下面是一个例子。
而不是:
使用:
如果不这样做,您的项目可能无法加载。
您可能收到的具体错误取决于您定义模块的明确程度。 如果省略命名空间,您将看到:
省略命名空间并包含程序集,您将看到:
如果您看到另一条错误消息,则您遇到的问题与此处讨论的问题不同。
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:
Use:
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:
Leave out the namespace and include the assembly and you'll instead see:
If you instead see another error message, you're suffering from a different problem than the one discussed here.
您可以编写 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
您可能还想确保您使用的是 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.