Custom Middleware in Plugin isn't Invoking

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
I have a plugin where i am using custom middleware. I can see its registering at startup (with break point) but invoke is never called. Here it is:

 public class PingerAuthenticationMiddleware
    {
        private readonly RequestDelegate _next;

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

        public async Task Invoke(HttpContext context)
        {
//break point not hitting here
            if (context.Request.Method != HttpMethod.Options.ToString())
            {
                var headers = context.Request.Headers;
                IWorkContext workContext = EngineContext.Current.Resolve<IWorkContext>();
            }
            await _next(context);
        }
}

Extension:
    public static class PingerMiddlewareExtensions
    {
        public static IApplicationBuilder UsePingerAuthenticationMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<PingerAuthenticationMiddleware>();
        }
    }

The startup file:
    public partial class PingerStartup : INopStartup
    {
        public int Order => 10001;

        public void Configure(IApplicationBuilder application)
        {
            application.UsePingerAuthenticationMiddleware();
        }

        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
        }
    }
5 years ago
have you solved it?
5 years ago
Did you get this to work as a plugin ?

Mine is the same - no call to the Invoke - Otherwise works fine when embedded in existing code

namespace Nop.Web.Framework.Infrastructure
{
    public partial class JSONRequestMiddlewareStartup : INopStartup
    {

        public int Order => 10001;
        public void Configure(IApplicationBuilder application)
        {

            // For Handler
            application.MapWhen(context => context.Request.Path.ToString().EndsWith(".report"),
                appBuilder => {
                    appBuilder.UseJSONRequestMiddleware();
                });
        }

        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {

        }

    }

    public static class JSONRequestMiddlewareExtensions
    {
        /// <summary>
        /// Configure middleware checking whether requested page is to provide a JSON response
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public static void UseJSONRequestMiddleware(this IApplicationBuilder application)
        {
            application.UseMiddleware<JSONRequestMiddleware>();
        }

    }

    public partial class JSONRequestMiddleware
    {
        #region Fields

        public readonly RequestDelegate _next;

        #endregion

        #region Ctor

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

        #endregion

        #region Methods

        public async Task Invoke(HttpContext context)
        {

            object rsp = null;
            
            // Do some stuff

            if (rsp == null)
            {
                //call the next middleware in the request pipeline
                await _next(context);
            }
            
            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(JsonConvert.SerializeObject(rsp));
        }

        #endregion
    }
}
5 years ago
NopMvcStartup is the last thing to process with Order = 1000 in a standard nopCommerce setup.
I guess that MVC is the last thing in to be processed since it servers the content to the client.

So, I'm building a plugin right now, and I put my Order to 900 and things work fine.


using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Infrastructure;
using Nop.Plugin.Misc.VerifyCustomerData.Infrastructure.Extensions;

namespace Nop.Plugin.Misc.VerifyCustomerData.Infrastructure
{

    public class VerifyCustomerDataStartup : INopStartup
    {
        public int Order => 900;

        public void Configure(IApplicationBuilder application)
        {
            application.UseVerifyCustomerDataMiddleware();
        }

        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
        }
    }
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.