coderushforroslyn-405452-static-code-analysis-analyzers-library-crr0053-missing-await.md
This analyzer identifies places where an async method should be awaited to avoid issues with exception handling and premature object disposal:
public Task<T> Handle(T request, CancellationToken cancellationToken) {
using var handler = HandlerFactory.CreateRequestHandler(request);
return ExecuteHandlerAsync<T>(handler, cancellationToken); // <-- CRR0053 reported for the ExecuteHandlerAsync(...) call
}
public Task<T> ExecuteHandlerAsync<T>(Handler<T> handler, CancellationToken cancellationToken) {
// A long running operation that utilizes handler object
}
Public Function Handle(ByVal request As T, ByVal cancellationToken As CancellationToken) As Task(Of T)
Using handler = HandlerFactory.CreateRequestHandler(request)
' CRR0053 reported for the ExecuteHandlerAsync(...) call:
Return ExecuteHandlerAsync(Of T)(handler, cancellationToken)
End Using
End Function
Public Function ExecuteHandlerAsync(Of T)(ByVal handler As Handler(Of T), ByVal cancellationToken As CancellationToken) As Task(Of T)
' A long running operation that utilizes handler object
End Function
To fix this warning, add the await operator to the method call:
public async Task<T> Handle(T request, CancellationToken cancellationToken) {
using var handler = HandlerFactory.CreateRequestHandler(request);
return await ExecuteHandlerAsync<T>(handler, cancellationToken).ConfigureAwait(false); // <-- CRR0053 no longer reported for the ExecuteHandlerAsync(...) call
}
public Task<T> ExecuteHandlerAsync<T>(Handler<T> handler, CancellationToken cancellationToken) {
// A long running operation that utilizes handler object
}
Public Async Function Handle(ByVal request As T, ByVal cancellationToken As CancellationToken) As Task(Of T)
Using handler = HandlerFactory.CreateRequestHandler(request)
' CRR0053 no longer reported for the ExecuteHandlerAsync(...) call:
Return Await ExecuteHandlerAsync(Of T)(handler, cancellationToken).ConfigureAwait(False)
End Using
End Function
Public Function ExecuteHandlerAsync(Of T)(ByVal handler As Handler(Of T), ByVal cancellationToken As CancellationToken) As Task(Of T)
' A long running operation that utilizes handler object
End Function
See Also