简体   繁体   中英

.NET Web Application Async call working locally from VS workspace code, but not from server

I have written code to get data from an API. I am using an Async method to handle the call and processing of the retrieved data. The code is triggered by the user clicking a Login button on their Start page. While the Async method is running in the background, the user is redirected to another .NET web application page, their Homepage.

All of the code works perfectly when I build and run the website locally from VS, the data is returned and my code processes the data, adding it to various SQL tables.

myApiIntegration.Log($"GetDataFromAPI({querystring}, {displayInConsole}): Getting data from API")

Dim response As HttpResponseMessage = Await myApiRequest.GetResponse(querystring)

' only continue if request was successful, otherwise log an error
If Not response.IsSuccessStatusCode Then
      myApiIntegration.Log("ERROR: API request failed: " & response.ReasonPhrase)
      Return Nothing
Else
      myApiIntegration.Log("API request successful")
...

I have checked my code in to a Test server. The site builds, and thanks to some logging I've put in, I can see that my code is triggered when the user clicks the Login button, right up to the API call itself: I can see the Request object is being initialised using the correct headers etc. The last log message I can see is created by the last line before the API call itself. The user is still redirected to their homepage.

After that, I cannot see any messages from my Async code. The next message I would expect to see would be either "API call successful" or "API call failed", depending on the response from the API (these are messages I'm logging from my code via Graylog, and again, they work locally); but there are no further messages, following the one telling me it is about to make the API call.

I have checked the server logs for errors. There are none at the time of logging in and running the API code. I have asked our Infrastructure guy to checked the firewall for messages - he couldn't see any - but he couldn't see any when the request was sent out from my local code either (and this worked). I have checked that my code is identical on my local workspace copy and on the server. It is.

Could anyone advise me what I should be looking at, what might be wrong, or what other avenues I could try? It's a Windows Server 2016 Datacenter VM. The application is a .NET Framework web app, written in VB.NET.

Fixed. My async method worked perfectly in my local workspace, but it needed to be queued as a "background work item" in order to run on the server. I used QueueBackgroundWorkItem to do this, after upgrading the framework to .NET 4.5.2 because HostingEnvironment in the older framework my web app was targeting did not support the QueueBackgroundWorkItem method.

                Dim ct As New System.Threading.CancellationToken
                Dim func As Func(Of System.Threading.CancellationToken, Task(Of MyObjects)) = Function() myApiIntegration.GetDataFromAPI(Int(idStr))
                HostingEnvironment.QueueBackgroundWorkItem(func)

The proper solution for request-extrinsic code is to use a durable queue with a background service . While an in-memory work queue like HostingEnvironment.QueueBackgroundWorkItem will operate correctly when everything is fine, it can lose work in the event of a server failure or rolling upgrades.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM