"An error occurred while sending the request" from emulator
My previous thread was closed and I can't find a way to reopen it, so apologies in advance if I am breaking etiquette.
I still have the same problem as detailed.
I did the following:
- installed VS2015 Community Edition
- installed Microsoft Bot Emulator
- downloaded and installed the template for bots
- Created a new project based on the bot template
- Updated the Bot.Connector
- Copied and pasted the full example of the alarm bot to MessageController.cs
At that, trying to build fails. If I install the bot.builder v1.2.3, it builds, and I get the webpage:
Bot_Application1
Describe your bot here and your terms of use etc.
Visit Bot Framework to register your bot. When you register it, remember to set your bot's endpoint to
https://your_bots_hostname/api/messages
I cannot connect though. I connect the emulator to either https://localhost/api/messages or http://localhost/api/messages, but both give me the error above.
The JSON in the window after the emulator returns "an error occurred while sending the request" is
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:443
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.<SendMessageAsync>d__50.MoveNext()
I don't think it is the emulator that is at fault, as the emulator works with the code supplied in the template, but not when I use the LUIS example, alarm bot.
I have checked that nothing is on the port, and even created a new VM to reproduce the problem on (and I could).
I know it was previously mentioned. But can you please change the URL in emulator to http://localhost:3978/api/messages.
Port number need to be mentioned if the service is not hosted on 80 for http or 443 for https.
Currently the exception detail states no service on port 443.
No connection could be made because the target machine actively refused it 127.0.0.1:443
@quarkyuk Also can you paste the class file content here to check if there is any issue with the code.
Thanks, I have tried that URL, and I just a "404 Not Found" error. Do you mean the MessagesController.cs file?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
namespace Microsoft.Bot.Sample.SimpleAlarmBot
{
[LuisModel("c413b2ef-382c-45bd-8ff0-f76d60e2a821", "6d0966209c6e4f6b835ce34492f3e6d9")]
[Serializable]
public class SimpleAlarmDialog : LuisDialog<object>
{
private readonly Dictionary<string, Alarm> alarmByWhat = new Dictionary<string, Alarm>();
public const string DefaultAlarmWhat = "default";
public bool TryFindAlarm(LuisResult result, out Alarm alarm)
{
alarm = null;
string what;
EntityRecommendation title;
if (result.TryFindEntity(Entity_Alarm_Title, out title))
{
what = title.Entity;
}
else
{
what = DefaultAlarmWhat;
}
return this.alarmByWhat.TryGetValue(what, out alarm);
}
public const string Entity_Alarm_Title = "builtin.alarm.title";
public const string Entity_Alarm_Start_Time = "builtin.alarm.start_time";
public const string Entity_Alarm_Start_Date = "builtin.alarm.start_date";
[LuisIntent("")]
public async Task None(IDialogContext context, LuisResult result)
{
string message = $"Sorry I did not understand: " + string.Join(", ", result.Intents.Select(i => i.Intent));
await context.PostAsync(message);
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.delete_alarm")]
public async Task DeleteAlarm(IDialogContext context, LuisResult result)
{
Alarm alarm;
if (TryFindAlarm(result, out alarm))
{
this.alarmByWhat.Remove(alarm.What);
await context.PostAsync($"alarm {alarm} deleted");
}
else
{
await context.PostAsync("did not find alarm");
}
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.find_alarm")]
public async Task FindAlarm(IDialogContext context, LuisResult result)
{
Alarm alarm;
if (TryFindAlarm(result, out alarm))
{
await context.PostAsync($"found alarm {alarm}");
}
else
{
await context.PostAsync("did not find alarm");
}
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.set_alarm")]
public async Task SetAlarm(IDialogContext context, LuisResult result)
{
EntityRecommendation title;
if (!result.TryFindEntity(Entity_Alarm_Title, out title))
{
title = new EntityRecommendation(type: Entity_Alarm_Title) { Entity = DefaultAlarmWhat };
}
EntityRecommendation date;
if (!result.TryFindEntity(Entity_Alarm_Start_Date, out date))
{
date = new EntityRecommendation(type: Entity_Alarm_Start_Date) { Entity = string.Empty };
}
EntityRecommendation time;
if (!result.TryFindEntity(Entity_Alarm_Start_Time, out time))
{
time = new EntityRecommendation(type: Entity_Alarm_Start_Time) { Entity = string.Empty };
}
var parser = new Chronic.Parser();
var span = parser.Parse(date.Entity + " " + time.Entity);
if (span != null)
{
var when = span.Start ?? span.End;
var alarm = new Alarm() { What = title.Entity, When = when.Value };
this.alarmByWhat[alarm.What] = alarm;
string reply = $"alarm {alarm} created";
await context.PostAsync(reply);
}
else
{
await context.PostAsync("could not find time for alarm");
}
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.snooze")]
public async Task AlarmSnooze(IDialogContext context, LuisResult result)
{
Alarm alarm;
if (TryFindAlarm(result, out alarm))
{
alarm.When = alarm.When.Add(TimeSpan.FromMinutes(7));
await context.PostAsync($"alarm {alarm} snoozed!");
}
else
{
await context.PostAsync("did not find alarm");
}
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.time_remaining")]
public async Task TimeRemaining(IDialogContext context, LuisResult result)
{
Alarm alarm;
if (TryFindAlarm(result, out alarm))
{
var now = DateTime.UtcNow;
if (alarm.When > now)
{
var remaining = alarm.When.Subtract(DateTime.UtcNow);
await context.PostAsync($"There is {remaining} remaining for alarm {alarm}.");
}
else
{
await context.PostAsync($"The alarm {alarm} expired already.");
}
}
else
{
await context.PostAsync("did not find alarm");
}
context.Wait(MessageReceived);
}
private Alarm turnOff;
[LuisIntent("builtin.intent.alarm.turn_off_alarm")]
public async Task TurnOffAlarm(IDialogContext context, LuisResult result)
{
if (TryFindAlarm(result, out this.turnOff))
{
PromptDialog.Confirm(context, AfterConfirming_TurnOffAlarm, "Are you sure?");
}
else
{
await context.PostAsync("did not find alarm");
context.Wait(MessageReceived);
}
}
public async Task AfterConfirming_TurnOffAlarm(IDialogContext context, IAwaitable<bool> confirmation)
{
if (await confirmation)
{
this.alarmByWhat.Remove(this.turnOff.What);
await context.PostAsync($"Ok, alarm {this.turnOff} disabled.");
}
else
{
await context.PostAsync("Ok! We haven't modified your alarms!");
}
context.Wait(MessageReceived);
}
[LuisIntent("builtin.intent.alarm.alarm_other")]
public async Task AlarmOther(IDialogContext context, LuisResult result)
{
await context.PostAsync("what ?");
context.Wait(MessageReceived);
}
public SimpleAlarmDialog(ILuisService service = null)
: base(service)
{
}
[Serializable]
public sealed class Alarm : IEquatable<Alarm>
{
public DateTime When { get; set; }
public string What { get; set; }
public override string ToString()
{
return $"[{this.What} at {this.When}]";
}
public bool Equals(Alarm other)
{
return other != null
&& this.When == other.When
&& this.What == other.What;
}
public override bool Equals(object other)
{
return Equals(other as Alarm);
}
public override int GetHashCode()
{
return this.What.GetHashCode();
}
}
}
}
Please put the following class in MessageController.cs. When the request comes to the web api it will hit the below Post Method.
[BotAuthentication]
public class MessagesController : ApiController
{
public async Task<Message> Post([FromBody]Message message)
{
return await Conversation.SendAsync(message, () => new SimpleAlarmDialog());
}
}
Thanks, I appreciate the response. So is the documented example incomplete? I placed it, but it then generates a load of CS0246 errors.
"The type or namespace name 'BotAuthenticationAttribute' could not be found (are you missing a using directive or an assembly reference?)
Here is where I put the code:
...
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
namespace Microsoft.Bot.Sample.SimpleAlarmBot
{
[BotAuthentication]
public class MessagesController : ApiController
{
public async Task<Message> Post([FromBody]Message message)
{
return await Conversation.SendAsync(message, () => new SimpleAlarmDialog());
}
}
[LuisModel("c413b2ef-382c-45bd-8ff0-f76d60e2a821", "6d0966209c6e4f6b835ce34492f3e6d9")]
[Serializable]
public class SimpleAlarmDialog : LuisDialog<object>
{
...
Put using Microsoft.Bot.Connector;.
The complete sample which the docs refer is here. There are other samples also which you could have a look at.
Super. Thanks very much, that is a great help.
I have the same Problem with the LuisBot Sample, a more complicated sample using Luis. I got the same Error. When i paste the code from above into my MessageController.cs, i get an error, because type "message" is not recognized. ...
@daschdasch i think you are still using old version. Please update your bot builder SDK and now its called Activity.
public async Task<HttpResponseMessage> Post([FromBody]Activity activity) { }
@b4usat thanks
@tomlm : I always have issue with bot emulator (V3.5.29) when running a bot within my company which is using some external api like LUIS in bot code. Without LUIS i can debug and run bot using emulator.
Same code with LUIS works fine outside my company on my personal laptop with same emulator.
Is there any way to find what is being blocked within my organization when using LUIS.
FYI, LUIS code works fine in my organization also if copy LUIS code in a C# console application so that means LUIS url is not blocked. Problem is only when I am using bot emulator.
Any help pls ?
Hi ,
Am also facing the same issue.

am using latest BOT SKD and Bot emulator(3.5.34).
This is the controller
namespace LeaderCoachBot
{
[BotAuthentication]
public class MessagesController : ApiController
{
///
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
and LUISDialog.cs namespace LeaderCoachBot.Dialogs { [LuisModel("8eefcccc-f1d8-4d02-b11e-389862e81685", "a6aad4b2faf9495ea7f046d3a62fd82c")] [Serializable] public class LUISDialog : LuisDialog
@agollasrikanth can you please open a new issue?