Gargoyle
Gargoyle

Reputation: 10324

Can I skip the 'await' on HttpWebRequest's GetResponseAsync()

In my Web Api 2 method I'm doing some work and then I need to hit another site's REST endpoint, so I'm using HttpWebRequest to send the message. However, that external URL is going to process for 30 minutes before it returns (I don't own the external site). I don't want the caller of my method to have to wait for that return to complete.

Besides the obvious "if the other site fails you don't know" is there any downside to not doing an await on the request.GetResponseAsync() method and just letting my code return?

Upvotes: 1

Views: 129

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456527

is there any downside to not doing an await

Possibly. When your local HttpWebRequest is disposed, it will probably notify the remote API, which is generally interpreted as a "cancel" by web servers. Your local HttpWebRequest could be finalized when your app is unloaded/recycled, which happens periodically on ASP.NET/IIS.

For this reason, I would recommend a standard queue-with-independent-backend solution for this: have your API place the request in a queue, and have a separate backend process do the long-running API calls.

Upvotes: 2

Related Questions