diff --git a/Activout.RestClient.Test.Json/RestClientTests.cs b/Activout.RestClient.Test.Json/RestClientTests.cs index af165fe..c642a41 100644 --- a/Activout.RestClient.Test.Json/RestClientTests.cs +++ b/Activout.RestClient.Test.Json/RestClientTests.cs @@ -93,39 +93,6 @@ private IMovieReviewService CreateMovieReviewService(JsonImplementation jsonImpl .Build(); } - [Theory] - [InlineData(JsonImplementation.SystemTextJson)] - [InlineData(JsonImplementation.NewtonsoftJson)] - public async Task TestErrorAsyncWithOldTaskConverter(JsonImplementation jsonImplementation) - { - // arrange - ExpectGetAllReviewsAndReturnError(); - - var reviewSvc = CreateRestClientBuilder(jsonImplementation) - .Accept("application/json") - .ContentType("application/json") - .With(new TaskConverterFactory()) - .With(_loggerFactory.CreateLogger()) - .With(_mockHttp.ToHttpClient()) - .BaseUri(BaseUri) - .Build(); - - // act - var aggregateException = - await Assert.ThrowsAsync(() => reviewSvc.GetAllReviews(MovieId)); - - // assert - _mockHttp.VerifyNoOutstandingExpectation(); - - Assert.IsType(aggregateException.InnerException); - var exception = (RestClientException)aggregateException.InnerException!; - - Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode); - var error = exception.GetErrorResponse(); - Assert.Equal(34, error.Errors[0].Code); - Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message); - } - [Theory] [InlineData(JsonImplementation.SystemTextJson)] [InlineData(JsonImplementation.NewtonsoftJson)] diff --git a/Activout.RestClient/Helpers/Implementation/TaskConverter.cs b/Activout.RestClient/Helpers/Implementation/TaskConverter.cs deleted file mode 100644 index f4b6029..0000000 --- a/Activout.RestClient/Helpers/Implementation/TaskConverter.cs +++ /dev/null @@ -1,81 +0,0 @@ -#nullable disable -using System; -using System.Diagnostics; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Threading.Tasks; - -namespace Activout.RestClient.Helpers.Implementation -{ - /* - * Convert from Task to Task where T is the Type - * - * Implemented by executing Task.ContinueWith(x => (T)x.Result) using reflection. - */ - [Obsolete("This class is obsolete and will be removed in a future version. Use TaskConverter3Factory instead.")] - internal class TaskConverter : ITaskConverter - { - private static readonly Type ObjectTaskType = typeof(Task); - private readonly MethodInfo _continueWith; - private readonly Delegate _lambda; - - public TaskConverter(Type actualReturnType) - { - _lambda = CreateLambda(actualReturnType); - _continueWith = GetContinueWithMethod(actualReturnType); - } - - public object ConvertReturnType(Task task) - { - return _continueWith.Invoke(task, new object[] { _lambda }); - } - - private static MethodInfo GetContinueWithMethod(Type actualReturnType) - { - // Inspired by https://stackoverflow.com/a/3632196/20444 - var baseFuncType = typeof(Func<,>); - var continueWithMethod = ObjectTaskType.GetMethods() - .Where(x => x.Name == nameof(Task.ContinueWith) && x.GetParameters().Length == 1) - .Select(x => new { M = x, P = x.GetParameters() }) - .Where(x => x.P[0].ParameterType.IsGenericType && - x.P[0].ParameterType.GetGenericTypeDefinition() == baseFuncType) - .Select(x => new { x.M, A = x.P[0].ParameterType.GetGenericArguments() }) - .Where(x => x.A[0].IsGenericType - && x.A[0].GetGenericTypeDefinition() == typeof(Task<>)) - .Select(x => x.M) - .SingleOrDefault(); - Debug.Assert(continueWithMethod != null, nameof(continueWithMethod) + " != null"); - return continueWithMethod.MakeGenericMethod(actualReturnType); - } - - private static Delegate CreateLambda(Type actualReturnType) - { - var constantExpression = Expression.Parameter(ObjectTaskType); - var propertyExpression = Expression.Property(constantExpression, "Result"); - var conversion = Expression.Convert(propertyExpression, actualReturnType); - - var lambdaMethod = GetLambdaMethod() ?? - throw new NullReferenceException("Failed to get Expression.Lambda method"); - var lambda = InvokeLambdaMethod(lambdaMethod, conversion, constantExpression); - return lambda.Compile(); - } - - private static LambdaExpression InvokeLambdaMethod(MethodBase lambdaMethod, UnaryExpression expression, - ParameterExpression parameter) - { - return (LambdaExpression)lambdaMethod.Invoke(null, new object[] { expression, new[] { parameter } }); - } - - private static MethodInfo GetLambdaMethod() - { - return typeof(Expression) - .GetMethods() - .Single(x => x.Name == nameof(Expression.Lambda) - && !x.IsGenericMethod - && x.GetParameters().Length == 2 - && x.GetParameters()[0].ParameterType == typeof(Expression) - && x.GetParameters()[1].ParameterType == typeof(ParameterExpression[])); - } - } -} \ No newline at end of file diff --git a/Activout.RestClient/Helpers/Implementation/TaskConverter2.cs b/Activout.RestClient/Helpers/Implementation/TaskConverter2.cs deleted file mode 100644 index db92200..0000000 --- a/Activout.RestClient/Helpers/Implementation/TaskConverter2.cs +++ /dev/null @@ -1,65 +0,0 @@ -#nullable disable -using System; -using System.Reflection; -using System.Threading.Tasks; - -namespace Activout.RestClient.Helpers.Implementation -{ - /* - * Convert from Task to Task where T is the Type - * - * Implemented by creating a TaskCompletionSource and calling SetResult() using reflection. - */ - [Obsolete("This class is obsolete and will be removed in a future version. Use TaskConverter3Factory instead.")] - public class TaskConverter2 : ITaskConverter - { - private readonly Type _type; - private readonly MethodInfo _setResultMethod; - private readonly MethodInfo _setExceptionMethod; - private readonly PropertyInfo _taskProperty; - - public TaskConverter2(Type actualReturnType) - { - _type = typeof(TaskCompletionSource<>).MakeGenericType(actualReturnType); - _setResultMethod = _type.GetMethod("SetResult"); - _setExceptionMethod = _type.GetMethod("SetException", new[] { typeof(Exception) }); - _taskProperty = _type.GetProperty("Task"); - } - - public object ConvertReturnType(Task task) - { - var taskCompletionSource = Activator.CreateInstance(_type); - - Task.Factory.StartNew(async () => await AsyncHelper(task, taskCompletionSource)); - - return GetTask(taskCompletionSource); - } - - private async Task AsyncHelper(Task task, object taskCompletionSource) - { - try - { - SetResult(taskCompletionSource, await task); - } - catch (Exception e) - { - SetException(taskCompletionSource, e); - } - } - - private object GetTask(object taskCompletionSource) - { - return _taskProperty.GetValue(taskCompletionSource); - } - - private void SetException(object taskCompletionSource, Exception e) - { - _setExceptionMethod.Invoke(taskCompletionSource, new object[] { e }); - } - - private void SetResult(object taskCompletionSource, object result) - { - _setResultMethod.Invoke(taskCompletionSource, new[] { result }); - } - } -} \ No newline at end of file diff --git a/Activout.RestClient/Helpers/Implementation/TaskConverter2Factory.cs b/Activout.RestClient/Helpers/Implementation/TaskConverter2Factory.cs deleted file mode 100644 index 9bcb13c..0000000 --- a/Activout.RestClient/Helpers/Implementation/TaskConverter2Factory.cs +++ /dev/null @@ -1,14 +0,0 @@ -#nullable disable -using System; - -namespace Activout.RestClient.Helpers.Implementation -{ - [Obsolete("This class is obsolete and will be removed in a future version. Use TaskConverter3Factory instead.")] - public class TaskConverter2Factory : ITaskConverterFactory - { - public ITaskConverter CreateTaskConverter(Type actualReturnType) - { - return actualReturnType == typeof(void) ? null : new TaskConverter2(actualReturnType); - } - } -} \ No newline at end of file diff --git a/Activout.RestClient/Helpers/Implementation/TaskConverterFactory.cs b/Activout.RestClient/Helpers/Implementation/TaskConverterFactory.cs deleted file mode 100644 index e3e4a8b..0000000 --- a/Activout.RestClient/Helpers/Implementation/TaskConverterFactory.cs +++ /dev/null @@ -1,14 +0,0 @@ -#nullable disable -using System; - -namespace Activout.RestClient.Helpers.Implementation -{ - [Obsolete("This class is obsolete and will be removed in a future version. Use TaskConverter3Factory instead.")] - public class TaskConverterFactory : ITaskConverterFactory - { - public ITaskConverter CreateTaskConverter(Type actualReturnType) - { - return actualReturnType == typeof(void) ? null : new TaskConverter(actualReturnType); - } - } -} \ No newline at end of file