public void Create_IfInOutByRefMethodReturnsTask_RoundtripsArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestInOutByRefReturnTask"); int expectedA = 1; string expectedInitialB = "B"; string expectedFinalB = "b"; object[] expectedC = new object[] { new object(), default(int), String.Empty }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert Assert.NotNull(invoker); bool callbackCalled = false; InOutRefTaskFunc callback = delegate(int a, ref string b, out object[] c) { callbackCalled = true; Assert.Equal(expectedA, a); Assert.Same(expectedInitialB, b); b = expectedFinalB; c = expectedC; return(Task.FromResult(0)); }; MethodInvokerFactoryTests instance = GetInstance(isInstance); object[] arguments = new object[] { expectedA, expectedInitialB, null, callback }; invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult(); Assert.True(callbackCalled); Assert.Same(expectedFinalB, arguments[1]); Assert.Same(expectedC, arguments[2]); }
public void Create_IfMultipleInputParameters_PassesInputArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestIntStringObjectArray"); int expectedA = 1; string expectedB = "B"; object[] expectedC = new object[] { new object() }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert Assert.NotNull(invoker); bool callbackCalled = false; Action <int, string, object> callback = (a, b, c) => { callbackCalled = true; Assert.Equal(expectedA, a); Assert.Same(expectedB, b); Assert.Same(expectedC, c); }; MethodInvokerFactoryTests instance = GetInstance(isInstance); object[] arguments = new object[] { expectedA, expectedB, expectedC, callback }; invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult(); Assert.True(callbackCalled); }
public void Create_IfMultipleOutputParameters_SetsOutputArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestOutIntStringObjectArray"); int expectedA = 1; string expectedB = "B"; object[] expectedC = new object[] { new object() }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert Assert.NotNull(invoker); bool callbackCalled = false; OutAction callback = delegate(out int a, out string b, out object[] c) { callbackCalled = true; a = expectedA; b = expectedB; c = expectedC; }; MethodInvokerFactoryTests instance = GetInstance(isInstance); object[] arguments = new object[] { default(int), null, null, callback }; invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult(); Assert.True(callbackCalled); Assert.Equal(expectedA, arguments[0]); Assert.Same(expectedB, arguments[1]); Assert.Same(expectedC, arguments[2]); }
public void InvokeAsync_DelegatesToLambda() { // Arrange object expectedInstance = new object(); object[] expectedArguments = new object[0]; bool invoked = false; object instance = null; object[] arguments = null; Func <object, object[], Task <object> > lambda = (i, a) => { invoked = true; instance = i; arguments = a; return(Task.FromResult <object>(null)); }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); // Act Task task = invoker.InvokeAsync(expectedInstance, expectedArguments); // Assert Assert.NotNull(task); task.GetAwaiter().GetResult(); Assert.True(invoked); Assert.Same(expectedInstance, instance); Assert.Same(expectedArguments, arguments); }
public void InvokeAsync_IfLambdaReturnsTaskWhenAllFaultedTask_ReturnsFaultedTask() { // Arrange Exception expectedException = new InvalidOperationException(); Func <object, object[], Task <object> > lambda = async(i1, i2) => { Task innerTask = new Task(() => { throw expectedException; }); innerTask.Start(); Assert.False(innerTask.GetType().IsGenericType); // Guard await Task.WhenAll(innerTask); return(null); }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Faulted, task.Status); Assert.NotNull(task.Exception); Assert.Same(expectedException, task.Exception.InnerException); }
public void InvokeAsync_IfLambdaReturnsTaskWhenAllCancelledTask_ReturnsCancelledTask() { // Arrange Func <object, object[], Task <object> > lambda = async(i1, i2) => { var cancellationSource = new System.Threading.CancellationTokenSource(); Task innerTask = new Task(() => { }, cancellationSource.Token); Assert.False(innerTask.GetType().IsGenericType); // Guard cancellationSource.Cancel(); await Task.WhenAll(innerTask); return(null); }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Canceled, task.Status); }
public void InvokeAsync_IfLambdaReturnsTaskWhenAllCancelledTaskWithReturnTypes_ReturnsCancelledTask() { // Arrange Func <object, object[], Task <object> > lambda = async(i1, i2) => { TaskCompletionSource <object> source = new TaskCompletionSource <object>(); source.SetCanceled(); await Task.WhenAll(source.Task); return(null); }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Canceled, task.Status); }
public void InvokeAsync_IfLambdaReturnsFaultedTask_ReturnsFaultedTask() { // Arrange Exception expectedException = new InvalidOperationException(); Func <object, object[], Task <object> > lambda = (i1, i2) => { TaskCompletionSource <object> source = new TaskCompletionSource <object>(); source.SetException(expectedException); return(source.Task); }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Faulted, task.Status); Assert.NotNull(task.Exception); Assert.Same(expectedException, task.Exception.InnerException); }
public async Task <object> InvokeAsync(object instance, object[] arguments) { // Return a task immediately in case the method is not async. await Task.Yield(); return(await _methodInvoker.InvokeAsync((TReflected)instance, arguments)); }
public async Task InvokeAsync(object[] arguments) { TReflected instance = _instanceFactory.Create(); using (instance as IDisposable) { await _methodInvoker.InvokeAsync(instance, arguments); } }
public async Task InvokeAsync(object[] arguments) { // Return a task immediately in case the method is not async. await Task.Yield(); TReflected instance = _instanceFactory.Create(); using (instance as IDisposable) { await _methodInvoker.InvokeAsync(instance, arguments); } }
public override async Task InvokeAsync(MethodInfo method, object?[] args) { try { await _invoker.InvokeAsync(method, args); } catch (Exception e) { OnExceptionInvoked(new EventArgsT <Exception>(e)); throw; } }
public void InvokeAsync_IfLambdaReturnsNull_ReturnsNull() { // Arrange Func <object, object[], Task <object> > lambda = (i1, i2) => null; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.Null(task); }
public void Create_IfReturnsTaskAndTaskCanceled_ReturnsCanceledTask(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ReturnCanceledTask"); // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert MethodInvokerFactoryTests instance = GetInstance(isInstance); Task task = invoker.InvokeAsync(instance, null); Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Canceled, task.Status); }
public void Create_IfReturnsTaskAndTaskCanceled_ReturnsCanceledTask(string functionName) { // Arrange MethodInfo method = GetMethodInfo(functionName); // Act IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker = _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method); // Assert DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic); Task task = invoker.InvokeAsync(instance, null); Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Canceled, task.Status); }
public void InvokeAsync_IfLambdaReturnsNestedTask_Throws() { // Arrange Func <object, object[], Task> lambda = (i1, i2) => { TaskCompletionSource <Task> source = new TaskCompletionSource <Task>(); source.SetResult(null); return(source.Task); }; IMethodInvoker <object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act & Assert ExceptionAssert.ThrowsInvalidOperation(() => invoker.InvokeAsync(instance, arguments), "Returning a nested Task is not supported. Did you mean to await or Unwrap the task instead of " + "returning it?"); }
public async Task InvokeAsync_IfLambdaThrows_PropogatesException() { // Arrange InvalidOperationException expectedException = new InvalidOperationException(); Func <object, object[], Task <object> > lambda = (i1, i2) => { throw expectedException; }; IMethodInvoker <object, object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act & Assert InvalidOperationException exception = await Assert.ThrowsAsync <InvalidOperationException>( () => invoker.InvokeAsync(instance, arguments)); Assert.Same(expectedException, exception); }
public void Create_IfParameterlessMethod_CanInvoke(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ParameterlessMethod"); // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); try { // Assert MethodInvokerFactoryTests instance = GetInstance(isInstance); invoker.InvokeAsync(instance, null).GetAwaiter().GetResult(); Assert.True(_parameterlessMethodCalled); } finally { _parameterlessMethodCalled = false; } }
public void Create_IfParameterlessMethod_CanInvoke(string functionName) { // Arrange MethodInfo method = GetMethodInfo(functionName); // Act IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker = _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method); try { // Assert DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic); invoker.InvokeAsync(instance, null).GetAwaiter().GetResult(); Assert.True(_parameterlessMethodCalled); } finally { _parameterlessMethodCalled = false; } }
public void InvokeAsync_IfLambdaReturnsTaskWhenAllTaskWithReturnTypes_ReturnsCompletedTask() { // Arrange Func <object, object[], Task> lambda = (i1, i2) => { Task innerTask = Task.WhenAll(Task.FromResult(0)); return(innerTask); }; IMethodInvoker <object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); }
public void Create_IfMultipleReferenceParameters_RoundtripsArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestByRefIntStringObjectArray"); int expectedInitialA = 1; string expectedInitialB = "B"; object[] expectedInitialC = new object[] { new object() }; int expectedFinalA = 2; string expectedFinalB = "b"; object[] expectedFinalC = new object[] { new object(), default(int), String.Empty }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert Assert.NotNull(invoker); bool callbackCalled = false; ByRefAction callback = delegate(ref int a, ref string b, ref object[] c) { callbackCalled = true; Assert.Equal(expectedInitialA, a); Assert.Same(expectedInitialB, b); Assert.Same(expectedInitialC, c); a = expectedFinalA; b = expectedFinalB; c = expectedFinalC; }; MethodInvokerFactoryTests instance = GetInstance(isInstance); object[] arguments = new object[] { expectedInitialA, expectedInitialB, expectedInitialC, callback }; invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult(); Assert.True(callbackCalled); Assert.Equal(expectedFinalA, arguments[0]); Assert.Same(expectedFinalB, arguments[1]); Assert.Same(expectedFinalC, arguments[2]); }
public void InvokeAsync_IfLambdaReturnsTaskDelayTask_ReturnsCompletedTask() { // Arrange Func <object, object[], Task> lambda = (i1, i2) => { Task innerTask = Task.Delay(1); Assert.False(innerTask.GetType().IsGenericType); // Guard return(innerTask); }; IMethodInvoker <object> invoker = CreateProductUnderTest(lambda); object instance = null; object[] arguments = null; // Act Task task = invoker.InvokeAsync(instance, arguments); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); }
public override async Task <object> InvokeAsync(object target, params object[] parameters) => await invoker.InvokeAsync(target, parameters);
public async Task Invoke(HttpContext httpContext, ContractMethodBinding binding) { if (httpContext.Request.ContentType == null && httpContext.Request.ContentLength != 0) { httpContext.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType; return; } IInvocationSerializer requestSerializer = null; if (httpContext.Request.ContentType != null && !invocationSerializers.TryGetValue(httpContext.Request.ContentType, out requestSerializer)) { httpContext.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType; return; } IInvocationSerializer responseSerializer = null; StringValues acceptHeader = httpContext.Request.Headers["Accept"]; if (acceptHeader.Count == 0 || (acceptHeader.Count == 1 && acceptHeader[0] == "*/*")) { // Use request serializer or use the first one resolved responseSerializer = requestSerializer ?? invocationSerializers.Values.First(); } else if (!acceptHeader.Any(value => invocationSerializers.TryGetValue(value, out responseSerializer))) { httpContext.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; return; } ArgumentDictionary arguments; Dictionary <string, Type> argumentMapping = binding.ContractMethodInfo.GetParameters().ToDictionary(x => x.Name, x => x.ParameterType); if (httpContext.Request.ContentLength != 0) { arguments = await requestSerializer.DeserializeArgumentsAsync(httpContext.Request.Body, argumentMapping); } else { arguments = new ArgumentDictionary(); } object contractImplementation = contractImplementationResolver.Resolve(binding.ContractType); AuthorizationContext authorizationContext = await authorizationContextProvider.GetAuthorizationContextAsync(contractImplementation, binding.ContractMethodInfo); if (!await authorizationHandler.AuthorizeAsync(httpContext, authorizationContext)) { return; } var result = await methodInvoker.InvokeAsync(binding.ContractMethodInfo, contractImplementation, arguments.Values.ToArray()); if (result == null) { httpContext.Response.StatusCode = (int)HttpStatusCode.NoContent; return; } else if (responseSerializer != null) { httpContext.Response.ContentType = responseSerializer.MediaType; httpContext.Response.StatusCode = (int)HttpStatusCode.OK; await responseSerializer.SerializeAsync(httpContext.Response.Body, result.GetType(), result); } else { httpContext.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; return; } }
protected override object Invoke(IMethodInvoker invoker, object target, params object[] args) => invoker.InvokeAsync(target, args).WaitAndGetResult();
public Task <object> InvokeAsync(object instance, object[] arguments) { return(_methodInvoker.InvokeAsync((TInstance)instance, arguments) .ContinueWith <object>(t => t.Result)); }