public async Task InvokeAsync(PipelineContext context, PipelineDelegate next) { if (_queryParameters.Any()) { if (!context.GetFirst(out Uri uri)) { throw new PipelineDependencyException <Uri>(this); } var keyValuePairs = NameValueToKeyValuePairs(HttpUtility.ParseQueryString(uri.Query)); foreach (var queryParameter in _queryParameters) { if (context.Get(queryParameter.ContextVariableName, out string contextParameterValue) && !string.IsNullOrEmpty(contextParameterValue)) { keyValuePairs.Add(new KeyValuePair <string, string>(queryParameter.ContextVariableName, contextParameterValue)); } else if (!queryParameter.Optional) { context.AddNotification(this, $"Context variable '{queryParameter.ContextVariableName}' was not found or does not contain a value and is not optional"); return; } } var ub = new UriBuilder(uri); QueryBuilder qb = new QueryBuilder(keyValuePairs); ub.Query = qb.ToString(); context.AddOrReplace(ub.Uri); await next.Invoke(); } }
public async Task InvokeAsync(PipelineContext context, PipelineDelegate next) { if (!context.GetFirst(out HttpRequestMessage httpRequestMessage)) { throw new PipelineDependencyException <HttpRequestMessage>(this); } HttpClientRequest request = new HttpClientRequest(httpRequestMessage); CancellationTokenSource cts = new CancellationTokenSource(RequestTimeout); var cancellationTokens = context.Get <CancellationToken>(); var token = cancellationTokens.Any() ? CancellationTokenSource.CreateLinkedTokenSource( cancellationTokens.Concat(new[] { cts.Token }).ToArray() ).Token : cts.Token; try { var result = await request.InvokeAsync(token); context.Add(result); await next.Invoke(); } catch (HttpRequestException ex) { throw new PipelineException(this, ex.Message, ex); } catch (Exception ex) { throw new PipelineException(this, ex.Message, ex); } }
public async Task InvokeAsync(PipelineContext context, PipelineDelegate next) { try { await next.Invoke(); } finally { IActionResult objectResult = null; if (context.Get("response", out T response)) { objectResult = new ObjectResult(response) { StatusCode = 200 } } ; else { objectResult = new ObjectResult( new { context.Id, notifications = context .Get <Notification>() .Select((x, i) => new { id = i + 1, Handler = x.Handler.ToString(), x.ErrorMessage }) }) { StatusCode = 500 }; } context.Add <IActionResult>(objectResult); } }
private static object ResolverImplementation <T>(ref PipelineContext context) { var container = context.Container; var name = context.Name; context.Existing = new Lazy <T>(() => container.Resolve <T>(name)); var lifetime = context.Get(typeof(LifetimeManager)); if (lifetime is PerResolveLifetimeManager) { var perBuildLifetime = new RuntimePerResolveLifetimeManager(context.Existing); context.Set(typeof(LifetimeManager), perBuildLifetime); } return(context.Existing); }
public async Task InvokeAsync(PipelineContext context, PipelineDelegate next) { if (context.Get("endpoint", out string endPoint)) { if (BaseUrl == null) { context.AddNotification(this, "Missing BaseUrl value"); return; } var uri = BaseUrl.Append(endPoint); context.Add(uri); await next.Invoke(); } else { context.AddNotification(this, "Missing endpoint information"); } }
public void Context_Add() { //given _ctx.Add(5); //then Assert.AreEqual(5, _ctx.Count); Assert.AreEqual(4, _ctx.Get <int>().Count()); Assert.AreEqual(1, _ctx.Get <string>().Count()); CollectionAssert.AreEquivalent( new[] { 1, 2, 3, 5 }, _ctx.Get <int>().ToArray()); CollectionAssert.AreEquivalent( new[] { "4" }, _ctx.Get <string>().ToArray()); }
public async Task InvokeAsync(PipelineContext context, PipelineDelegate next) { if (context.GetFirst(out IEnumerable <DublinBikeStation> response)) { if (context.Get("orderBy", out string value)) { var index = orderIdentifiers .Select((x, i) => new { index = i, identifer = x }) .Where(x => string.Compare(x.identifer, value, true) == 0) .Select(x => x.index) .FirstOrDefault(); switch (index) { case 1: response = response.OrderBy(x => x.Name); break; case 2: response = response.OrderByDescending(x => x.Available_bikes); break; case 3: response = response.OrderByDescending(x => x.Available_bike_stands); break; case 4: response = response.OrderByDescending(x => x.Last_update); break; default: response = response.OrderBy(x => x.Number); break; } context.AddOrReplace("response", response.Select(x => x)); } } await next.Invoke(); }