C
C#10mo ago
Camster

✅ MediatR Pipeline where TResponse has a generic type

All my API methods return a Result wrapper, like so:
public class Result
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public TimeSpan Time { get; set; }
public Exception ResultException { get; set; }
}

public class Result<T> : Result
{
public T Obj { get; set; }
}
public class Result
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public TimeSpan Time { get; set; }
public Exception ResultException { get; set; }
}

public class Result<T> : Result
{
public T Obj { get; set; }
}
2 Replies
Camster
CamsterOP10mo ago
I want to use MediatR's pipeline to log exceptions to a separate database. But I'm not quite sure how to add a third generic type to the IPipelineBehavior. I tried this:
// Without response object
public class ExceptionPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, Result>
{
private IExceptionHandler _handler;

public ExceptionPipeline(IExceptionHandler handler)
{
_handler = handler;
}

public async Task<Result> Handle(TRequest request, RequestHandlerDelegate<Result> next, CancellationToken cancellationToken)
{
Result response;
try
{
response = await next();
} catch (Exception ex)
{
var message = await _handler.LogException(ex);
response = Result.Failure(message);
}
return response;
}
}

// With response object
public class ExceptionPipeline<TRequest, TResponse, TResult> : IPipelineBehavior<TRequest, Result<TResult>>
{
private IExceptionHandler _handler;

public ExceptionPipeline(IExceptionHandler handler)
{
_handler = handler;
}

public async Task<Result<TResult>> Handle(TRequest request, RequestHandlerDelegate<Result<TResult>> next, CancellationToken cancellationToken)
{
Result<TResult> response;
try
{
response = await next();
} catch (Exception ex)
{
var message = await _handler.LogException(ex);
response = Result<TResult>.Failure(message);
}
return response;
}
}
// Without response object
public class ExceptionPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, Result>
{
private IExceptionHandler _handler;

public ExceptionPipeline(IExceptionHandler handler)
{
_handler = handler;
}

public async Task<Result> Handle(TRequest request, RequestHandlerDelegate<Result> next, CancellationToken cancellationToken)
{
Result response;
try
{
response = await next();
} catch (Exception ex)
{
var message = await _handler.LogException(ex);
response = Result.Failure(message);
}
return response;
}
}

// With response object
public class ExceptionPipeline<TRequest, TResponse, TResult> : IPipelineBehavior<TRequest, Result<TResult>>
{
private IExceptionHandler _handler;

public ExceptionPipeline(IExceptionHandler handler)
{
_handler = handler;
}

public async Task<Result<TResult>> Handle(TRequest request, RequestHandlerDelegate<Result<TResult>> next, CancellationToken cancellationToken)
{
Result<TResult> response;
try
{
response = await next();
} catch (Exception ex)
{
var message = await _handler.LogException(ex);
response = Result<TResult>.Failure(message);
}
return response;
}
}
But I get an error that basically says the implementation Type can't be converted to the service type. Any help would be appreciated!
Camster
CamsterOP10mo ago
GitHub
Issue with Generic Wrapper as response type in pipeline · Issue #18...
When adding a Generic request result wrapper to all of my mediator request response types, the pipeline refuses to execute, throwing a generic type constraint exception that the request does not fi...
Want results from more Discord servers?
Add your server