| | | 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 CoreWCF.Channels.Framing; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Configuration |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Extension methods for adding typed middleware to a <see cref="IFramingConnectionHandshakeBuilder"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class UseMiddlewareFramingConnectionHandshakeExtensions |
| | | 18 | | { |
| | | 19 | | internal const string OnConnectedAsyncMethodName = "OnConnectedAsync"; |
| | | 20 | | |
| | 0 | 21 | | private static readonly MethodInfo s_getServiceInfo = typeof(UseMiddlewareFramingConnectionHandshakeExtensions). |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Adds a middleware type to the connection handshake pipeline. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <typeparam name="TMiddleware">The middleware type.</typeparam> |
| | | 27 | | /// <param name="app">The <see cref="IFramingConnectionHandshakeBuilder"/> instance.</param> |
| | | 28 | | /// <param name="args">The arguments to pass to the middleware type instance's constructor.</param> |
| | | 29 | | /// <returns>The <see cref="IFramingConnectionHandshakeBuilder"/> instance.</returns> |
| | | 30 | | public static IFramingConnectionHandshakeBuilder UseMiddleware<TMiddleware>(this IFramingConnectionHandshakeBuil |
| | | 31 | | { |
| | 581 | 32 | | return app.UseMiddleware(typeof(TMiddleware), args); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Adds a middleware type to the connection handshake pipeline. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="app">The <see cref="IFramingConnectionHandshakeBuilder"/> instance.</param> |
| | | 39 | | /// <param name="middleware">The middleware type.</param> |
| | | 40 | | /// <param name="args">The arguments to pass to the middleware type instance's constructor.</param> |
| | | 41 | | /// <returns>The <see cref="IFramingConnectionHandshakeBuilder"/> instance.</returns> |
| | | 42 | | public static IFramingConnectionHandshakeBuilder UseMiddleware(this IFramingConnectionHandshakeBuilder app, Type |
| | | 43 | | { |
| | 581 | 44 | | IServiceProvider handshakeServices = app.HandshakeServices; |
| | 581 | 45 | | return app.Use(next => |
| | 581 | 46 | | { |
| | 581 | 47 | | MethodInfo[] methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public); |
| | 581 | 48 | | MethodInfo[] invokeMethods = methods.Where(m => |
| | 3154 | 49 | | string.Equals(m.Name, OnConnectedAsyncMethodName, StringComparison.Ordinal) |
| | 581 | 50 | | ).ToArray(); |
| | 581 | 51 | | |
| | 581 | 52 | | if (invokeMethods.Length > 1) |
| | 581 | 53 | | { |
| | 581 | 54 | | // TODO: String resources |
| | 0 | 55 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddleMultipleInvokes({OnConnecte |
| | 581 | 56 | | } |
| | 581 | 57 | | |
| | 581 | 58 | | if (invokeMethods.Length == 0) |
| | 581 | 59 | | { |
| | 581 | 60 | | // TODO: String resources |
| | 0 | 61 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddlewareNoInvokeMethod({OnConne |
| | 581 | 62 | | } |
| | 581 | 63 | | |
| | 581 | 64 | | MethodInfo methodInfo = invokeMethods[0]; |
| | 581 | 65 | | if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType)) |
| | 581 | 66 | | { |
| | 581 | 67 | | // TODO: String resources |
| | 0 | 68 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddlewareNonTaskReturnType({OnCo |
| | 581 | 69 | | } |
| | 581 | 70 | | |
| | 581 | 71 | | ParameterInfo[] parameters = methodInfo.GetParameters(); |
| | 581 | 72 | | if (parameters.Length == 0 || parameters[0].ParameterType != typeof(FramingConnection)) |
| | 581 | 73 | | { |
| | 581 | 74 | | // TODO: String resources |
| | 0 | 75 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddlewareNoParameters({OnConnect |
| | 581 | 76 | | } |
| | 581 | 77 | | |
| | 581 | 78 | | object[] ctorArgs = new object[args.Length + 1]; |
| | 581 | 79 | | ctorArgs[0] = next; |
| | 581 | 80 | | Array.Copy(args, 0, ctorArgs, 1, args.Length); |
| | 581 | 81 | | object instance = ActivatorUtilities.CreateInstance(app.HandshakeServices, middleware, ctorArgs); |
| | 581 | 82 | | if (parameters.Length == 1) |
| | 581 | 83 | | { |
| | 581 | 84 | | return (HandshakeDelegate)methodInfo.CreateDelegate(typeof(HandshakeDelegate), instance); |
| | 581 | 85 | | } |
| | 581 | 86 | | |
| | 0 | 87 | | Func<object, FramingConnection, IServiceProvider, Task> factory = Compile<object>(methodInfo, parameters |
| | 581 | 88 | | |
| | 0 | 89 | | return context => |
| | 0 | 90 | | { |
| | 0 | 91 | | IServiceProvider serviceProvider = handshakeServices; |
| | 0 | 92 | | if (serviceProvider == null) |
| | 0 | 93 | | { |
| | 0 | 94 | | // TODO: String resources |
| | 0 | 95 | | throw new InvalidOperationException($"Resources.FormatException_UseMiddlewareIServiceProviderNot |
| | 0 | 96 | | } |
| | 0 | 97 | | |
| | 0 | 98 | | return factory(instance, context, serviceProvider); |
| | 0 | 99 | | }; |
| | 581 | 100 | | }); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private static Func<T, FramingConnection, IServiceProvider, Task> Compile<T>(MethodInfo methodInfo, ParameterInf |
| | | 104 | | { |
| | | 105 | | // If we call something like |
| | | 106 | | // |
| | | 107 | | // public class Middleware |
| | | 108 | | // { |
| | | 109 | | // public Task Invoke(ConnectionContext context, ILoggerFactory loggerFactory) |
| | | 110 | | // { |
| | | 111 | | // |
| | | 112 | | // } |
| | | 113 | | // } |
| | | 114 | | // |
| | | 115 | | |
| | | 116 | | // We'll end up with something like this: |
| | | 117 | | // Generic version: |
| | | 118 | | // |
| | | 119 | | // Task Invoke(Middleware instance, ConnectionContext httpContext, IServiceProvider provider) |
| | | 120 | | // { |
| | | 121 | | // return instance.Invoke(httpContext, (ILoggerFactory)UseMiddlewareConnectionHandshakeExtensions.GetSe |
| | | 122 | | // } |
| | | 123 | | |
| | | 124 | | // Non generic version: |
| | | 125 | | // |
| | | 126 | | // Task Invoke(object instance, ConnectionContext httpContext, IServiceProvider provider) |
| | | 127 | | // { |
| | | 128 | | // return ((Middleware)instance).Invoke(httpContext, (ILoggerFactory)UseMiddlewareConnectionHandshakeEx |
| | | 129 | | // } |
| | | 130 | | |
| | 0 | 131 | | Type middleware = typeof(T); |
| | | 132 | | |
| | 0 | 133 | | ParameterExpression connectionContextArg = Expression.Parameter(typeof(FramingConnection), "connectionContex |
| | 0 | 134 | | ParameterExpression providerArg = Expression.Parameter(typeof(IServiceProvider), "serviceProvider"); |
| | 0 | 135 | | ParameterExpression instanceArg = Expression.Parameter(middleware, "middleware"); |
| | | 136 | | |
| | 0 | 137 | | var methodArguments = new Expression[parameters.Length]; |
| | 0 | 138 | | methodArguments[0] = connectionContextArg; |
| | 0 | 139 | | for (int i = 1; i < parameters.Length; i++) |
| | | 140 | | { |
| | 0 | 141 | | Type parameterType = parameters[i].ParameterType; |
| | 0 | 142 | | if (parameterType.IsByRef) |
| | | 143 | | { |
| | | 144 | | // TODO: String resources |
| | 0 | 145 | | throw new NotSupportedException($"Resources.FormatException_InvokeDoesNotSupportRefOrOutParams({OnCo |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | var parameterTypeExpression = new Expression[] |
| | 0 | 149 | | { |
| | 0 | 150 | | providerArg, |
| | 0 | 151 | | Expression.Constant(parameterType, typeof(Type)), |
| | 0 | 152 | | Expression.Constant(methodInfo.DeclaringType, typeof(Type)) |
| | 0 | 153 | | }; |
| | | 154 | | |
| | 0 | 155 | | MethodCallExpression getServiceCall = Expression.Call(s_getServiceInfo, parameterTypeExpression); |
| | 0 | 156 | | methodArguments[i] = Expression.Convert(getServiceCall, parameterType); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | Expression middlewareInstanceArg = instanceArg; |
| | 0 | 160 | | if (methodInfo.DeclaringType != typeof(T)) |
| | | 161 | | { |
| | 0 | 162 | | middlewareInstanceArg = Expression.Convert(middlewareInstanceArg, methodInfo.DeclaringType); |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | MethodCallExpression body = Expression.Call(middlewareInstanceArg, methodInfo, methodArguments); |
| | | 166 | | |
| | 0 | 167 | | var lambda = Expression.Lambda<Func<T, FramingConnection, IServiceProvider, Task>>(body, instanceArg, connec |
| | | 168 | | |
| | 0 | 169 | | return lambda.Compile(); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | private static object GetService(IServiceProvider sp, Type type, Type middleware) |
| | | 173 | | { |
| | 0 | 174 | | object service = sp.GetService(type); |
| | 0 | 175 | | if (service == null) |
| | | 176 | | { |
| | | 177 | | // TODO: String resources |
| | 0 | 178 | | throw new InvalidOperationException($"Resources.FormatException_InvokeMiddlewareNoService({type}, {middl |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | return service; |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |