public static class A
{ private static readonly MethodInfo GetServiceInfo; public static IApplicationBuilder My_UseStaticFiles(this IApplicationBuilder app, StaticFileOptions options)
{
//检查参数
if (app == null)
{
throw new ArgumentNullException("app");
}
if (options == null)
{
throw new ArgumentNullException("options");
} var t = Microsoft.Extensions.Options.Options.Create<StaticFileOptions>(options); object[] args = new object[] {
t
};
//执行该方法 会自动注入 该类中所需要的对象 RequestDelegate next
//return builder.UseMiddleware<RequestCultureMiddleware>(); return app.UseMiddleware<StaticFileMiddleware>(args);
} public static IApplicationBuilder My_UseMiddleware<T>(this IApplicationBuilder app, params object[] args)
{
return app.My_UseMiddleware(typeof(T), args);
}
public static IApplicationBuilder My_UseMiddleware(this IApplicationBuilder app, Type middleware, params object[] args)
{
if (typeof(IMiddleware).GetTypeInfo().IsAssignableFrom(middleware.GetTypeInfo()))
{
if (args.Length != )
{
throw new NotSupportedException(My_Resources.FormatException_UseMiddlewareExplicitArgumentsNotSupported(typeof(IMiddleware)));
}
return My_UseMiddlewareInterface(app, middleware);
}
IServiceProvider applicationServices = app.ApplicationServices;
return app.Use
(
delegate (RequestDelegate next)
{
MethodInfo[] array = (from m in middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public)
where string.Equals(m.Name, "Invoke", StringComparison.Ordinal) || string.Equals(m.Name, "InvokeAsync", StringComparison.Ordinal)
select m).ToArray();
if (array.Length > )
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddleMutlipleInvokes("Invoke", "InvokeAsync"));
}
if (array.Length == )
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoInvokeMethod("Invoke", "InvokeAsync", middleware));
}
MethodInfo methodInfo = array[];
if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType))
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNonTaskReturnType("Invoke", "InvokeAsync", "Task"));
}
ParameterInfo[] parameters = methodInfo.GetParameters();
if (parameters.Length == || parameters[].ParameterType != typeof(HttpContext))
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoParameters("Invoke", "InvokeAsync", "HttpContext"));
}
object[] array2 = new object[args.Length + ];
array2[] = next;
Array.Copy(args, , array2, , args.Length);
object instance = ActivatorUtilities.CreateInstance(app.ApplicationServices, middleware, array2);
if (parameters.Length == )
{
return (RequestDelegate)methodInfo.CreateDelegate(typeof(RequestDelegate), instance);
}
Func<object, HttpContext, IServiceProvider, Task> factory = My_Compile<object>(methodInfo, parameters);
return delegate (HttpContext context)
{
IServiceProvider serviceProvider = context.RequestServices ?? applicationServices;
if (serviceProvider == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareIServiceProviderNotAvailable("IServiceProvider"));
}
return factory(instance, context, serviceProvider);
};
} );
} private static Func<T, HttpContext, IServiceProvider, Task> My_Compile<T>(MethodInfo methodInfo, ParameterInfo[] parameters)
{
ParameterExpression expression = Expression.Parameter((Type)typeof(HttpContext), "httpContext");
ParameterExpression expression2 = Expression.Parameter((Type)typeof(IServiceProvider), "serviceProvider");
ParameterExpression expression3 = Expression.Parameter((Type)typeof(T), "middleware");
Expression[] expressionArray = new Expression[] { expression };
for (int i = ; i < parameters.Length; i++)
{
Type type = parameters[i].ParameterType;
if (type.IsByRef)
{
throw new NotSupportedException(My_Resources.FormatException_InvokeDoesNotSupportRefOrOutParams("Invoke"));
}
Expression[] expressionArray2 = new Expression[] { (Expression)expression2, (Expression)Expression.Constant(type, (Type)typeof(Type)), (Expression)Expression.Constant(methodInfo.DeclaringType, (Type)typeof(Type)) };
expressionArray[i] = (Expression)Expression.Convert((Expression)Expression.Call(GetServiceInfo, expressionArray2), type);
}
Expression expression4 = (Expression)expression3;
if (methodInfo.DeclaringType != typeof(T))
{
expression4 = (Expression)Expression.Convert(expression4, methodInfo.DeclaringType);
}
ParameterExpression[] expressionArray3 = new ParameterExpression[] { expression3, expression, expression2 };
return Expression.Lambda<Func<T, HttpContext, IServiceProvider, Task>>((Expression)Expression.Call(expression4, methodInfo, expressionArray), expressionArray3).Compile();
} private static IApplicationBuilder My_UseMiddlewareInterface(IApplicationBuilder app, Type middlewareType)
{
return app.Use
(
(RequestDelegate next)
=>
async delegate
(HttpContext context)
{
IMiddlewareFactory middlewareFactory = (IMiddlewareFactory)context.RequestServices.GetService(typeof(IMiddlewareFactory));
if (middlewareFactory == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoMiddlewareFactory(typeof(IMiddlewareFactory)));
}
IMiddleware middleware = middlewareFactory.Create(middlewareType);
if (middleware == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareUnableToCreateMiddleware(middlewareFactory.GetType(), middlewareType));
}
try
{
await middleware.InvokeAsync(context, next);
}
finally
{
middlewareFactory.Release(middleware);
}
}
);
} } internal static class My_Resources
{
// Fields
private static readonly ResourceManager _resourceManager; internal static string FormatException_UseMiddlewareNoMiddlewareFactory(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoMiddlewareFactory"), p0);
} internal static string FormatException_UseMiddlewareUnableToCreateMiddleware(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareUnableToCreateMiddleware"), p0, p1);
} // Methods
static My_Resources()
{
_resourceManager = new ResourceManager(
"Microsoft.AspNetCore.Http.Abstractions.Resources",
IntrospectionExtensions.GetTypeInfo((Type)typeof(My_Resources)).Assembly);
}
private static string GetString(string name, params string[] formatterNames)
{
string str = _resourceManager.GetString(name);
if (formatterNames != null)
{
for (int i = ; i < formatterNames.Length; i++)
{
str = str.Replace("{" + formatterNames[i] + "}", "{" + ((int)i) + "}");
}
}
return str;
}
internal static string FormatException_UseMiddleMutlipleInvokes(object p0, object p1) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddleMutlipleInvokes", Array.Empty<string>()), p0, p1); internal static string FormatException_UseMiddlewareNoInvokeMethod(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoInvokeMethod", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareNonTaskReturnType(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNonTaskReturnType", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareNoParameters(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoParameters", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareIServiceProviderNotAvailable(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareIServiceProviderNotAvailable", Array.Empty<string>()), p0); internal static string FormatException_UseMiddlewareExplicitArgumentsNotSupported(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareExplicitArgumentsNotSupported", Array.Empty<string>()), p0); //--
internal static string FormatException_InvokeDoesNotSupportRefOrOutParams(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_InvokeDoesNotSupportRefOrOutParams", Array.Empty<string>()), p0);
}

最新文章

  1. Objective-C中的继承和多态
  2. 使用dynamic linq 解决自定义查询的若干弊端
  3. js location对象
  4. GDB中应该知道的几个调试方法 来自陈皓
  5. Openvz特点和分析
  6. Java中重写与重载的辨析
  7. php 在web端读出pdf 与各种文件下载
  8. 怎么样eclipse发达国家多重聚合关系maven项目和使用git管理
  9. listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例(转载:http://blog.chinaunix.net/uid-83572-id-5510.ht)
  10. (七十三)iOS本地推送通知的实现
  11. CDI Features
  12. 重启部署在阿里云上的huginn
  13. &lt;5&gt;Cocos Creator 脚本简介
  14. uname command
  15. 【webGL】
  16. 【LOJ】#6432. 「PKUSC2018」真实排名
  17. python语法(四)— 文件操作
  18. C# webBrowser 获取元素class属性值
  19. css四种选择器总结
  20. 第二次作业&lt;2&gt;

热门文章

  1. 题解 CF546C 【Soldier and Cards】
  2. QT 线程的使用(继承QThread)
  3. python学习-5 python基础-2 条件语句(if的简单用法2---elif)
  4. Python中的with语句(上下文管理协议)
  5. 第十章 MIZ702 ZYNQ制作UBOOT固化程序
  6. poj 2891 模数不互质的中国剩余定理
  7. hdu 1087最长上升子序列和问题
  8. 使ul中的li居中
  9. 【css】浅谈BFC
  10. 6. Java基本数据类型