Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • João Roque 11 posts 95 karma points
    Apr 16, 2021 @ 09:23
    João Roque
    0

    Custom Middleware

    Hi,

    I have the following Configure method on Startup:

        public void Configure(IApplicationBuilder app)
        {
            app.Use(async (context, next) =>
            {
                await next.Invoke();
            });
            app.UseUmbracoBackOffice();                
            app.UseUmbracoWebsite(); 
            app.UseMiddleware<GlobalizationMiddleware>();
        }
    

    If I put a breakpoint in the "await next.Invoke();" it never gets hit nor does the Invoke method on my GlobalizationMiddleware (the constructor does though). I've tried placing it before and after every UseUmbracoxxx callI without any progress. Is it something the umbraco is doing to bypass my middlewares? Is there a way arround it?

    Thanks,

    Joao

  • Benjamin Carleski 33 posts 294 karma points MVP c-trib
    Apr 16, 2021 @ 15:53
    Benjamin Carleski
    0

    It looks like Umbraco registers itself to handle all requests, and when a middleware handles a request, it doesn't need to pass it on to the next middleware :). I would imagine you could register an endpoint of your own, or you could add your own controller. You also could register a middleware using an IStartupFilter, like in the example below, to get into the pipeline before Umbraco.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IStartupFilter, StartupFilter>();
            services.AddUmbraco(_env, _config)
                .AddBackOffice()               
                .AddWebsite()
                .AddComposers()
                .Build();
        }
    
        private class StartupFilter : IStartupFilter
        {
            public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
            {
                app.UseMiddleware<GlobalizationMiddleware>();
                next(app);
            };
        }
    
  • João Roque 11 posts 95 karma points
    Apr 21, 2021 @ 09:12
    João Roque
    0

    Hi,

    I was on a small holiday that's why I'm only answer today.

    Using the startup filter like you said did work but it doesn't work for my use case because I need to access the session and at that time the Session Property returns an InvalidOperationException.

    I'm thinking of using instead a base controller that overrides OnActionExecuting and doing the GlobalizationMiddleware operations there.

    Joao

Please Sign in or register to post replies

Write your reply to:

Draft