c# - System.Net.WebException:远程服务器返回错误:(429)请求太多
问题描述
我正在使用 ASP.NET Core 2.2、GraphQL.NET、CosmosDB、EntityFrameworkCore (Microsoft.EntityFrameworkCore.Cosmos(2.2.4) 进行 API 开发项目。
在运行解决方案时,我在 VS2019 的输出窗口中看到一个错误:
System.Net.WebException: The remote server returned an error: (429) Too Many Requests.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClient.CreateDocumentCollectionIfNotExistsOnce(DbContext _, String collectionId)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 119926.3431ms 200 application/json
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:53116/graphql application/json 1468
Microsoft.EntityFrameworkCore.Infrastructure:Information: Entity Framework Core 2.2.4-servicing-10062 initialized 'TaxathandDbContext' using provider 'Microsoft.EntityFrameworkCore.Cosmos' with options: ServiceEndPoint=https://taxathanddb.documents.azure.com/ Database=TaxathandDb
Microsoft.EntityFrameworkCore.Infrastructure:Information: A transient exception has been encountered during execution and the operation will be retried after 9031ms.
System.Net.WebException: The remote server returned an error: (429) Too Many Requests.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClient.CreateDocumentCollectionIfNotExistsOnce(DbContext _, String collectionId)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
启动.cs
public void ConfigureServices(IServiceCollection services)
{
string serviceEndPoint = Configuration.GetValue<string>("CosmosDBEndpoint");
string authKeyOrResourceToken = Configuration.GetValue<string>("CosmosDBAccessKey");
string databaseName = Configuration.GetValue<string>("CosmosDBName");
services.AddEntityFrameworkCosmos();
services.AddScoped<DbContext, SampleDbContext>();
services.AddDbContext<TaxathandDbContext>(options => options.UseCosmos(serviceEndPoint, authKeyOrResourceToken, databaseName, contextOptions =>
{
contextOptions.ExecutionStrategy(d => new CosmosExecutionStrategy(d));
}
));
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDataLoaderContextAccessor, DataLoaderContextAccessor>();
services.AddSingleton<DataLoaderDocumentListener>();
services.AddSingleton<IDocumentWriter, DocumentWriter>();
services.AddScoped<IUtilityService, UtilityService>();
services.AddScoped<ICommonService, CommonService>();
services.AddScoped<ICountryService, CountryService>();
services.AddScoped<CountryResultType>();
services.AddScoped<GraphQLQuery>();
services.AddScoped<ICountriesResolver, CountriesResolver>();
services.AddScoped<CountryType>();
services.AddScoped<Response>();
services.AddScoped(typeof(ResponseGraphType<>));
services.AddScoped(typeof(ResponseListGraphType<>));
services.AddTransient<IAddressRepository, AddressRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
services.AddGraphQL(o =>
{
o.ExposeExceptions = true;
}
).AddGraphTypes(ServiceLifetime.Scoped);
services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
services.AddScoped<SampleSchema>();
}
谁能帮我解决这个问题?
解决方案
考虑到您没有适当的中间件来动态限制流量,并且考虑到堆栈跟踪相当多地引用 Cosmos,我敢打赌是 Cosmos 导致了 429 错误代码。
Cosmos DB 实际上有一个设置,您可以在其中指定吞吐量,默认为 1000 RU/秒(每秒请求单位)。您可能超出了此默认值,Azure 通过返回 429 错误代码告诉您“停止”。
有关此设置以及如何配置它的详细信息,请参阅此文档 https://docs.microsoft.com/en-us/azure/cosmos-db/request-units
推荐阅读
- javascript - 使用 setstate 更改字典,但不起作用。反应
- c# - 如何在 C# 中动态获取 LIST<> 中的多个字段的用户输入,而不是像我在下面的代码中那样进行硬编码?
- javascript - 显示了错误的 Chrome favicon,但图标的 url 是正确的
- oracle - 编写一个函数/过程,将大约 150k 行合并为一个,并将其作为输出发送,以便能够从 UI 下载
- python - 在熊猫中使用 pgeocode
- r - 在 R 中为逻辑回归模型创建合成数据集
- python - 云计算:在 5 台计算机上运行 Python 程序并找到非空列表中的最小值
- javascript - 如何从 API 源下载 csv 文件?
- javascript - 置换算法的BigO
- php - Wordpress - 如果内容条件隐藏 the_content() 调用的部分标记父级