c# - Make an Http request through a Lambda Func -
i'm trying make httpclient requests through helper function manage circuitbreaker polly policy.
i'm trying call var response = clientfactory.makerequest(() => client.getasync("/"));
inside of client factory have circuit breaker policy defined , i'm trying execute lambda above using policy so
public async task<httpresponsemessage> makerequest(func<httpresponsemessage> request) { var response = policy.executeasync(() => request.invoke()); return response; }
i'm new lambda's whole , passing function gets more confusing. how configure function , first line of code execute client , return httpresponsemessage? don't think task<httpresponsemessage>
or func<httpresponsemessage>
correct
i suggest, read information on async/await (not lambdas) key understanding how achieve it.
https://docs.microsoft.com/en-us/dotnet/csharp/async
you using asynchronous programming must decide, whether want have asynchronous method makerequest or want synchronous one. if want synchronous (but - no need using executeasync. suppose, there execute() alternative) simple write :
public httpresponsemessage makerequest(func<httpresponsemessage> request) { var response = policy.executeasync(() => request.invoke()); return await response; }
if want asynchronous :
public async task<httpresponsemessage> makerequest(func<httpresponsemessage> request) { var response = policy.executeasync(() => request.invoke()); return response; } public void mymethodusingasync() { var responsepromises = makerequest(() => {...}); ///do job wich done before response retrieved (not waiting it); , if need - use await var responsereceived = await responsepromises; }
Comments
Post a Comment