Making an Http Call without Blocking IO RRS feed

  • Question

  • User-1204637165 posted

    So I have a peice of code am using this code to make a post request to some other Endpoint in my controller. So I would explain so you all understand.

    I have a platform were I have users. My users are merchants. My merchants supply a call back url or webhook when signing up on my platform, I am suppose to post information to the webhook of my merchant.

    I also have partners. My partners call my app through an Endpoint. when they call my app via a Post request. I am suppose to make a post request to my merchants web hook.

    Unfortunately the code that makes a post request to my merchant webhook blocks IO. I tried making it async but it still blocks IO. The block happens when there is a communication when the merchant webhook . This block is affectiing the communication between my endpoint and my partners.

    I want a scenario were If I make a post to my merchant endpoint. Either there is an authentication issue or a post error to my merchant webhook, this should have no effect on the flow of my application.

    This happens when there is a probably and authenitication issue with the Merchant Webhook. Here is my code below

     public async Task Posttowebservice(string Url,string AuthorizeType, string Token, string Jsonbody)
                //string responseResponse = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Timeout = 1000000;
                if(Token != null)
                    request.Headers.Add("Authorization Bearer ",Token);
                request.Accept = "application/json";
                request.ContentLength = Jsonbody.Length;
                using (Stream webStream =await request.GetRequestStreamAsync())
                using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
                   await requestWriter.WriteAsync(Jsonbody);
                        // await requestWriter.FlushAsync();
                    await request.GetResponseAsync();
                catch (Exception e)
                    logger.LogError("Problem from the Merchant Webhook \n"+e.Message);

    I am looking at posting in a way that the entire application flows without been blocked either there is an error in the POST request or not.

    Monday, July 13, 2020 4:29 PM

All replies

  • User-474980206 posted

    not sure what you mean by blocking IO. what's blocked? the sample code calls a webservice and return a response after the web service call is done. did you want to return before the web service completed? then run as a background request.  

    Monday, July 13, 2020 6:01 PM
  • User-1204637165 posted

    Thanks alot. but if I do it in a background.

    Hope I wont have the problems of delayed response when there is an error posting to the merchant web hooks.

    Is there a way to post successfully without reading response.

    I tried to remove the response reading part, But I notice it was not sending well.

    Am looking at how it is done in Enterprise Integration Systems.

    They send messages without been blocked in anyway.

    They can afford to send messages to 40 systems in few seconds without blocks in their thread.

    This system would be getting notifications in thousands at the point it live so it needs to be resilient and work seemlessly without blocks or slow issues.

    Best Regards.

    Wednesday, July 15, 2020 8:13 AM