reactjsreduxendpointhangfirevisual-studio-templates

Access HangFire endpoint from Visual Studio 2019 React template project


Following the guide at https://medium.com/@jamesdale1993/asp-net-core-2-with-signalr-and-react-redux-a-simple-example-c25ea6b19dbe I created a project with Visual Studio 2019 and the react-redux template (core 3.0 project).

I got the SignalR up and running as per article, react frontend connected to SignalR endpoint in backend code.

I then installed HangFire (https://www.hangfire.io/), set it up, and the recurring job set up for testing is working correctly. I can see (in database) the recurring job firing every minute as it should.

My problem: I cannot access HangFire Dashboard! By default it should be http://localhost:55663/hangfire, but I see only header of template project.

I guess it is some kind of routing issue as I can only access what is served in ClientApp/build folder (react).

Any help would be greatly appreciated.

I've tried to change the default /hangfire endpoint by setting app.UseHangfireDashboard("/jobs"); in Startup.cs Configure section, same result.

In Startup.cs code below, search for "Hangfire" for relevant sections.

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();
            services.AddControllersWithViews();

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });

            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

            // Database connection
            services.AddTransient<IDbConnection>((sp) =>
                new SqlConnection(Configuration.GetConnectionString("TestProjectConnection"))
            );

            // Hangfire
            services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("TestProjectConnection")));
            services.AddHangfireServer();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        [System.Obsolete]
        public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseCors("MyPolicy");
            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseRouting();

            //Hangfire endpoint
            app.UseHangfireDashboard("/jobs");
            //backgroundJobs.Enqueue(() => Debug.WriteLine("Hello world from Hangfire!"));
            RecurringJob.AddOrUpdate<ITestClass>("TestMethod", x => x.WriteMessage("Testing"), Cron.MinuteInterval(1));

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });

            app.UseSignalR(routes =>
            {
                routes.MapHub<SignalRCounter>("/signalrcounter");
            });


            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp/build";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });


        }

Solution

  • It was a matter of order of the routes.

    Working with:

    public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseCors("MyPolicy");
                //app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseSignalR(routes =>
                {
                    routes.MapHub<SignalRCounter>("/signalrcounter");
                });
    
    
                app.UseHangfireDashboard("/hangfire");
                app.UseRewriter();
    
                app.UseAuthentication();
                app.UseSpa(spa =>
                {
                    spa.Options.SourcePath = "ClientApp/build";
    
                    if (env.IsDevelopment())
                    {
                        spa.UseReactDevelopmentServer(npmScript: "start");
                    }
                });
    
                app.UseHangfireServer();
    
                //backgroundJobs.Enqueue(() => Debug.WriteLine("Hello world from Hangfire!"));
                RecurringJob.AddOrUpdate<ITestClass>("TestMethod", x => x.WriteMessage("Testing"), Cron.MinuteInterval(1));
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller}/{action=Index}/{id?}");
                });
            }