Enqueuing a job in VB.Net

Tags: #<Tag:0x00007f4583cc5a90> #<Tag:0x00007f4583cc59a0>

I am downloading a bunch of PDF files using background jobs.

For Each dr As DataRow In dt.Rows
            Dim url As String = dr("file_url").ToString()
            BackgroundJob.Enqueue(Sub() downloadFile(url))
Next

downloadFile(url) looks like this;

Private Sub downloadFile(url As String)
        Dim remoteUri As String = url
        Dim fileName As String = Guid.NewGuid().ToString() + ".pdf"

        Using client As New WebClient()
            client.DownloadFile(remoteUri, Server.MapPath("/Temp/") + fileName)
        End Using
End Sub

When I run, I am getting the following exception:

Value cannot be null. Parameter name: value

What I am doing wrong?

I found the root cause. The subroutine must be public. And, Server relies on current context. Hence, I switched it to HostingEnvironment.MapPath.

It works now!