private bool IsAsyncAction(ControllerActionDescriptor actionDescriptor) { Type attType = typeof(AsyncStateMachineAttribute); AsyncStateMachineAttribute attribute = actionDescriptor.MethodInfo.GetCustomAttribute(attType) as AsyncStateMachineAttribute; return(attribute != null); }
public void Run() { foreach (ChainedTestMethod chainedTestMethod in TestChain) { ScenarioLogger?.Log($"---> {chainedTestMethod.TestMethod.TestMethodType} {chainedTestMethod.TestCase}"); try { AsyncStateMachineAttribute asyncAttribute = chainedTestMethod.TestMethod.MethodInfo .GetCustomAttribute <AsyncStateMachineAttribute>(); if (asyncAttribute != null) { Task task = (Task)chainedTestMethod.TestMethod.MethodInfo .Invoke(null, chainedTestMethod.Parameters); task.Wait(); if (task.Exception != null) { throw new AggregateException(task.Exception); } } else { chainedTestMethod.TestMethod.MethodInfo.Invoke(null, chainedTestMethod.Parameters); } ScenarioLogger?.Log("Done"); } catch (Exception e) { ScenarioLogger?.Log(e.Message); ScenarioLogger?.Log(e.StackTrace); throw new AggregateException(e); } } }
/// <summary> /// Check the method body of the Initialize method of an analyzer and if that's empty, /// then the analyzer hasn't been implemented yet. /// </summary> private static bool HasImplementation(CodeFixProvider fixer) { MethodInfo method = fixer.GetType().GetTypeInfo().GetMethod("RegisterCodeFixesAsync"); AsyncStateMachineAttribute stateMachineAttr = method?.GetCustomAttribute <AsyncStateMachineAttribute>(); MethodInfo moveNextMethod = stateMachineAttr?.StateMachineType.GetTypeInfo().GetDeclaredMethod("MoveNext"); if (moveNextMethod != null) { MethodBody body = moveNextMethod.GetMethodBody(); int? ilInstructionCount = body?.GetILAsByteArray()?.Count(); return(ilInstructionCount != 177); } return(true); }
private void CompileAsyncMethod(MethodBase method, AsyncStateMachineAttribute asyncAttribute) { RuntimeHelpers.PrepareMethod(method.MethodHandle); InliningAnalyzerSource.Log.AsyncMethodStart(method.Name); var moveNextMethod = asyncAttribute.StateMachineType.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.NonPublic); if (moveNextMethod != null) { RuntimeHelpers.PrepareMethod(moveNextMethod.MethodHandle); } InliningAnalyzerSource.Log.AsyncMethodStop(method.Name); }
/// <summary> /// Check the method body of the Initialize method of an analyzer and if that's empty, /// then the analyzer hasn't been implemented yet. /// </summary> private static bool HasImplementation(CodeFixProvider fixer) { MethodInfo method = fixer.GetType().GetTypeInfo().GetMethod("RegisterCodeFixesAsync"); AsyncStateMachineAttribute stateMachineAttr = method?.GetCustomAttribute <AsyncStateMachineAttribute>(); MethodInfo moveNextMethod = stateMachineAttr?.StateMachineType.GetTypeInfo().GetDeclaredMethod("MoveNext"); if (moveNextMethod != null) { MethodBody body = moveNextMethod.GetMethodBody(); int? ilInstructionCount = body?.GetILAsByteArray()?.Count(); return(ilInstructionCount != 177); } // See if the method body is: // { // return Task.CompletedTask; // } byte[] methodBodyIL = method?.GetMethodBody()?.GetILAsByteArray(); if (methodBodyIL != null && methodBodyIL.Length == 6 && methodBodyIL[0] == 0x28 && // call <method> methodBodyIL[5] == 0x2a) // ret { if (!BitConverter.IsLittleEndian) { Array.Reverse <byte>(methodBodyIL, 1, sizeof(Int32)); } int metadataToken = BitConverter.ToInt32(methodBodyIL, 1); MethodBase calledMethod = method.Module.ResolveMethod(metadataToken); if (calledMethod != null && calledMethod.DeclaringType.FullName == "System.Threading.Tasks.Task" && calledMethod.Name == "get_CompletedTask") { return(false); } } return(true); }
// Token: 0x06001674 RID: 5748 RVA: 0x00052B28 File Offset: 0x00050D28 private static void ProcessMethod(MethodInfo methodInfo, Dictionary <string, MethodInfo> endMethodMap, Dictionary <string, ServiceMethodInfo> methodTable, List <Attribute> attributes) { OperationContractAttribute customAttribute = OwaServiceMethodMap.GetCustomAttribute <OperationContractAttribute>(attributes); if (customAttribute == null) { return; } WebInvokeAttribute customAttribute2 = OwaServiceMethodMap.GetCustomAttribute <WebInvokeAttribute>(attributes); JsonRequestFormatAttribute customAttribute3 = OwaServiceMethodMap.GetCustomAttribute <JsonRequestFormatAttribute>(attributes); WebGetAttribute customAttribute4 = OwaServiceMethodMap.GetCustomAttribute <WebGetAttribute>(attributes); OperationBehaviorAttribute customAttribute5 = OwaServiceMethodMap.GetCustomAttribute <OperationBehaviorAttribute>(attributes); JsonResponseOptionsAttribute customAttribute6 = OwaServiceMethodMap.GetCustomAttribute <JsonResponseOptionsAttribute>(attributes); JsonRequestWrapperTypeAttribute customAttribute7 = OwaServiceMethodMap.GetCustomAttribute <JsonRequestWrapperTypeAttribute>(attributes); AsyncStateMachineAttribute customAttribute8 = OwaServiceMethodMap.GetCustomAttribute <AsyncStateMachineAttribute>(attributes); bool flag = customAttribute != null && customAttribute.AsyncPattern; bool flag2 = customAttribute8 != null; bool flag3 = customAttribute5 == null || customAttribute5.AutoDisposeParameters; bool isResponseCacheable = customAttribute6 != null && customAttribute6.IsCacheable; WebMessageBodyStyle webMessageBodyStyle = (customAttribute2 != null) ? customAttribute2.BodyStyle : WebMessageBodyStyle.Bare; if (customAttribute2 != null) { WebMessageFormat requestFormat = customAttribute2.RequestFormat; } if (customAttribute2 != null) { WebMessageFormat responseFormat = customAttribute2.ResponseFormat; } JsonRequestFormat jsonRequestFormat = (customAttribute3 != null) ? customAttribute3.Format : JsonRequestFormat.Custom; bool isHttpGet = (customAttribute2 != null) ? customAttribute2.Method.Equals("GET", StringComparison.InvariantCultureIgnoreCase) : (customAttribute4 != null); string text = (customAttribute2 != null) ? customAttribute2.UriTemplate : ((customAttribute4 != null) ? customAttribute4.UriTemplate : null); UriTemplate uriTemplate = (!string.IsNullOrEmpty(text)) ? new UriTemplate(text) : null; bool flag4 = webMessageBodyStyle == WebMessageBodyStyle.WrappedRequest || webMessageBodyStyle == WebMessageBodyStyle.Wrapped; bool isWrappedResponse = webMessageBodyStyle == WebMessageBodyStyle.WrappedResponse || webMessageBodyStyle == WebMessageBodyStyle.Wrapped; WebMessageFormat webMethodRequestFormat = (customAttribute2 != null && customAttribute2.IsRequestFormatSetExplicitly) ? customAttribute2.RequestFormat : WebMessageFormat.Json; WebMessageFormat webMethodResponseFormat = (customAttribute2 != null && customAttribute2.IsResponseFormatSetExplicitly) ? customAttribute2.ResponseFormat : WebMessageFormat.Json; Type type = (customAttribute7 != null) ? customAttribute7.Type : null; string text2 = methodInfo.Name; MethodInfo beginMethod = null; MethodInfo methodInfo2 = null; MethodInfo syncMethod = null; MethodInfo genericAsyncTaskMethod = null; Type type2 = null; Type type3; if (text2.StartsWith("Begin", StringComparison.InvariantCultureIgnoreCase) && flag) { type3 = ((methodInfo.GetParameters().Length > 0) ? methodInfo.GetParameters()[0].ParameterType : null); beginMethod = methodInfo; text2 = text2.Substring("Begin".Length); string key = "End" + text2; if (endMethodMap.TryGetValue(key, out methodInfo2)) { type2 = methodInfo2.ReturnType; } } else { syncMethod = methodInfo; type3 = ((methodInfo.GetParameters().Length > 0) ? methodInfo.GetParameters()[0].ParameterType : null); type2 = methodInfo.ReturnType; if (flag2 && type2 != null && type2.GenericTypeArguments != null && type2.GenericTypeArguments.Length > 0) { genericAsyncTaskMethod = OwaServiceMethodMap.handleAsyncMethodInfo.MakeGenericMethod(type2.GenericTypeArguments); type2 = type2.GenericTypeArguments[0]; } } bool isStreamedResponse = OwaServiceMethodMap.IsStreamResponse(type2); bool shouldAutoDisposeResponse = flag3 && OwaServiceMethodMap.ImplementsInterface <IDisposable>(type2); bool shouldAutoDisposeRequest = flag3 && OwaServiceMethodMap.ImplementsInterface <IDisposable>(type3); if (flag4 && type == null) { string wrappedRequestTypeName = OwaServiceMethodMap.GetWrappedRequestTypeName(text2); type = OwaServiceMethodMap.thisAssembly.GetType(wrappedRequestTypeName, false); } ServiceMethodInfo value = new ServiceMethodInfo { BeginMethod = beginMethod, EndMethod = methodInfo2, GenericAsyncTaskMethod = genericAsyncTaskMethod, IsAsyncAwait = flag2, IsAsyncPattern = flag, IsHttpGet = isHttpGet, IsResponseCacheable = isResponseCacheable, IsStreamedResponse = isStreamedResponse, IsWrappedRequest = flag4, IsWrappedResponse = isWrappedResponse, JsonRequestFormat = jsonRequestFormat, Name = text2, RequestType = type3, ResponseType = type2, ShouldAutoDisposeRequest = shouldAutoDisposeRequest, ShouldAutoDisposeResponse = shouldAutoDisposeResponse, SyncMethod = syncMethod, UriTemplate = uriTemplate, WebMethodRequestFormat = webMethodRequestFormat, WebMethodResponseFormat = webMethodResponseFormat, WrappedRequestType = type, WrappedRequestTypeParameterMap = OwaServiceMethodMap.BuildParameterMap(type) }; methodTable.Add(text2, value); }
public static MethodInfo GetLocalFunction(Type type, MethodInfo method, string localMemberName, Type[] localFunctionGenericTypes, string originalMethodName = null) { MethodInfo localMethod = null; #if !PORTABLE AsyncStateMachineAttribute asyncStateMachineAttribute = method.CustomAttributes.Select( attributeData => { AsyncStateMachineAttribute attribute = null; if (attributeData.AttributeType == typeof(AsyncStateMachineAttribute)) { attribute = (AsyncStateMachineAttribute)method.GetCustomAttribute(attributeData.AttributeType); } return(attribute); }).FirstOrDefault(); if (asyncStateMachineAttribute != null) { var asyncMethodName = method.Name; // check for existing async state machine method "MoveNext" method = asyncStateMachineAttribute.StateMachineType.GetMethod("MoveNext", BindingFlags.NonPublic | BindingFlags.Instance); if (method != null) { // search for local method inside "MoveNext" localMethod = GetLocalFunction(type, method, localMemberName, localFunctionGenericTypes, asyncMethodName); } } else { if (type.IsProxy()) { type = type.BaseType; } string expectedName = String.Format("<{0}>g__{1}|", string.IsNullOrEmpty(originalMethodName) ? method.Name : originalMethodName, localMemberName); MethodBody body = method.GetMethodBody(); var disasembledBody = MethodBodyDisassembler.DisassembleMethodInfo(method); var callInstructions = disasembledBody.Where(instr => instr.OpCode == System.Reflection.Emit.OpCodes.Call).ToArray(); foreach (var instruction in callInstructions) { MethodBase methodBase = null; try { methodBase = type.Module.ResolveMethod(instruction.Operand.Int); } catch (Exception) { //The exception is captured when the metadata token refers generic method. //Do not handle the exception since there is no way to know in advance if the metadata token refers non-generic or generic method. } if (methodBase == null && localFunctionGenericTypes != null) { try { methodBase = type.Module.ResolveMethod(instruction.Operand.Int, null, localFunctionGenericTypes); //This additional check is required because if we have function<T1,T2> and pass localFunctionGenericTypes weith <T1,T2,T3> //the method ResolveMethod returns function<T1,T2> even if the typenames are less than those in the provided array. if (methodBase != null) { Type[] genericArgDefinition = methodBase.GetGenericArguments(); if (genericArgDefinition.Length != localFunctionGenericTypes.Length) { methodBase = null; } } } catch (Exception) { //The exception is captured when the metadata token refers non-generic method. //Do not handle the exception since there is no way to know in advance if the metadata token refers non-generic or generic method. } } if (methodBase != null && methodBase.DeclaringType == type && methodBase.Name.StartsWith(expectedName)) { localMethod = methodBase as MethodInfo; break; } } if (localMethod == null) { if (localFunctionGenericTypes == null) { throw new MissingMemberException(BuildMissingLocalFunctionMessage(type, method, localMemberName)); } else { int methodGenericArgCount = method.GetGenericArguments().Length; List <Type> userFriendlyLocalFunctionTypes = new List <Type>(); for (int index = methodGenericArgCount; index < localFunctionGenericTypes.Length; index++) { userFriendlyLocalFunctionTypes.Add(localFunctionGenericTypes[index]); } string userFriendlyName = String.Join(".", method.ToString(), localMemberName); throw new MissingMemberException(BuildGenericMethodNonMatchingTypesMessage(type, userFriendlyName, userFriendlyLocalFunctionTypes.ToArray())); } } } #endif return(localMethod); }