An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
DelegateHandler in non-http service
I have this API
public class ApiService : ServicesBase, IApiService
{
public ApiService(IMapper mapper, IConfiguration configuration, ICheckService check, ServiceClient serviceClient) :
base(mapper, configuration, check, serviceClient)
{
}
public override async Task<Compute> ExecuteComputation(string id, string sessionId)
{
try
{
if (await Check())
{
var response = await ExecuteComputation(new Request
{
InputContract = new ContextDTO()
{
Id = id,
Session = sessionId
}
});
return response;
}
else
{
throw new ValidationException("Invalid value");
}
}
catch (Exception ex)
{
throw await Task.FromException<Exception>(new Exception(ex.Message));
}
}
}
public abstract class ServicesBase : IServices
{
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
private readonly ICheckService _check;
private readonly ServiceClient _ServiceClient;
public ServicesBase(IMapper mapper, IConfiguration configuration, ICheckService check, ServiceClient serviceClient)
{
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_check = check ?? throw new ArgumentNullException(nameof(check));
_ServiceClient = serviceClient ?? throw new ArgumentNullException(nameof(serviceClient));
}
public abstract Task<Compute> ExecuteComputation(string id, string session);
protected async Task<Compute> ExecuteComputation(Request input)
{
var response = await _serviceClient.ExecuteComputation(input);
if (response.Status != ResponseStatus.Success && response.Status != ResponseStatus.Warning)
{
return new Compute()
{
Errors = new Models.Errors()
{
Status = (int)(response.Status),
Messagge = response.Description
}
};
}
return _mapper.Map<Compute>(response.Output);
}
protected async Task<bool> Check()
{
var validation = await _check.GetValidationData(_configuration.GetValue<string>(AppConfiguration.KindOfData), _configuration.GetValue<string>(AppConfiguration.Value));
if (!string.IsNullOrEmpty(validation?.ExpectedValue))
{
return true;
}
return false;
}
}
public interface IServices
{
Task<Compute> ExecuteComputation(string id, string session);
}
In my ApiService I have some methods like ExecuteComputation, I know that as regards the HttpClient it is possible to add the delegate AddHttpMessageHandler to intercept SendAsync method to verify for example the authentication or other, is there something similar for non-http services?
Let me explain better I would like to avoid that the ApiService methods have to worry about performing the Check but delegate it precisely, that is, I call the method of the ServiceBase class the delegate intercepts, verifies the check and then redirects to the base method
Developer technologies | C#
Developer technologies | ASP.NET Core | Other
A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.