Topic: Atstsky hell and asynchronous asynchrony
I here in the code wrote one construction which frightens me of the complexity. It is a piece of the code of class RestServiceClient intended for sending of requests to HTTP-servers. This function creates HttpClient and causes , specified as parameter. public async Task InvokeHttpCall (Func <HttpClient, Task <HttpResponseMessage>> callback) {using (var httpClient = new HttpClient ()) {HttpResponseMessage response = null; var http = httpClient; await this.retryPolicy. ExecuteAndCaptureAsync (async () => await Task. Run (async () => response = await callback. Invoke (http)));} } It is made for this purpose not to repeat the same code for a wrapper of each type of HTTP-inquiry. Here I result the simplified code, is real there still setting of different titles of authentification, samplings of time of handling of request etc. Result of performance of request (the variable response) is used then for reset HttpStatusCode. There some similar methods, with deserialising of result and without it, I result here the elementary variant. This method so is used: public async Task DeleteAsync (Uri uri) {await this.httpCallsInvoker. InvokeHttpCall ((httpClient) => httpClient. DeleteAsync (uri));} Here there is a wrapper for DELETE-zaposa, remaining types of requests are implemented similarly, but with additional parameters. Here this line: await this.retryPolicy. ExecuteAndCaptureAsync Uses library Polly for application of different policies of error handling of connection (retry, circuit breaker, etc.) . It too asynchronous also accepts all as parameters. And so. It is all works, but the result seems to me unduly difficult. Especially I am am confused here with this ladder from async/await: await this.retryPolicy. ExecuteAndCaptureAsync (async () => await Task. Run (async () => response = await callback. Invoke (http))); It would be desirable to rewrite all it to more clear type therefore as now, except me, somebody disassembles this code in my command. Colleagues, prompt, as well as from what side it is better to approach to it? Whether there are here no explicit jambs which I do not note? It seems to me that something in this sequence async/await can be simplified, but I could not yet.