[英]How to capture status code in catch block of an azure function written .net core 3.1 using visual studio 2019
[英]How to catch an exception and respond with a status code in .NET Core
我正在运行.Net Core Web API项目。 我有一个启动文件(如下)。 在Startup.ConfigureServices(...)
方法中,我添加了一个创建IFoo实例的工厂方法。 我想捕获IFooFactory抛出的任何异常,并返回带有状态代码的更好的错误消息。 目前,我收到异常消息的500错误。 有人可以帮忙吗?
public interface IFooFactory
{
IFoo Create();
}
public class FooFactory : IFooFactory
{
IFoo Create()
{
throw new Exception("Catch Me!");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IFooFactory,FooFactory>();
services.AddScoped(serviceProvider => {
IFooFactory fooFactory = serviceProvider.GetService<IFooFactory>();
return fooFactory.Create(); // <== Throws Exception
});
}
}
因此,当我以两种不同的方式阅读问题时,我发布了两个不同的答案 - 大量删除/取消删除/编辑 - 不确定哪一个实际上回答了您的问题:
要弄清楚应用程序启动时出现的问题并且根本不起作用,请尝试以下操作:
使用Startup
的开发人员例外页面:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseDeveloperExceptionPage();
}
在Program
类中:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.CaptureStartupErrors(true) // useful for debugging
.UseSetting("detailedErrors", "true") // what it says on the tin
.Build();
host.Run();
}
如果你想在api正常工作时偶尔处理异常,那么你可以使用一些中间件:
public class ExceptionsMiddleware
{
private readonly RequestDelegate _next;
/// <summary>
/// Handles exceptions
/// </summary>
/// <param name="next">The next piece of middleware after this one</param>
public ExceptionsMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// The method to run in the piepline
/// </summary>
/// <param name="context">The current context</param>
/// <returns>As task which is running the action</returns>
public async Task Invoke(HttpContext context)
{
try
{
await _next.Invoke(context);
}
catch(Exception ex)
{
// Apply some logic based on the exception
// Maybe log it as well - you can use DI in
// the constructor to inject a logging service
context.Response.StatusCode = //Your choice of code
await context.Response.WriteAsync("Your message");
}
}
}
这有一个“问题” - 如果已经发送了响应头,则无法编写状态代码。
您可以使用Configure
方法在Startup
类中配置中间件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseMiddleware<ExceptionsMiddleware>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.