Search code examples
c#asp.netvb.netbackground-processhangfire

Hangfire and VB.NET - Gettings things configured in the Application Startup class


Earlier this week I ran across Scott Hanselman's post about background processing in ASP.NET (http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx). I've written routines in the past that automatically hits a page every so often to run some tasks, but this background processing idea is something I never even considered, and Hangfire (http://hangfire.io/) looks perfect for my background processing needs (mainly sending emails).

To get HangFire going (after installing it via NuGet) I need to get it started up in the Application Startup class. The documentation provides C# code that I'm not sure how to convert to VB.NET. It looks like some lambda code, which I'm somewhat familiar with through my use of LINQ, but I can't quick figure this one out.

public void Configure(IAppBuilder app)
{
    app.UseHangfire(config =>
    {
        config.UseSqlServerStorage("<connection string or its name>");
        config.UseServer();
    });
}

This is where I'm starting, which is basically next to nothing, I know... :)

Public Sub Configuration(app As IAppBuilder)
     'This is where I know this code should go... 
End Sub

I'd appreciate some direction or explanation as to what's going on here and how to port this code to VB.NET. Thanks!


Solution

  • You should be able to write that as:

    Public Sub Configure(app As IAppBuilder)
        Dim act = Sub(config As IBootstrapperConfiguration)
                    config.UseSqlServerStorage("<...>")
                    config.UseServer()
                  End Sub
    
        app.UseHangfire(act)
    End Sub
    

    For more on creating multi line lambdas in VB.Net, refer to the MSDN.