/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="cancellationToken">Cancellation token</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> instance that represents the result of the invoked route.</returns> public async Task<Response> Invoke(Route route, CancellationToken cancellationToken, DynamicDictionary parameters, NancyContext context) { object result; try { result = await route.Invoke(parameters, cancellationToken).ConfigureAwait(false); } catch(RouteExecutionEarlyExitException earlyExitException) { context.WriteTraceLog( sb => sb.AppendFormat( "[DefaultRouteInvoker] Caught RouteExecutionEarlyExitException - reason {0}", earlyExitException.Reason)); return earlyExitException.Response; } if (!(result is ValueType) && result == null) { context.WriteTraceLog( sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); result = new Response(); } return this.negotiator.NegotiateResponse(result, context); }
/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> intance that represents the result of the invoked route.</returns> public Response Invoke(Route route, DynamicDictionary parameters, NancyContext context) { var result = route.Invoke(parameters); if (result == null) { context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); result = new Response(); } return this.InvokeRouteWithStrategy(result, context); }
/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> intance that represents the result of the invoked route.</returns> public Response Invoke(Route route, DynamicDictionary parameters, NancyContext context) { var result = route.Invoke(parameters); if (result == null) { context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); result = new Response(); } return(this.InvokeRouteWithStrategy(result, context)); }
/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="cancellationToken">Cancellation token</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> intance that represents the result of the invoked route.</returns> public Task <Response> Invoke(Route route, CancellationToken cancellationToken, DynamicDictionary parameters, NancyContext context) { var tcs = new TaskCompletionSource <Response>(); var result = route.Invoke(parameters, cancellationToken); result.WhenCompleted( completedTask => { var returnResult = completedTask.Result; if (!(returnResult is ValueType) && returnResult == null) { context.WriteTraceLog( sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); returnResult = new Response(); } try { var response = this.negotiator.NegotiateResponse(returnResult, context); tcs.SetResult(response); } catch (Exception e) { tcs.SetException(e); } }, faultedTask => { var earlyExitException = GetEarlyExitException(faultedTask); if (earlyExitException != null) { context.WriteTraceLog( sb => sb.AppendFormat( "[DefaultRouteInvoker] Caught RouteExecutionEarlyExitException - reason {0}", earlyExitException.Reason)); tcs.SetResult(earlyExitException.Response); } else { tcs.SetException(faultedTask.Exception); } }); return(tcs.Task); }
public void Should_return_response_from_action_when_invoked() { //Given var expectedResponse = new Response(); Func<object, Response> action = x => expectedResponse; var route = new Route("/", null, null, action); // When var response = route.Invoke(); // Then response.ShouldBeSameAs(expectedResponse); }
/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="cancellationToken">Cancellation token</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> intance that represents the result of the invoked route.</returns> public Task<Response> Invoke(Route route, CancellationToken cancellationToken, DynamicDictionary parameters, NancyContext context) { var tcs = new TaskCompletionSource<Response>(); var result = route.Invoke(parameters, cancellationToken); result.WhenCompleted( completedTask => { var returnResult = completedTask.Result; if (returnResult == null) { context.WriteTraceLog( sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); returnResult = new Response(); } try { var negotiatedResult = this.InvokeRouteWithStrategy(returnResult, context); tcs.SetResult(negotiatedResult); } catch (Exception e) { tcs.SetException(e); } }, faultedTask => { var earlyExitException = GetEarlyExitException(faultedTask); if (earlyExitException != null) { context.WriteTraceLog( sb => sb.AppendFormat( "[DefaultRouteInvoker] Caught RouteExecutionEarlyExitException - reason {0}", earlyExitException.Reason)); tcs.SetResult(earlyExitException.Response); } else { tcs.SetException(faultedTask.Exception); } }); return tcs.Task; }
public void Should_invoke_action_with_parameters_when_invoked() { //Given RouteParameters capturedParameters = null; Func<dynamic, Response> action = x => { capturedParameters = x; return null; }; dynamic parameters = new RouteParameters(); parameters.foo = 10; parameters.bar = "value"; var route = new Route("/", parameters, null, action); // When route.Invoke(); // Then capturedParameters.ShouldBeSameAs((object)parameters); }
/// <summary> /// Invokes the specified <paramref name="route"/> with the provided <paramref name="parameters"/>. /// </summary> /// <param name="route">The route that should be invoked.</param> /// <param name="parameters">The parameters that the route should be invoked with.</param> /// <param name="context">The context of the route that is being invoked.</param> /// <returns>A <see cref="Response"/> intance that represents the result of the invoked route.</returns> public Response Invoke(Route route, DynamicDictionary parameters, NancyContext context) { dynamic result; try { result = route.Invoke(parameters); } catch (RouteExecutionEarlyExitException earlyExitException) { context.WriteTraceLog(sb => sb.AppendFormat("[DefaultRouteInvoker] Caught RouteExecutionEarlyExitException - reason {0}", earlyExitException.Reason)); result = earlyExitException.Response; } if (result == null) { context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Invocation of route returned null")); result = new Response(); } return this.InvokeRouteWithStrategy(result, context); }
public void Should_invoke_action_with_parameters_when_invoked() { //Given DynamicDictionary capturedParameters = null; Func<dynamic, CancellationToken, Task<object>> action = (args, ct) => { capturedParameters = args; return Task.FromResult<object>(null); }; dynamic parameters = new DynamicDictionary(); parameters.foo = 10; parameters.bar = "value"; var route = new Route<object>("GET", "/", null, action); // When route.Invoke(parameters, new CancellationToken()); // Then capturedParameters.ShouldBeSameAs((object)parameters); }
public async Task Should_return_response_from_action_when_invoked() { //Given var expectedResponse = new Response(); Func<dynamic, CancellationToken, Task<Response>> action = (args, ct) => Task.FromResult(expectedResponse); var route = new Route<Response>("GET", "/", null, action); // When var response = await route.Invoke(new DynamicDictionary(), new CancellationToken()); // Then response.ShouldBeSameAs(expectedResponse); }