1

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.

2

Re: Atstsky hell and asynchronous asynchrony

Hello, Artem Korneev, you wrote: AK> 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: AK> AK> await this.retryPolicy. ExecuteAndCaptureAsync (AK> async () => AK> await Task. Run (AK> async () => response = await callback. Invoke (http))); AK> And who forces to use you these all async / await? await this.retryPolicy. ExecuteAndCaptureAsync (() => Task. Run (() => response = callback. Invoke (http)));

3

Re: Atstsky hell and asynchronous asynchrony

Hello, Artem Korneev, you wrote: AK> It would be desirable to rewrite all it to more clear type. To use await in each method on a call chain it is absolutely optional. public async Task InvokeHttpCall (Func <HttpClient, Task <HttpResponseMessage>> callback) {using (var httpClient = new HttpClient ()) {HttpResponseMessage response = null; var http = httpClient; await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http).ContinueWith (t => response = t. Result));}} And if a companion author Polly would read , the code would look as response = (await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http))).Result; I on your place would get issue on this business.

4

Re: Atstsky hell and asynchronous asynchrony

Hello, Sinix, you wrote: S> Hello, Artem Korneev, you wrote: AK>> It would be desirable to rewrite all it to more clear type. S> to use await in each method on a call chain it is absolutely optional. S> S> public async Task InvokeHttpCall (Func <HttpClient, Task <HttpResponseMessage>> callback) S> {S> using (var httpClient = new HttpClient ()) S> {S> HttpResponseMessage response = null; S> var http = httpClient; S> await this.retryPolicy. ExecuteAndCaptureAsync (() => S> callback. Invoke (http).ContinueWith (t => response = t. Result)); S>} S>} S> S> And if a companion author Polly would read  the code would look as S> S> response = await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http)).Result; S> S> I on your place would get issue on this business. callback. Invoke (http) <- what for Invoke??? response = await this.retryPolicy. ExecuteAndCaptureAsync (() => callback (http)).Result;

5

Re: Atstsky hell and asynchronous asynchrony

Hello, alexander_r, you wrote: _> callback. Invoke (http) <- what for Invoke??? In order that

6

Re: Atstsky hell and asynchronous asynchrony

Hello, Artem Korneev, you wrote: AK> AK> public async Task InvokeHttpCall (Func <HttpClient, Task <HttpResponseMessage>> callback) AK> {AK> using (var httpClient = new HttpClient ()) AK> {AK> HttpResponseMessage response = null; AK> var http = httpClient; AK> await this.retryPolicy. ExecuteAndCaptureAsync (AK> async () => AK> await Task. Run (AK> async () => response = await callback. Invoke (http))); AK>} AK>} AK> In your code there is one jamb not concerning to async. httpclient should be static private static HttpClient httpClient = new HttpClient (); YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE

7

Re: Atstsky hell and asynchronous asynchrony

Hello, ifle, you wrote: I> In your code there is one jamb not concerning to async. httpclient should be static Static to it certainly be harmfully, and here   within the limits of the client - can be and it is necessary, if it would be desirable to receive a pool of connections within the limits of group of connections (if roughly, on everyone  HttpClient - the group of connections). PS: And it is even better to throw out to hell all this  the stack. But it is already faster from area of a bad advice within the limits of the given forum.

8

Re: Atstsky hell and asynchronous asynchrony

Hello, ifle, you wrote: I> In your code there is one jamb not concerning to async. httpclient should be static does not approach on usage conditions. This RestClient is used in multi-tenant cloudy service, i.e. there two serial requests can come from different users of the different organizations. In it HttpClient there is field DefaultRequestHeaders where are exposed  authorizations. I.e. making static, we break at once all authentification. All is used it rather rarely (at least, while) so problems because of re-creation of object HttpClient are not present. But for thanks information.

9

Re: Atstsky hell and asynchronous asynchrony

Hello, Artem Korneev, you wrote: AK> does not approach on usage conditions. Here it is more important - it is necessary  connections or not? And that at enough small loading it is possible tcp to eat all ports (assuming that a distant host one, and is  ). AK> In it HttpClient there is field DefaultRequestHeaders where are exposed  authorizations. I.e. making static, we break at once all authentification. It is possible to transfer easy all necessary  actually at the moment of request (by preparation HttpRequestMessage). AK> all Is used it rather rarely (at least, while) so problems because of re-creation of object HttpClient are not present. And, well then probably apprx.

10

Re: Atstsky hell and asynchronous asynchrony

Hello, Artem Korneev, you wrote: On RestSharp for needs looked?

11

Re: Atstsky hell and asynchronous asynchrony

