Properly getting connection string from web.config inside job class

hi friends,
is it safe to use web.config connection string in background jobs like following or somehow is it possible that this job could not find web.config file under specific conditions. I’m asking because I read that hangfire can run jobs in different processes. I never faced the problem but I need to know. Maybe under heavy load hangfire tries to run my class “out of context” or “out of application” without proper web.config ? I believe its not possible but I need confirmation, please.


I’m calling the job:

BackgroundJob.Enqueue(Sub() HangfireFunctionsClass.myExecute_HF(sqlstr))

and the class is:

Public Class HangfireFunctionsClass

Shared sub myExecute_HF(ByVal sqlstr As String) 

    ' Is the following line is okay in this class (for hangfire of course) ?

    dim CONN_STR = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionStringNET").ConnectionString

    Using cn As New SqlConnection(DBClass.ConnectionString_)
        Using cmd As New SqlCommand(sqlstr, cn)
        End Using
    end using    
End Function

End Class

Hangfire runs your jobs in the process where you have your new BackgroundJobServer( ) call.

In our case, we enqueue background jobs in one application, and process the queue in another application, and then it is not possible to ‘share’ connection string this way. Then you would have a web.config or app.config in both applications, and having the same connection string in both config files.

Having a generic Execute whatever string I get-hangfire task seems a little bold, though… I would rather have created different hangfire jobs for different sql statements, and let the hangfire job implementation be the ‘owner’ of the sql and how it is executed.

There is a similar question answered already btw. Take a look: Access Web.Config section from HangFire Job