简体   繁体   中英

create a asynchronous function using async await and return a regular object

I'm trying to build an sdk wrapper that will make all the relevant http calls that need to be made but taking the complexity out of it for the developer using it so I'm trying to build a function that returns a regular object but within that function I want the call and async and wait on httpclient or webclient but I can't figure out how to do this?

All I can see is to have a void function which is no good, or having a Task or Task but that's no good either.

I would like the user to define the function as normal ie

 MyClss myobject = company.sdk.RequestData("http://....");

Is this not possible? I believe that if you use the .Result, it turns the call into an synchronous call which is no good.

Any suggestions or solution? Maybe I'm looking at this the wrong way?

I would like the user to define the function as normal ie MyClss myobject = company.sdk.RequestData("http://....");

The definition of "normal" is changing. In the New World, it is normal for asynchronous methods to be called asynchronously (ie via await ).

What you just said is that you want the user to be able to call your asynchronous method synchronously. That is abnormal.

As Simon commented, the correct solution is to provide an asynchronous API for asynchronous methods. Just follow the TAP ; all .NET developers will need to learn it sooner or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM