简体   繁体   English

Azure WebJob 抛出 HttpRequestException

[英]Azure WebJob Throwing HttpRequestException

I'm hosting an application on Azure as a continuous WebJob.我在 Azure 上托管一个应用程序作为一个连续的 Web 作业。 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).该程序经常(大约每秒一次)通过创建 DocumentClient 实例来调用 CosmosDB 数据库(我使用 DocumentClient 函数 CreateDocumentQuery 和结果 IEnumerable 上的 Linq 查询来从我的数据库中检索对象)。 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:当我将程序发布为 Azure WebJob 并运行它时,我的日志表明正在抛出 HttpRequestException 消息:

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...在 System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) 在 System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) 在 System.Threading.Tasks.Task1.get_Result() 在 Microsoft.Azure.Documents.Linq.DocumentQuery1 .d__31.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) 在...我的调用代码...

This problem only seems to occur when I make frequent use of the DocumentClient and only on the WebJob side of things.这个问题似乎只发生在我频繁使用 DocumentClient 并且只在 WebJob 方面。 Running an equivalent load locally does not faze my application.在本地运行等效负载不会影响我的应用程序。 Why is this exception occurring in my WebJob?为什么在我的 WebJob 中会发生此异常? It might be worth noting that this problem occurs with both the S1 and P1V2 App Service tiers.可能值得注意的是,S1 和 P1V2 应用服务层都会出现此问题。

DocumentClient shouldn't be used on per-request basis and instead you should use it as a singleton instance in your application. DocumentClient 不应在每个请求的基础上使用,而应将其用作应用程序中的单例实例。 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.因此,我将 Client 属性声明为“静态”并在 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.您可以在 Connect 方法中调用await Client.OpenAsync( ) 来“预热”客户端,并在您的每个公共方法中直接使用 Client 实例来调用 DocumentDB API。

Dispose the Client in the Dispose method of Service.在 Service 的 Dispose 方法中处置 Client。

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 https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#sdk-usage

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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