问题
How can I catch all exceptions in dialogs? Is there something like ASP.NET Exception Filter? I want to send different messages to user depending on exception type.
Thank you
回答1:
You are right about the fact that you can use ExceptionFilter
.
You just have to do the following:
Create your ExceptionFilter class, for example to force the tracking of the exception in Application Insights (or in your case handle specific exception types):
using Microsoft.ApplicationInsights;
using System.Net.Http;
using System.Web.Http.Filters;
namespace BotDemo.App_Start
{
public class ExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext ctx)
{
HandleError(ctx);
}
private static void HandleError(HttpActionExecutedContext ctx)
{
ctx.Response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)
{
Content = new StringContent(ctx.Exception.Message)
};
var client = new TelemetryClient();
client.TrackException(ctx.Exception);
}
}
}
Don't forget to define your exception filter in your Application_Start()
:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configuration.Filters.Add(new ExceptionFilter());
...
That's it.
In fact Bot Framework template is using ASP.Net, so you have all the normal features.
来源:https://stackoverflow.com/questions/45068972/error-handling-in-bot-framework-dialogs