skip to Main Content

I have a .net framework 4.7 project which is essentially an HTTPmodule to be used independantly in IIS server to detect incoming requests and responses, to 3rd party applications installed in IIS,which then need to be modified.

We were adding the module using the webconfig for the respective applications.However now i am migrating it to .net core 6.we use a sample web api to test the httpmodule project.However it seems the addition has to be in startup.cs of the sample web app.

Issue:-
1.I add a middlware to the 1st project like below

 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Http;
 using System.Threading.Tasks;

namespace Myspace.HttpModule.Example
{
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    public class MyMiddleware
    {
        private readonly RequestDelegate _next;
        

        public MyMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public Task Invoke(HttpContext httpContext)
        {

            

            return _next(httpContext);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class MyMiddlewareExtensions
    {
        public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<MyMiddleware>();
        }
    }
}

2.Now i need to add this to the 2nd project which is the sample web app to test the HTTPModule.However when i try to add it to the startup.cs file that i created in the sample web app project

using Myspace.HttpModule.Example;
using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();



var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
    
}



app.UseAuthorization();

app.UseMiddleware<MyMiddleware>();

app.UseMyMiddleware();

app.MapControllers();

app.Run();

I get compilation errors at

app.UseMiddleware<MyMiddleware>();
    
app.UseMyMiddleware();

So is it that i cant add like this.Wouldnt "using" work in this case?
Also is this the only way to add a middleware to be able to get requests globally or from the specific sample or 3rd party app?
I googled a lot but couldn’t find piece together a definite answer. I am a newbie to .Net

The error is like below

enter image description here

Update:-

The error image is

enter image description here

2

Answers


  1. the method should be included in

    namespace Microsoft.AspNetCore.Builder

    assembly:Microsoft.AspNetCore.Http.Abstractions.dll
    enter image description here
    which was included in Microsoft.AspNetCore.app in your project when you created the project :

    enter image description here

    please check the framewok if there’s something wrong with it

    Login or Signup to reply.
  2. your screenshot is pointing to a spelling mistake. its UseMiddleware not Middelware as it can be seen in your screenshot.
    please check if you are using correct name.

    and also you only need to use either

    app.UseMiddleware<MyMiddleware>();
    

    or

    app.UseMyMiddleware();
    

    not both.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search