| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Linq.Expressions; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Queue.Common.Configuration |
| | | 12 | | { |
| | | 13 | | public static class QueueMiddlewareBuilderExtension |
| | | 14 | | { |
| | | 15 | | private const string InvokeAsyncMethodName = "InvokeAsync"; |
| | | 16 | | |
| | | 17 | | public static IQueueMiddlewareBuilder UseMiddleware<TMiddleware>(this IQueueMiddlewareBuilder app, |
| | | 18 | | params object[] args) |
| | | 19 | | { |
| | 100 | 20 | | return app.UseMiddleware(typeof(TMiddleware), args); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public static IQueueMiddlewareBuilder UseMiddleware(this IQueueMiddlewareBuilder app, Type middleware, |
| | | 24 | | params object[] args) |
| | | 25 | | { |
| | 100 | 26 | | IServiceProvider handshakeServices = app.Services; |
| | 100 | 27 | | return app.Use(next => |
| | 100 | 28 | | { |
| | 100 | 29 | | MethodInfo[] methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public); |
| | 100 | 30 | | MethodInfo[] invokeMethods = methods.Where(m => |
| | 500 | 31 | | string.Equals(m.Name, InvokeAsyncMethodName, StringComparison.Ordinal) |
| | 100 | 32 | | ).ToArray(); |
| | 100 | 33 | | |
| | 100 | 34 | | if (invokeMethods.Length > 1) |
| | 100 | 35 | | { |
| | 100 | 36 | | // TODO: String resources |
| | 0 | 37 | | throw new InvalidOperationException( |
| | 0 | 38 | | $"Resources.FormatException_UseMiddleMultipleInvokes({InvokeAsyncMethodName}"); |
| | 100 | 39 | | } |
| | 100 | 40 | | |
| | 100 | 41 | | if (invokeMethods.Length == 0) |
| | 100 | 42 | | { |
| | 100 | 43 | | // TODO: String resources |
| | 0 | 44 | | throw new InvalidOperationException( |
| | 0 | 45 | | $"Resources.FormatException_UseMiddlewareNoInvokeMethod({InvokeAsyncMethodName}, {middleware}"); |
| | 100 | 46 | | } |
| | 100 | 47 | | |
| | 100 | 48 | | MethodInfo methodInfo = invokeMethods[0]; |
| | 100 | 49 | | if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType)) |
| | 100 | 50 | | { |
| | 100 | 51 | | // TODO: String resources |
| | 0 | 52 | | throw new InvalidOperationException( |
| | 0 | 53 | | $"Resources.FormatException_UseMiddlewareNonTaskReturnType({InvokeAsyncMethodName}, {nameof(Task |
| | 100 | 54 | | } |
| | 100 | 55 | | |
| | 100 | 56 | | ParameterInfo[] parameters = methodInfo.GetParameters(); |
| | 100 | 57 | | if (parameters.Length == 0 || parameters[0].ParameterType != typeof(QueueMessageContext)) |
| | 100 | 58 | | { |
| | 100 | 59 | | // TODO: String resources |
| | 0 | 60 | | throw new InvalidOperationException( |
| | 0 | 61 | | $"Resources.FormatException_UseMiddlewareNoParameters({InvokeAsyncMethodName}, {nameof(QueueMess |
| | 100 | 62 | | } |
| | 100 | 63 | | |
| | 100 | 64 | | object[] ctorArgs = new object[args.Length + 1]; |
| | 100 | 65 | | ctorArgs[0] = next; |
| | 100 | 66 | | Array.Copy(args, 0, ctorArgs, 1, args.Length); |
| | 100 | 67 | | object instance = ActivatorUtilities.CreateInstance(app.Services, middleware, ctorArgs); |
| | 100 | 68 | | if (parameters.Length == 1) |
| | 100 | 69 | | { |
| | 100 | 70 | | return (QueueMessageDispatcherDelegate)methodInfo.CreateDelegate(typeof(QueueMessageDispatcherDelega |
| | 100 | 71 | | } |
| | 100 | 72 | | |
| | 0 | 73 | | Func<object, QueueMessageContext, IServiceProvider, Task> factory = |
| | 0 | 74 | | Compile<object>(methodInfo, parameters); |
| | 100 | 75 | | |
| | 0 | 76 | | return context => |
| | 0 | 77 | | { |
| | 0 | 78 | | IServiceProvider serviceProvider = handshakeServices; |
| | 0 | 79 | | if (serviceProvider == null) |
| | 0 | 80 | | { |
| | 0 | 81 | | // TODO: String resources |
| | 0 | 82 | | throw new InvalidOperationException( |
| | 0 | 83 | | $"Resources.FormatException_UseMiddlewareIServiceProviderNotAvailable({nameof(IServiceProvid |
| | 0 | 84 | | } |
| | 0 | 85 | | |
| | 0 | 86 | | return factory(instance, context, serviceProvider); |
| | 0 | 87 | | }; |
| | 100 | 88 | | }); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | |
| | | 92 | | private static Func<T, QueueMessageContext, IServiceProvider, Task> Compile<T>(MethodInfo methodInfo, |
| | | 93 | | ParameterInfo[] parameters) |
| | | 94 | | { |
| | 0 | 95 | | Type middleware = typeof(T); |
| | 0 | 96 | | ParameterExpression connectionContextArg = |
| | 0 | 97 | | Expression.Parameter(typeof(QueueMessageContext), "connectionContext"); |
| | 0 | 98 | | ParameterExpression providerArg = Expression.Parameter(typeof(IServiceProvider), "serviceProvider"); |
| | 0 | 99 | | ParameterExpression instanceArg = Expression.Parameter(middleware, "middleware"); |
| | | 100 | | |
| | 0 | 101 | | var methodArguments = new Expression[parameters.Length]; |
| | 0 | 102 | | methodArguments[0] = connectionContextArg; |
| | 0 | 103 | | for (int i = 1; i < parameters.Length; i++) |
| | | 104 | | { |
| | 0 | 105 | | Type parameterType = parameters[i].ParameterType; |
| | 0 | 106 | | if (parameterType.IsByRef) |
| | | 107 | | { |
| | | 108 | | // TODO: String resources |
| | 0 | 109 | | throw new NotSupportedException( |
| | 0 | 110 | | $"Resources.FormatException_InvokeDoesNotSupportRefOrOutParams({InvokeAsyncMethodName})"); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | var parameterTypeExpression = new Expression[] |
| | 0 | 114 | | { |
| | 0 | 115 | | providerArg, Expression.Constant(parameterType, typeof(Type)), |
| | 0 | 116 | | Expression.Constant(methodInfo.DeclaringType, typeof(Type)) |
| | 0 | 117 | | }; |
| | | 118 | | |
| | 0 | 119 | | var getServiceInfo = typeof(QueueMiddlewareBuilderExtension).GetMethod(nameof(GetService), |
| | 0 | 120 | | BindingFlags.NonPublic | BindingFlags.Static); |
| | 0 | 121 | | if (getServiceInfo == null) |
| | | 122 | | { |
| | 0 | 123 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddlewareGetServiceMethodNotFoun |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | MethodCallExpression getServiceCall = Expression.Call(getServiceInfo, parameterTypeExpression); |
| | 0 | 127 | | methodArguments[i] = Expression.Convert(getServiceCall, parameterType); |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | Expression middlewareInstanceArg = instanceArg; |
| | 0 | 131 | | if (methodInfo.DeclaringType != typeof(T) && methodInfo.DeclaringType != null) |
| | | 132 | | { |
| | 0 | 133 | | middlewareInstanceArg = Expression.Convert(middlewareInstanceArg, methodInfo.DeclaringType); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | MethodCallExpression body = Expression.Call(middlewareInstanceArg, methodInfo, methodArguments); |
| | | 137 | | |
| | 0 | 138 | | var lambda = |
| | 0 | 139 | | Expression.Lambda<Func<T, QueueMessageContext, IServiceProvider, Task>>(body, instanceArg, |
| | 0 | 140 | | connectionContextArg, providerArg); |
| | | 141 | | |
| | 0 | 142 | | return lambda.Compile(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private static object GetService(IServiceProvider sp, Type type, Type middleware) |
| | | 146 | | { |
| | 0 | 147 | | object service = sp.GetService(type); |
| | 0 | 148 | | if (service == null) |
| | | 149 | | { |
| | | 150 | | // TODO: String resources |
| | 0 | 151 | | throw new InvalidOperationException( |
| | 0 | 152 | | $"Resources.FormatException_InvokeMiddlewareNoService({type}, {middleware})"); |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return service; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |