简体   繁体   中英

Azure WebJob Throwing HttpRequestException

I'm hosting an application on Azure as a continuous WebJob. The program frequently (around once per second) makes calls to a CosmosDB database by creating a DocumentClient instance (I make use of the DocumentClient function CreateDocumentQuery and a Linq Query on the resultant IEnumerable to retrieve objects from my database). When I run the program locally it behaves as expected without any issues. When I publish the program as an Azure WebJob and run it, my logs indicate that an HttpRequestException is being thrown with the message:

An error occurred while sending the request.

Additionally, I get the following stack trace:

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Microsoft.Azure.Documents.Linq.DocumentQuery1.d__31.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) at ... my calling code...

This problem only seems to occur when I make frequent use of the DocumentClient and only on the WebJob side of things. Running an equivalent load locally does not faze my application. Why is this exception occurring in my WebJob? It might be worth noting that this problem occurs with both the S1 and P1V2 App Service tiers.

DocumentClient shouldn't be used on per-request basis and instead you should use it as a singleton instance in your application. Creating client per-request will add lots of overhead on the latency.

So I'd declare Client property as "static" and initialize it in the constructor of Service. You could call await Client.OpenAsync( ) in the Connect method to "warm" up the client and in each of your public methods directly use the Client instance to call the DocumentDB APIs.

Dispose the Client in the Dispose method of Service.

Those clients are designed to be re-used, so it's recommended that you have a single static instance that you re-use across all functions. Here you can find tips on performance issue:

https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#sdk-usage

Hope that helps!

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