1
Answer

Getting this Error on BOT EMULATION

Rp Singh

Rp Singh

7y
394
1
 Hi,
 
I am tring to simulate a bot with Microsoft Bot Emulator. I am getting this error. 
 
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:3978
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Bot.Connector.Emulator.ConversationModel.d__50.MoveNext()
 
 
The codebase is -
  1. [BotAuthentication]  
  2.    public class MessagesController : ApiController  
  3.    {  
  4.        ///   
  5.        /// POST: api/Messages  
  6.        /// Receive a message from a user and reply to it  
  7.        ///   
  8.        public async Task Post([FromBody]Activity activity)  
  9.        {  
  10.   
  11.            if (activity.Type == "Message")  
  12.            {  
  13.                //ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));  
  14.                // calculate something for us to return  
  15.                //int length = (activity.Text ?? string.Empty).Length;  
  16.   
  17.                // return our reply to the user  
  18.                //Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");  
  19.                //await connector.Conversations.ReplyToActivityAsync(reply);  
  20.   
  21.                string StockRateString;  
  22.                StockLUIS StLUIS = await GetEntityFromLUIS(activity.Text);  
  23.                if (StLUIS.intents.Count() > 0)  
  24.                {  
  25.                    switch (StLUIS.intents[0].intent)  
  26.                    {  
  27.                        case "StockPrice":  
  28.                            StockRateString = await GetStock(StLUIS.entities[0].entity);  
  29.                            break;  
  30.                        case "StockPrice2":  
  31.                            StockRateString = await GetStock(StLUIS.entities[0].entity);  
  32.                            break;  
  33.                        default:  
  34.                            StockRateString = "Sorry, I am not getting you...";  
  35.                            break;  
  36.                    }  
  37.   
  38.                }  
  39.                else  
  40.                {  
  41.                    StockRateString = "Sorry, I am not getting you...";  
  42.                }  
  43.   
  44.                //return activity.CreateReply(StockRateString);  
  45.            }  
  46.            else  
  47.            {  
  48.                HandleSystemMessage(activity);  
  49.            }  
  50.            var response = Request.CreateResponse(HttpStatusCode.OK);  
  51.            return response;  
  52.        }  
  53.   
  54.   
  55.   
  56.        private Activity HandleSystemMessage(Activity message)  
  57.        {  
  58.            if (message.Type == ActivityTypes.DeleteUserData)  
  59.            {  
  60.                // Implement user deletion here  
  61.                // If we handle user deletion, return a real message  
  62.            }  
  63.            else if (message.Type == ActivityTypes.ConversationUpdate)  
  64.            {  
  65.                // Handle conversation state changes, like members being added and removed  
  66.                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info  
  67.                // Not available in all channels  
  68.            }  
  69.            else if (message.Type == ActivityTypes.ContactRelationUpdate)  
  70.            {  
  71.                // Handle add/remove from contact lists  
  72.                // Activity.From + Activity.Action represent what happened  
  73.            }  
  74.            else if (message.Type == ActivityTypes.Typing)  
  75.            {  
  76.                // Handle knowing tha the user is typing  
  77.            }  
  78.            else if (message.Type == ActivityTypes.Ping)  
  79.            {  
  80.            }  
  81.   
  82.            return null;  
  83.        }  
  84.   
  85.        private async Task<string> GetStock(string StockSymbol)  
  86.        {  
  87.            double? dblStockValue = await StockBot.GetStockRateAsync(StockSymbol);  
  88.            if(dblStockValue == null)  
  89.            {  
  90.                return string.Format("This \"{0}\" is not a valid Stock Symbol", StockSymbol);  
  91.            }  
  92.            else  
  93.            {  
  94.                return string.Format("Stock price of {0} is {1}", StockSymbol, dblStockValue);  
  95.            }  
  96.        }  
  97.   
  98.        private static async Task GetEntityFromLUIS(string Query)  
  99.        {  
  100.            Query = Uri.EscapeDataString(Query);  
  101.            StockLUIS Data = new StockLUIS();  
  102.            using (HttpClient client = new HttpClient())  
  103.            {  
  104.                client.DefaultRequestHeaders.ExpectContinue = false;  
  105.                string RequestURI = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/27e3147e-21c9-4a11-ae26-3ea3a1dbf421?subscription-key=bb0397455aa54d9ca92c3f3f097420b9&q=" + Query;  
  106.                HttpResponseMessage msg = await client.GetAsync(RequestURI);  
  107.                if(msg.IsSuccessStatusCode)  
  108.                {  
  109.                    var JsonDataResponse = await msg.Content.ReadAsStringAsync();  
  110.                    Data = JsonConvert.DeserializeObject(JsonDataResponse);  
  111.                }  
  112.            }  
  113.            return Data;  
  114.   
  115.        }  
  116.    }  
 
Answers (1)