0
\$\begingroup\$

This code is asp.net core middleware written in f# and called from c# for a blazor server-side app. How can this be made more efficient if any in regards to the async code.

F#:

type CheckMaintenanceStatusMiddleWare(next : RequestDelegate) =

    let _next = next

    member this.InvokeAsync(context : HttpContext) =

        let statusCheck = true
        if statusCheck
        then 
            Task.Run(fun arg -> context.Response.Redirect("/Maintenance"))
        else 
           _next.Invoke(context)

[<Extension>]
type CheckMaintenanceStatusMiddleWareExtensions() =

    [<Extension>]
    static member inline UseCheckMaintenanceStatus(builder : IApplicationBuilder) =

        builder.UseMiddleware<CheckMaintenanceStatusMiddleWare>() 

C#

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCheckMaintenanceStatus();

            var connectionString = Configuration.GetConnectionString("DefaultConnection");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            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.UseHttpsRedirection();
            app.UseStaticFiles();

            //app.UseCookiePolicy();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }

Razor Component:

@page "/Maintenance"

<h3>Maintenance</h3>

@code {

}
\$\endgroup\$

closed as unclear what you're asking by t3chb0t, dfhwze, IEatBagels, Linny, pacmaninbw Oct 7 at 17:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ @CaringDev please explain, thanks. \$\endgroup\$ – user1206480 Oct 7 at 6:29
  • 1
    \$\begingroup\$ Hi, you should explain what your code does in your post, otherwise it's hard to tell if it can be improved. \$\endgroup\$ – IEatBagels Oct 7 at 14:27
  • \$\begingroup\$ @IEatBagels, do you have experience with Asp.net core 3? Specifically middleware, and async code? \$\endgroup\$ – user1206480 Oct 13 at 0:44
2
\$\begingroup\$

Task.Run schedules the work specified by the lambda to be executed on the thread pool. Scheduling the work and managing the thread pool is costly in terms of CPU time, allocations and possible thread starvation. Task.Run is useful for CPU-bound tasks but usually a smell in asynchronous code in web applications. Most of the time it hints at misunderstanding of async vs. parallel. As context.Response.Redirect("/Maintenance") is not async by itself, there is no need to block/wait/schedule work. Instead, you can synchronously instruct the response to redirect and return a premade, completed task:

context.Response.Redirect("/Maintenance")
Task.CompletedTask

or return a result directly

Task.FromResult(response)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.