我正在使用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)
Startup.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
我想了解有关上下文文件的更多信息,因为错误表明请求太多。因此,考虑到您已经在cosmosdb中部署了数据库,建议删除Database.EnsureCreated(),因为它会尝试检查数据库并针对每个不建议的请求创建模型,这会导致性能问题。
有关更多信息,请参考此文档https://docs.microsoft.com/en-us/ef/core/providers/cosmos/?tabs=dotnet-core-cli