我正在使用MediatR請求 - 回應記錄在我的應用程式中使用IPipelineBehavior<TRequest, TResponse>
代碼示例:
internal sealed class AppLoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ILogger<AppLoggingBehavior<TRequest, TResponse>> _logger;
public AppLoggingBehavior(ILogger<AppLoggingBehavior<TRequest, TResponse>> logger)
{
_logger = logger;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
string requestName = typeof(TRequest).Name;
string unqiueId = Guid.NewGuid().ToString();
string requestJson = JsonSerializer.Serialize(request);
_logger.LogInformation($"Begin Request Id:{unqiueId}, request name:{requestName}, request json:{requestJson}");
var timer = new Stopwatch();
timer.Start();
var response = await next();
timer.Stop();
_logger.LogInformation($"End Request Id:{unqiueId}, request name:{requestName}, total request time:{timer.ElapsedMilliseconds}ms");
return response;
}
}
但是升級到之后,Nuget - v10.0.0我開始收到以下編譯錯誤。
型別“TRequest”不能用作泛型型別或方法“IPipelineBehavior<TRequest, TResponse>”中的型別引數“TRequest”。沒有從“TRequest”到“MediatR.IRequest”的裝箱轉換或型別引數轉換
我設法從官方回購中找到了移植指南MediatR。但是找不到任何例子。
我是否還缺少其他東西,請有人可以幫助我嗎?
uj5u.com熱心網友回復:
您還需要TRequest在抽象類中指定引數的型別。它必須至少作為您嘗試實作的介面中的引數而具體化。
internal sealed class AppLoggingBehavior<TRequest, TResponse>
: IPipelineBehavior<TRequest, TResponse>
where TRequest : MediatR.IRequest<TResponse> // <- this is the part you're missing
{
// rest of your code...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407933.html
標籤:
上一篇:從動態物件生成類定義
