| 值 | |
|---|---|
| 规则 ID | ASP0016 |
| 类别 | 使用情况 |
| 修复是中断修复还是非中断修复 | 非中断 |
原因
用于创建 RequestDelegate 的方法会返回 Task<T>。 RequestDelegate 会放弃此值。
规则说明
不要返回提供给需要 RequestDelegate 的 API 的 Delegate 值。 例如,以下示例返回 Task<string>,其中将放弃 Task 的 string 值。
var app = WebApplication.Create();
app.Use(next =>
{
return new RequestDelegate((HttpContext context) =>
{
return Task.FromResult(""hello world"");
});
});
如何解决冲突
若要解决此规则的冲突,请将返回类型更改为非泛型 Task;如果委托是路由处理程序,请将其强制转换为 Delegate,以便将返回值写入响应。
何时禁止显示警告
不禁止显示此规则发出的警告。