Should I use synchronous/asynchronous API calls when creating an API that calls 2 other APIs?Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onWhen should I use a List vs a LinkedListdecimal vs double! - Which one should I use and when?When should I create a destructor?How to call asynchronous method from synchronous method in C#?How to implement Request / Response pattern with Web Api?Calling async method synchronouslyShould this MVC4 API call be async?C# unsupported grant type when calling web apiCan I Get Access Token on Azure without registering my autotesting app?
Cremated People Pottery
Does PC weight have a mechanical effect?
What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?
TiKZ won't graph 1/sqrt(x)
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
Using roof rails to set up hammock
Reflecting Telescope Blind Spot?
How can the US president give an order to a civilian?
Why is gun control associated with the socially liberal Democratic party?
Converting 3x7 to a 1x7. Is it possible with only existing parts?
When is the phrase "j'ai bon" used?
Monotonic operations and integrals
Why is Skinner so awkward in Hot Fuzz?
My parents claim they cannot pay for my college education; what are my options?
How do credit card companies know what type of business I'm paying for?
I sent an angry e-mail to my interviewers about a conflict at my home institution. Could this affect my application?
Someone who is granted access to information but not expected to read it
Sci fi/fantasy book, people stranded on a planet where tech doesn't work, magic mist
VHDL: What is correct way to model open collector output for FPGA?
Why did the USA sell so many airplanes prior to WW2?
Can I appeal credit ding if ex-wife is responsible for paying mortgage?
Can artificial satellite positions affect tides?
At what temperature should the earth be cooked to prevent human infection?
How can this shape perfectly cover a cube?
Should I use synchronous/asynchronous API calls when creating an API that calls 2 other APIs?
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onWhen should I use a List vs a LinkedListdecimal vs double! - Which one should I use and when?When should I create a destructor?How to call asynchronous method from synchronous method in C#?How to implement Request / Response pattern with Web Api?Calling async method synchronouslyShould this MVC4 API call be async?C# unsupported grant type when calling web apiCan I Get Access Token on Azure without registering my autotesting app?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I’m creating an API that serves as the bridge between the app and 2 other APIs. I want to know if what is the best way to do this. I’m using HttpClient. The app has almost a thousand users so if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests? Is there a better way of doing an API like this?
Here is a sample of my code using synchronous:
[HttpGet]
[Route("api/apiname")]
public String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
var request = JsonConvert.SerializeObject(getNPRequest);
string errorMessage = "";
try
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
var response = httpClient.GetAsync("api/MobileApp/GetNumberP?"
+ "strCardNumber=" + getNPRequest.strCardNumber
+ "&strDateOfBirth=" + getNPRequest.strDateOfBirth).Result;
return response;
catch (Exception e)
throw utils.ReturnException("GetNumberofP", e, errorMessage);
c# asp.net-web-api httpclient
add a comment |
I’m creating an API that serves as the bridge between the app and 2 other APIs. I want to know if what is the best way to do this. I’m using HttpClient. The app has almost a thousand users so if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests? Is there a better way of doing an API like this?
Here is a sample of my code using synchronous:
[HttpGet]
[Route("api/apiname")]
public String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
var request = JsonConvert.SerializeObject(getNPRequest);
string errorMessage = "";
try
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
var response = httpClient.GetAsync("api/MobileApp/GetNumberP?"
+ "strCardNumber=" + getNPRequest.strCardNumber
+ "&strDateOfBirth=" + getNPRequest.strDateOfBirth).Result;
return response;
catch (Exception e)
throw utils.ReturnException("GetNumberofP", e, errorMessage);
c# asp.net-web-api httpclient
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
Alsocatch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.
– Enigmativity
Mar 28 at 1:57
add a comment |
I’m creating an API that serves as the bridge between the app and 2 other APIs. I want to know if what is the best way to do this. I’m using HttpClient. The app has almost a thousand users so if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests? Is there a better way of doing an API like this?
Here is a sample of my code using synchronous:
[HttpGet]
[Route("api/apiname")]
public String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
var request = JsonConvert.SerializeObject(getNPRequest);
string errorMessage = "";
try
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
var response = httpClient.GetAsync("api/MobileApp/GetNumberP?"
+ "strCardNumber=" + getNPRequest.strCardNumber
+ "&strDateOfBirth=" + getNPRequest.strDateOfBirth).Result;
return response;
catch (Exception e)
throw utils.ReturnException("GetNumberofP", e, errorMessage);
c# asp.net-web-api httpclient
I’m creating an API that serves as the bridge between the app and 2 other APIs. I want to know if what is the best way to do this. I’m using HttpClient. The app has almost a thousand users so if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests? Is there a better way of doing an API like this?
Here is a sample of my code using synchronous:
[HttpGet]
[Route("api/apiname")]
public String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
var request = JsonConvert.SerializeObject(getNPRequest);
string errorMessage = "";
try
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
var response = httpClient.GetAsync("api/MobileApp/GetNumberP?"
+ "strCardNumber=" + getNPRequest.strCardNumber
+ "&strDateOfBirth=" + getNPRequest.strDateOfBirth).Result;
return response;
catch (Exception e)
throw utils.ReturnException("GetNumberofP", e, errorMessage);
c# asp.net-web-api httpclient
c# asp.net-web-api httpclient
edited Mar 28 at 1:50
Kate Lastimosa
asked Mar 25 at 2:55
Kate LastimosaKate Lastimosa
364
364
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
Alsocatch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.
– Enigmativity
Mar 28 at 1:57
add a comment |
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
Alsocatch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.
– Enigmativity
Mar 28 at 1:57
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
Also
catch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.– Enigmativity
Mar 28 at 1:57
Also
catch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.– Enigmativity
Mar 28 at 1:57
add a comment |
3 Answers
3
active
oldest
votes
if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests
No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.
You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result
because that forces the async code to become blocking and effectively becomes synchronous.
add a comment |
t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber=getNPRequest.strCardNumber&strDateOfBirth=getNPRequest.strDateOfBirth");
You should also consider creating a new httpClient
for each call.
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps tryWebClient
instead.
– Enigmativity
Mar 28 at 6:13
add a comment |
It seems you're missing the async
and await
keywords.
public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
(...)
var response = await httpClient.GetAsync();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330699%2fshould-i-use-synchronous-asynchronous-api-calls-when-creating-an-api-that-calls%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests
No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.
You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result
because that forces the async code to become blocking and effectively becomes synchronous.
add a comment |
if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests
No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.
You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result
because that forces the async code to become blocking and effectively becomes synchronous.
add a comment |
if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests
No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.
You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result
because that forces the async code to become blocking and effectively becomes synchronous.
if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests
No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.
You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result
because that forces the async code to become blocking and effectively becomes synchronous.
answered Mar 25 at 3:05
ProgrammerAlProgrammerAl
194315
194315
add a comment |
add a comment |
t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber=getNPRequest.strCardNumber&strDateOfBirth=getNPRequest.strDateOfBirth");
You should also consider creating a new httpClient
for each call.
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps tryWebClient
instead.
– Enigmativity
Mar 28 at 6:13
add a comment |
t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber=getNPRequest.strCardNumber&strDateOfBirth=getNPRequest.strDateOfBirth");
You should also consider creating a new httpClient
for each call.
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps tryWebClient
instead.
– Enigmativity
Mar 28 at 6:13
add a comment |
t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber=getNPRequest.strCardNumber&strDateOfBirth=getNPRequest.strDateOfBirth");
You should also consider creating a new httpClient
for each call.
t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber=getNPRequest.strCardNumber&strDateOfBirth=getNPRequest.strDateOfBirth");
You should also consider creating a new httpClient
for each call.
answered Mar 28 at 2:01
EnigmativityEnigmativity
80.2k967135
80.2k967135
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps tryWebClient
instead.
– Enigmativity
Mar 28 at 6:13
add a comment |
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps tryWebClient
instead.
– Enigmativity
Mar 28 at 6:13
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
do you mean wrapping my httpclient in a using statement like this: using(var client = new HttpClient()) //do something with http client
– Kate Lastimosa
Mar 28 at 3:17
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
@KateLastimosa - Yes, precisely that.
– Enigmativity
Mar 28 at 4:07
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
Is there a specific behavior why I should use the using statement? That is how I originally did it. But some articles said not to do that. Here is the link. aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
– Kate Lastimosa
Mar 28 at 5:41
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps try
WebClient
instead.– Enigmativity
Mar 28 at 6:13
@KateLastimosa - I hadn't seen all that before. It sounds like it got all too complicated. Perhaps try
WebClient
instead.– Enigmativity
Mar 28 at 6:13
add a comment |
It seems you're missing the async
and await
keywords.
public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
(...)
var response = await httpClient.GetAsync();
add a comment |
It seems you're missing the async
and await
keywords.
public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
(...)
var response = await httpClient.GetAsync();
add a comment |
It seems you're missing the async
and await
keywords.
public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
(...)
var response = await httpClient.GetAsync();
It seems you're missing the async
and await
keywords.
public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
(...)
var response = await httpClient.GetAsync();
answered Mar 25 at 2:59
tymtamtymtam
6,98033760
6,98033760
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330699%2fshould-i-use-synchronous-asynchronous-api-calls-when-creating-an-api-that-calls%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.
– Enigmativity
Mar 28 at 1:56
Also
catch (Exception e)
is a bad anti-pattern. You should only ever catch specific exceptions that you can meaningfully handle.– Enigmativity
Mar 28 at 1:57