从Web App

时间:2018-05-08 04:48:12

标签: web-services azure asp.net-web-api azure-web-sites azure-cosmosdb

我们有一个部署到Azure的Web服务( Azure App Service ),它通过用于Cosmos DB / Document DB的标准C#SDK与我们的Azure Cosmos DB进行通信。

两者 - 应用服务和Cosmos数据库帐户/集合 - 位于Azure的相同资源组和相同位置。

对于Web服务对Cosmos DB执行大量请求的某些批量操作,我们在与数据库通信时经常会遇到Web服务错误:

System.AggregateException: One or more errors occurred. --->    System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalBind(EndPoint localEP)
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
 --- End of inner exception stack trace ---

[...]

at Microsoft.Azure.Documents.Query.ProxyDocumentQueryExecutionContext.<ExecuteNextAsync>d__0.MoveNext()<---

我们的每个ApiController实例都静态地分配一个存储库类,而后者又在其构造函数中获取IReliableReadWriteDocumentClient实例并通过

保存它的整个生命周期。
  IDocumentDbInitializer dbinit = new DocumentDbInitializer();
  Client = dbinit.GetClient(endpointUrl, myAuthKey, connectionPolicy);

因此,根据我的理解,我们应该只为整个Web服务中的2个存储库使用2个Document DB客户端。

到目前为止我们尝试过的事情:

  • 在批量操作期间将客户端上的请求限制为小于3 / s
  • 将客户ConnectionPolicy.MaxConnectionLimit从默认(50)减少到20
  • 增加应用ServicePointManager.DefaultConnectionLimit

这些措施都没有显着减少我们遇到的例外情况。

有关如何避免此错误的建议吗?
其他 Cosmos DB SDK 功能可以调整/配置/适应我们的用例..?

0 个答案:

没有答案