Hello, TK, you wrote: TK> Hello, Artem Korneev, you wrote: AK>> 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: AK>> AK>> await this.retryPolicy. ExecuteAndCaptureAsync (AK>> async () => AK>> await Task. Run (AK>> async () => response = await callback. Invoke (http))); AK>> TK> And who forces to use you these all async / await? TK> TK> await this.retryPolicy. ExecuteAndCaptureAsync (() => Task. Run (() => response = callback. Invoke (http))); TK> Esi eventually all the same at all of us is reduced to the synchronous code on which   generally, it will be synchronously possible directly in ExecuteAndCaptureAsync the delegate to transfer which to be fulfilled

12

Re: Atstsky hell and asynchronous asynchrony

Hello, Tom, you wrote: Tom> Esi eventually all the same at all of us is reduced to the synchronous code on which   generally, it will be synchronously possible directly in ExecuteAndCaptureAsync the delegate to transfer which to be fulfilled callback launches the new task, it it is necessary to wait and push result in response. And Task. Run here generally the superfluous.

13

Re: Atstsky hell and asynchronous asynchrony

S> And Task. Run here generally the superfluous. About it and speech

14

Re: Atstsky hell and asynchronous asynchrony

Hello, Sharov, you wrote: S> On RestSharp for needs looked? No, did not look. The REST-client was easier for implementing, than to search for the ready. Now looked on RestSharp, syntax is not pleasant to me. var request = new RestRequest ("resource / {id}", Method. POST);... IRestResponse response = client. Execute (request); var content = response. Content; at me all is enveloped in  and HTTP-methods have appropriate wrappers in class RestClient, i.e. I cause it somehow so: var result = restClient. Get <MyDataType> ("http://localhost") Also I receive in the answer already deserialised object.

15

Re: Atstsky hell and asynchronous asynchrony

Hello, Sinix, you wrote: S> And if a companion author Polly would read  the code would look as S> S> response = await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http)).Result; S> That for  the such? The reference can be asked?

16

Re: Atstsky hell and asynchronous asynchrony

Hello, SergASh, you wrote: SAS> That for  the such? The reference can be asked? Was specific on  anything fresh Task-based Asynchronous Pattern (TAP) did not quit, but here a reality and it is not required. Enough the general reasons from FDG book: Framework Design Principle Frameworks must be designed starting from a set of usage scenarios and code samples implementing these scenarios. If it is not clear, about what speech -  on a detailed explanation. On a specific example://1. We have an action which something returns var result = await GetResultAsync (someParams);//2. We want to launch this action in any context using (var contex = GetCaptureContext (someParams)) {var result = await GetResultAsync (someParams);} //3. We want to control calls in a context, for example, to launch the code some times.//we Turn action in the delegate, we receive var result = await retryPolicy. ExecuteAndCaptureAsync (() => GetResultAsync (someParams)));//4. We want to return except the result something else, for example, an amount of iterations var executeResult = await retryPolicy. ExecuteAndCaptureAsync (() => GetResultAsync (someParams))); var result = executeResult. Result; Sense such: each short step allows to solve __ a problem and in itself does not create the new. About with small steps it to trace very easily and it is not required to spring back to specify, whether forgot that. An elephant eat on slices. If on this principle to hammer and threaten on an elephant entirely the today's example turns out: the code is able everything, except the initial scenario That the most insulting, here during this moment people __ do not try to stop and look for the moment to which all went not so. Instead a problem mask new crutches and so until the volume of crutches does not outgrow volume of actually useful code on the order.

17

Re: Atstsky hell and asynchronous asynchrony

Hello, TK, you wrote: TK> And who forces to use you these all async / await? Without any of these async / await it stupidly did not work. I from that operation left already, I can not feel that code now. One of these days I will try to understand with.net core on the house Linux-desktop, I will try once more. Generally, at me in a head the thought that superfluous there not async / await, more truly, not only async / await, but also asynchrony of those methods as a whole is twisted. To me it is thought what enough there to cause ExecuteAndCaptureAsync, it already should provide asynchrony. But it is necessary to try. As I will try - I will write.

18

Re: Atstsky hell and asynchronous asynchrony

Hello, Sinix, you wrote: S> And if a companion author Polly would read  the code would look as S> S> response = await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http)).Result; S> await together with Result?

19

Re: Atstsky hell and asynchronous asynchrony

Hello, Doc, you wrote: Doc> await together with Result? Brackets were lost response = (await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http))).Result;//or var executeResult = await this.retryPolicy. ExecuteAndCaptureAsync (() => callback. Invoke (http)); response = executeResult. Result;