4

Azure에서 asp.net mvc Web App가 실행 중입니다. 한 가지 방법으로 Azure Function Web API에 여러 HTTP 호출을합니다. 이 Azure 함수 내에서 새 레코드를 데이터베이스에 삽입하기 위해 DbContext을 사용합니다.Azure 함수를 호출하면 HTTP 결과로 150 번 예외가 발생합니다.

// Method in web application making http requests to azure function web api 
public async Task CreateRecords() { 
    int amountOfCalls = 150; 
    var allTasks = new List<Task<HttpResponseMessage>>(); 

    for (int i = 0; i < amountOfCalls; i++) { 
     var task = HttpClientInstance.Instance.PostAsync(<uri>, new StringContent(i.ToString())); 
     allTasks.Add(task); 
    } 

    await Task.WhenAll(allTasks); // Gives an exception 
} 

amountOfCalls을 25, 50 또는 100으로 설정하면 모든 것이 정상입니다. 내가 설정 한 경우 그러나,이 amountOfCalls (150)에, 처음에 데이터베이스에 일부 레코드를 삽입하지만, 결국 오류가 발생합니다

Unable to connect to the remote server.

그래서 처음에는 그것이 최대 동시 로그인 뭔가 될 줄 알았는데 Azure SQL 데이터베이스. 그래서 나는 그 중 하나를 가격 책정 S9 (1600 DTU는 최대 3200 동시 로그인 here 언급)로 업그레이 드했습니다.

하지만 문제가 해결되지 않았습니다. 그래서 Azure 기능을 원격으로 디버깅하기 시작했습니다. Azure 함수 자체에서는 예외가 없습니다. 다음 단계는 Application Insights를 살펴 보는 것입니다. 푸른 기능은 다음과 같습니다

type: System.InvalidOperationException

method: WebApp.DataAccess.PocDbContext..ctor

requestName: InsertRecordFunction

requestDuration: 55,643.2201

: 그리고 나는 DbContext의 생성자 (불행히도 스택 트레이스없이) 예외를 준 것을보고

[DependencyInjectionConfig(typeof(DependencyConfig))] // Autofac 
public static class InsertRecordFunction 
{ 
    [FunctionName("InsertRecordFunction")] 
    public static async Task<HttpResponseMessage> Run(
     [HttpTrigger(AuthorizationLevel.Function, "post", Route = "InsertRecordFunction")]HttpRequestMessage req, 
     TraceWriter log, 
     [Inject]IRecordDataService recordDataService) 
    { 
     log.Info("InsertRecordFunction was triggered by Http."); 
     try { 
      await recordDataService.AddRandomRecordAsync(); 
     } 
     catch (Exception ex) { 
      return req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); 
     } 

     return req.CreateResponse(HttpStatusCode.OK); 
    } 

그리고 PocDbContext의 ctor에는 다음과 같습니다

public PocDbContext() : base("DefaultConnection") { 
    Database.SetInitializer<PocDbContext>(null); 
} 

저는 여전히 Azure SQL 데이터베이스에 연결 풀링과 관련이 있다고 생각합니다. 이 문제가 나오기 전에 나는 실행 전략을 SqlAzureExecutionStrategy (나는 그것을 this way)으로 설정하라는 또 다른 이야기를 들었다.

(정적) HttpClient을 사용하는 Azure 웹 응용 프로그램에서 웹 API 호출에 대해 어떤 종류의 최대 값이 있습니까? 정확한 예외가 무엇인지 알아 내기 위해 내가 할 수있는 다른 조치는 무엇입니까? 감사.

업데이트

는 응용 프로그램 통찰력에서 나는 예외에 대한 추가 정보를 발견

나는 푸른 SQL 데이터베이스의 가격 계층을 업그레이드 한 후 기대하지 어떤

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

. 엔티티 프레임 워크 (SQL Server Connection Pooling (ADO.NET))을 뒷받침 ADO.NET에 대한 문서,에서

+0

여기서 푸른 색 함수에서 처리 할 수있는 최대 동시 호출에 대해 설명하는 스레드입니다. https://stackoverflow.com/questions/44257660/azure-functions-limiting-parallel-execution이 스레드에서 언급 한 maxconcurrent 요청을 확인할 수 있습니다. https : // github.com/Azure/Azure-Functions/issues/419 – Baskar

+0

http 대신 큐를 사용할 수 있습니까? – Mikhail

+0

@ Baskar, 그 링크는 실제로 내 질문에 대답하지 않습니다. – Goat

답변

3

:

By default, connection pooling is enabled in ADO.NET. Unless you explicitly disable it, the pooler optimizes the connections as they are opened and closed in your application.

Adding Connections 섹션에서 아래로 조금 더 :

Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default).

그래서 명시 적으로 설정하는 연결 문자열의 최대 Pool size> = 150 이상으로 트릭을해야합니다.