Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

How to use HttpResponseFeature in a ASP.net Core Middleware

I just found out that "RequestFeatures" is a thing in ASP.net Core, news to me.. Anyways..

I have created a middleware that needs to alter the response body and the headers provided with the response.

Im used to do this by alterntiv it directly on the HttpContext context-object provided as an argument to the Invoke-method.. however I read somewhere that context.Features is the way to go due to optimizations and what not.. (is this true?, I get that its alot easiter to test the Features, than to "mock" an entire HttpContext which has been historically a painful thing to do..) So I created my own implementation of the HttpResponseFeature and registred it in my Invoke method using:

        httpContext.Features.Set<IHttpResponseFeature>
        (
            //Registering my own HttpResponseFeature that takes an argument..
            new MyHttpResponseFeature(httpResponseMessage)
        );

However, the OnCompleted or the OnStarting-methods never runs. I have added a few breakpoints to validate this, but the breakpoints are never hit. Am I missing something?

question from:https://stackoverflow.com/questions/65910746/how-to-use-httpresponsefeature-in-a-asp-net-core-middleware

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Turns out that the FeatureCollection/RequestFeatures is a "new" thing if your building your very own custom HTTP server ontop of ASP.net Core. An article that covers this fairly well is this one: https://reynders.co/use-iserver-from-aspnet-core-to-create-your-own-web-server/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...