Porting HttpModule .Net Class Library to .Net Core

2019-07-18 23:02发布

问题:

I am migrating a project from .net Web Application to .Net core Web API.

I am using HTTP Module which is .net framework class library, in IIS Integrated mode.

So, thought to port as it is, to my new core app.

I pasted web.config to my new core app and added a project reference to that Http Module and it started working.

I am having some Context.Response.Write in my BeginRequest method, and I can see those lines whenever I am making a call to my web api.

But, I am having some questions here.

  1. No break point is hitting in the module.
  2. I thought no symbols were loaded, so I looked into the Modules window in VS but no such module is loaded.
  3. Any way to load the symbols when the non core projects loaded into core projects.
  4. Wondering how, without loading a reference, the module is invoking.

Could some one share some inputs on above points.

Thx.

回答1:

HTTP Modules are a concept of the classic ASP.NET / IIS application model.

ASP.NET Core does not use IIS' event system, the requests that IIS received are forwarded to the ASP.NET Core app, processed by this application and returned.

If you are using an http module inside a web.config, there isn't a good guarantee that it will have a chance to process parts of the request/response at the right times. Also, you will not be able to host the app outside of IIS if you ever get the module ordering working correctly - you would effectively be running ASP.NET Core and ASP.NET side-by-side.

If you want to port module-like functionality to an ASP.NET Core app, the best place to start is a Middleware. A middleware in ASP.NET Core is part of the processing pipeline and gets to process incoming requests and outgoing response at a point in the pipeline defined by the application.



回答2:

ASP.NET Core has no support for HTTP modules, the pipeline has been completely redesigned. It's only being invokved because you told IIS to run it, but it won't have any visibility as far as your application is concerned (IIS is "external" to your application in the Core world).

Microsoft has some decent documentation around converting to the new middleware system here (far too much to re-post or condense into an answer):

https://docs.microsoft.com/en-us/aspnet/core/migration/http-modules