0

I am unable to make On Premise API calls from MS Bot (v4) C# once deployed on Azure. The On Premise API call works fine when locally tested with emulator.

As recommended by Microsoft support team, I have already tried using Direct Line channel for web chat but does not help.

                AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

                HttpWebRequest webRequest = HttpWebRequest.CreateHttp(updatedURL);
                webRequest.Method = currentStep.StepsServiceCall.Method;
                webRequest.ContentType = "application/json";
                webRequest.KeepAlive = true;

                if (!String.IsNullOrEmpty(currentStep.StepsServiceCall.Headers))
                {
                    string updatedJson = currentStep.InjectPropertyValuesJson(currentStep.StepsServiceCall.Headers, properties);

                    webRequest.ContentLength = updatedJson.Length;

                    using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
                    {
                        streamWriter.Write(updatedJson);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                }

                using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string content = reader.ReadToEnd();

                        return await currentStep.ProcessResponseText(context, cancellationToken, content);
                    }

Getting below error from Azure App Service logs.

Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter: Exception caught : An error occurred while sending the request. Error 12029 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A connection with the server could not be established'.

Sandeep
  • 3
  • 1
  • Have you tried to open your On Permise API from outside your current network? Using 3G from your phone for example? If you are in a corporate network, unless someone change the network configurations to allow inbound connections, Azure won't be able to reach your On Permise API. – Paulo Correia May 29 '19 at 10:37
  • Thanks for your response Paulo. On Premise API is not accessible outside organization network. What configurations needs to be done for Azure to reach On Premise API? – Sandeep Jun 04 '19 at 10:40
  • Hi @Sandeep, You need to ask your IT Department on seeing how to expose your API to Azure. For sure you have the server where the API is hosted, they will need to make some configurations at the firewall and routing level, to allow an outside request to reach your internal server. At extremes, and for testing you can always try to use https://ngrok.com for development/testing. I wouldn't recomend to use it on LIVE. – Paulo Correia Jun 04 '19 at 17:36

1 Answers1

1

To me the error message looks reasonably straight forward, your Azure App Service cannot reach the URL that your on-premise API is hosted on. Assuming that the server the API is hosted on responds to ICMP packets (pings) I would try the following:

  1. Ping the URL from your local development machine - this can be done through command prompt/terminal ping <url-here>.
  2. In Azure access the KUDU interface - go to App Service > Development Tools > Advanced tools > Go
  3. In the one of the dropdown menus (tools I believe) there should be a command prompt option.
  4. Attempt to ping the URL in the KUDU console.
  5. If the ping fails then you will need to setup a route or update the URL you are using to use the FQDN as per this answer.
  6. If the FQDN doesn't work then Azure Hybrid Connections are also a potential solution to your problem - a video is available here.
Matt Stannett
  • 2,700
  • 1
  • 15
  • 36
  • Thanks Matt for the responses. This helped me to figure out that the url is not accessible from Azure. However in that case your approach suggests to use Azure Hybrid, which would be paid service. Is there any alternate solution, which we can handle from code? – Sandeep Jun 04 '19 at 10:38
  • No problem @Sandeep. The crux of the problem is that your API endpoint is not publicly accessible, so adding code won't fix this. You basically need a public DNS entry for your API - your hosting service will be able to provide this. Before you expose your API to the internet you will want to [Secure it using tokens](https://www.blinkingcaret.com/2017/09/06/secure-web-api-in-asp-net-core/). Or you could add an IP address whitelist to your API get your [web app's ips](https://learn.microsoft.com/en-us/azure/app-service/overview-inbound-outbound-ips#find-outbound-ips) (not recommended). – Matt Stannett Jun 04 '19 at 10:50