Call another Web API from a Web API without waiting for the result


Gabriel G.Roy

Is there a way to trigger a call to an external web API Httpin my own web API without having to wait for the result?

I'm in a situation where I don't really care if the call is successful and don't need the result of that query.

I'm currently doing the following in one of my Web API methods:

var client = new HttpClient() { BaseAddress = someOtherApiAddress };
client.PostAsync("DoSomething", null);

I can't put this code in a usingstatement because the call won't pass in this case. I also don't want to call .Result()the task because I don't want to wait for the query to finish.

I'm trying to understand what it means to do something like this. I've read a lot that it 's really dangerous , but I'm not sure why. For example, what happens when my initial query ends. Will IISdispose of thread and client objects, will this cause problems on the other side of the query?

Dudemanword

Is there a way to make Http calls to an external web API in my own web API without waiting for the result?

Yes. This is called a misfire. However, it seems that you have already discovered it.

I'm trying to understand the implications of doing something like this

Answers linked to these three risks in a link in your state:

  1. Unhandled exceptions in threads not associated with the request will cause the process to be interrupted. This happens even if you set a handler via the Application_Error method.

This means that any exception thrown in your application or the receiving application will not be caught (there are ways to overcome this)

  1. If you run your site in a web farm, you may end up with multiple instances of your application, all trying to run the same task at the same time. It's more challenging to handle than the first project, but still not too hard. A typical approach is to use a resource common to all servers (such as a database) as a synchronization mechanism to coordinate tasks.

You may have multiple dismissals and forget to call when you intend to make only one call.

  1. The AppDomain in which your website is running can be closed for a number of reasons, killing your background tasks along with it. If it happens in the middle of code execution, it can corrupt data.

This is dangerous. If your AppDomain fails, it can corrupt the data being sent to another API, causing unexpected behavior on the other end.

Related


Call Web API from MVC POST action method and receive result

Paul B I'm trying to call a Web API from an MVC POST action method and receive the result, but not sure how, for example: [HttpPost] public ActionResult Submit(Model m) { // Get the posted form values and add to list using model binding

Pass timezone to web api call from nodatime

Space Cowboy 74 I'm going crazy with datetime issues and networking. I host a web server in the central time zone. When a customer in the Eastern time zone tries to schedule an item for a given date using my app, they pass in a value of (for example) March 14,

Call a Web API from another Web API

PNR Is it possible to make a web api call another web api? I am using the code below to access the web api from my web api but it never returns from the call. If I use the code from the console app it works fine. public void DoStuff(){ RunAsync().Wait(); }

Call Web API from MVC Controller

sarabiani I have a WebAPI controller in my MVC5 project solution. WebAPI has a method to return all files in a specific folder as a Json list: [{"name":"file1.zip", "path":"c:\\"}, {...}] I want to call this method from my HomeController, convert the Json resp

Attempt to read response from Web API call

Sam I'm trying to read my employee list from a Web API call and I'm getting an error. using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://myApiUrl"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Ac

Call Web API from MVC POST action method and receive result

Paul B I'm trying to call a Web API from an MVC POST action method and receive the result, but not sure how, for example: [HttpPost] public ActionResult Submit(Model m) { // Get the posted form values and add to list using model binding

Call MVC Web API from Android application

Martin Hoban I have an MVC Web API application running in VS 2012. It connects to a SQL Server database with some data. I want to output this data to a textview on my android app, but I can't find any good tutorials that can do this. It would be greatly apprec

Call Web API from MVC Controller

sarabiani I have a WebAPI controller in my MVC5 project solution. WebAPI has a method to return all files in a specific folder as a Json list: [{"name":"file1.zip", "path":"c:\\"}, {...}] I want to call this method from my HomeController, convert the Json resp

Call a Web API from another Web API

PNR Is it possible to make a web api call another web api? I am using the code below to access the web api from my web api but it never returns from the call. If I use the code from the console app it works fine. public void DoStuff(){ RunAsync().Wait(); }

Call Web API from MVC POST action method and receive result

Paul B I'm trying to call a Web API from an MVC POST action method and receive the result, but not sure how, for example: [HttpPost] public ActionResult Submit(Model m) { // Get the posted form values and add to list using model binding

Call a Web API from another Web API

PNR Is it possible to make a web api call another web api? I am using the code below to access the web api from my web api but it never returns from the call. If I use the code from the console app it works fine. public void DoStuff(){ RunAsync().Wait(); }

Call Web API from MVC POST action method and receive result

Paul B I'm trying to call a Web API from an MVC POST action method and receive the result, but not sure how, for example: [HttpPost] public ActionResult Submit(Model m) { // Get the posted form values and add to list using model binding

Call a Web API from another Web API

PNR Is it possible to make a web api call another web api? I am using the code below to access the web api from my web api but it never returns from the call. If I use the code from the console app it works fine. public void DoStuff(){ RunAsync().Wait(); }

Android Web API call without asynctask

Jay Pandya I need to make an api call when a button is clicked. So I do it the following way. btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View

Call a Web API from another Web API

Vans Fannel I have a Windows service with a Web API interface. I use this service to upload data from one system to another. My problem is that the other system (where I have to upload the data) is a web api service and I'm not sure if it's a good idea to make

Call a Web API from another Web API

Somad I am new to JSON and Web API. I have created a service called AdminService which has a get method. See the get method below: [Route("{iUserId:long}/{iSegmentId:long}/GetUserSegmentItemBySegmentIdAndUserId")] public IEnumerable<spADGetUserSegmentI

Call a Web API from another Web API

PNR Is it possible to make a web api call another web api? I am using the code below to access the web api from my web api but it never returns from the call. If I use the code from the console app it works fine. public void DoStuff(){ RunAsync().Wait(); }

Call Web API from MVC POST action method and receive result

Paul B I'm trying to call a Web API from an MVC POST action method and receive the result, but not sure how, for example: [HttpPost] public ActionResult Submit(Model m) { // Get the posted form values and add to list using model binding

Android Web API call without asynctask

Jay Pandya I need to make an api call when a button is clicked. So I do it the following way. btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View