private async Task <RequestResult <Response> > SendApiMethodInternal <Response>( ServiceContext serviceContext, Func <Task <Response> > apiMethodFunc, ApiMethodContext apiContext) where Response : class { apiContext ??= ApiMethodContext.Default; var filters = GetServiceFilters(serviceContext.IncludeFilters, serviceContext.ExcludeFilters); try { return(await Policy .Handle <ApiMethodException>() .Or <ApiException>() .WaitAndRetryAsync( serviceContext.MaxRetryCount, retryNumber => TimeSpan.FromMilliseconds(serviceContext.SleepTime), (exception, i) => HandleExceptionAsync(filters, exception, serviceContext, apiContext)) .ExecuteAsync(ct => ExecuteApiMethod(filters, serviceContext, apiMethodFunc, apiContext), (CancellationToken)apiContext)); } catch (Exception ex) when(ex is OperationCanceledException || ex is TaskCanceledException) { throw new NetworkConnectionException(ex.Message, ex); } catch (ApiMethodException ex) { throw ex.InnerException ?? ex; } }
/// <summary> /// Выполнить метод. /// </summary> /// <param name="serviceContext">Контекст сервиса.</param> /// <param name="apiFunc">Делегат выполняемого метода.</param> /// <param name="apiContext">Апи контекст.</param> /// <typeparam name="TResult">Тип результата.</typeparam> /// <returns>Результат выполненного метода.</returns> public Task <RequestResult <TResult> > SendApiMethod <TResult>( ServiceContext serviceContext, Func <Task <TResult> > apiFunc, ApiMethodContext apiContext) where TResult : class { return(SendApiMethodInternal(serviceContext, apiFunc, apiContext)); }
protected Task <RequestResult <TResult> > SendApiMethod <TResult>( Func <Task <TResult> > apiFunc, ApiMethodContext apiContext, [CallerMemberName] string callerMethodName = null) where TResult : class { var context = GetServiceMethodContext(callerMethodName) ?? Context; return(ServiceDecorator.SendApiMethod(context, apiFunc, apiContext)); }
private static async Task HandleExceptionAsync( IList <IServiceFilter> filters, Exception ex, ServiceContext serviceContext, ApiMethodContext apiContext) { ex.Data["serviceType"] = serviceContext.ServiceType.Name; await CheckTryResolveException(filters, ex, serviceContext, apiContext); }
private static async Task CheckDoRequest( IList <IServiceFilter> filters, ServiceContext serviceContext, ApiMethodContext apiContext) { foreach (var filter in filters) { var filterResult = await filter.CanDoRequest(serviceContext, apiContext); if (filterResult.CanContinue) { continue; } throw filterResult.Exception; } }
private static async Task CheckTryResolveException( IList <IServiceFilter> filters, Exception ex, ServiceContext serviceContext, ApiMethodContext apiContext) { foreach (var filter in filters) { var filterResult = await filter.TryResolveException(serviceContext, ex, apiContext); if (filterResult.CanContinue) { continue; } throw filterResult.Exception; } }
private static async Task CheckValidateResult <Response>( IList <IServiceFilter> filters, RequestResult <Response> requestResult, ApiMethodContext apiContext) where Response : class { foreach (var filter in filters) { var filterResult = await filter.ValidateResult(requestResult, apiContext); if (filterResult.CanContinue) { continue; } throw filterResult.Exception; } }
private static async Task <RequestResult <Response> > ExecuteApiMethod <Response>( IList <IServiceFilter> filters, ServiceContext serviceContext, Func <Task <Response> > apiMethodFunc, ApiMethodContext apiContext) where Response : class { await CheckDoRequest(filters, serviceContext, apiContext); RequestResult <Response> requestResult; try { requestResult = new RequestResult <Response>(await apiMethodFunc()); } catch (Exception e) { throw new ApiMethodException(e); } await CheckValidateResult(filters, requestResult, apiContext); return(requestResult); }
public async Task <RequestResult <ContactResult> > GetContacts(int count = 5, int page = 1, ApiMethodContext context = default) { return(await PackageServiceResult(() => _connectionService.GetContacts(count, page, context))); }
/// <inheritdoc /> public Task <ServiceFilterResult> TryResolveException(ServiceContext serviceContext, Exception exception, ApiMethodContext apiContext) { return(Task.FromResult(ServiceFilterResult.Skip)); }
/// <inheritdoc /> public Task <ServiceFilterResult> ValidateResult <Response>(RequestResult <Response> requestResult, ApiMethodContext apiContext) where Response : class { return(Task.FromResult(ServiceFilterResult.Skip)); }
public Task <ServiceFilterResult> CanDoRequest(ServiceContext serviceContext, ApiMethodContext apiContext) { return(CanDoRequest()); }
public async Task <RequestResult <ContactResult> > GetContacts(int count = 5, int page = 1, ApiMethodContext context = default) { return(await SendApiMethod(() => _contactService.GetContacts(count, page), context)); }