Is there a way in C # to make an async method call without making the caller also async?

To be more clear, I refer to a factory method that was not used by async. In a recent API update, they made the old method obsolete and created a new async method. Now our code base is from VS2010 when async does not exist yet. I would like to change the method call to a new version so that we do not give a warning about the failure.

Now the problem: any call to the async method seems to require a “wait” to ensure that the method is indeed complete, and that any state data initialized in this method is available for subsequent operations. However, “waiting” in a call requires the calling method to be asynchronous as well, which then requires that the calling object of this method also use waiting, etc. Up to the top of the call stack. The only way to avoid this is to place the async factory method call in the async void method. However, Microsoft specifically stated that this feature exists only for event handlers and should be avoided otherwise.

Now, however, I can configure each individual function in the call stack. But this is not only annoying, but also an extreme violation of encapsulation. Why should a top-level class change its method headers just because some calls to the 27 layer function down had to deal with an external API change? I suspect that I am missing something very obvious, but from what it looks like, I have two options: either change all the method headers or obey the "bad programming practice". Is there another way? Thanks in advance for any input.

+4
source share
1 answer

Is there another way?

. ( , ), ( , ). .

, async. . .

, , - ( : ).

+1

Source: https://habr.com/ru/post/1648440/


All Articles