簡體   English   中英

System.Net.WebException:遠程服務器返回錯誤:(429)請求太多

[英]System.Net.WebException: The remote server returned an error: (429) Too Many Requests

我正在使用 ASP.NET Core 2.2、GraphQL.NET、CosmosDB、EntityFrameworkCore (Microsoft.EntityFrameworkCore.Cosmos(2.2.4) 開發 API 開發項目。

在運行解決方案時,我在 VS2019 的 output window 中看到錯誤:

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

我想了解有關上下文文件的更多信息,因為錯誤顯示請求過多。 因此,考慮到您已經在 cosmosdb 中部署了數據庫,建議刪除 Database.EnsureCreated() ,因為它會嘗試檢查數據庫並在每個不推薦的請求上創建模型,並且會產生性能問題。

有關詳細信息,請參閱此文檔https://docs.microsoft.com/en-us/ef/core/providers/cosmos/?tabs=dotnet-core-cli

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM