public ChaosInvocationResp ProcessInvocation(ChaosInvocation chaosInvocation) { try { var realImplementObject = _serviceResolver.GetService(chaosInvocation.InterfaceTypeFullName); var realImplementType = realImplementObject.GetType(); var realImplementInfo = ReflectionClass.Reflection(realImplementType); var mi = FindMethod(realImplementInfo, chaosInvocation); var requestParameters = chaosInvocation.Parameters; var args = requestParameters.DeserializeToArguments(_serializer, _typeFinder) .ToArray(); var returnValue = mi.Func(realImplementObject, args); var invocationReply = ToChaosInvocationResp(chaosInvocation, returnValue); return(invocationReply); } catch (Exception ex) { var invocationReply = ToChaosInvocationResp(chaosInvocation, (object)null); invocationReply.Exception = SerializeException.CreateFromException(ex); return(invocationReply); } }
public ChaosInvocation CreateChaosInvocation(Type implementType, MethodInfo invocationMethod, object[] invocationArguments) { var chaosInvocation = new ChaosInvocation() { InterfaceTypeFullName = implementType.FullName, MethodName = invocationMethod.Name, GenericTypes = GetGenericTypes(invocationMethod).ToArray(), Parameters = invocationMethod.GetChaosParameters(_serializer, invocationArguments) }; if (invocationMethod.ReturnType != typeof(void)) { chaosInvocation.ReturnTypeFullName = invocationMethod.ReturnType.FullName; if (invocationMethod.ReturnType.IsTaskType()) { chaosInvocation.IsReturnTask = true; var resultType = invocationMethod.ReturnType.GenericTypeArguments[0]; chaosInvocation.ReturnTypeFullName = resultType.FullName; } } return(chaosInvocation); }
public object Send(ChaosInvocation invocation) { var req = invocation.ToAnyProto(); var reply = _client.SendInvocation(req); var invocationResp = reply.ConvertTo <ChaosInvocationResp>(); if (invocationResp.DataTypeFullName == null) { return(null); } return(_chaosConverter.ToData(invocationResp)); }
private ChaosInvocationResp ToChaosInvocationResp(ChaosInvocation invocation, object returnValue) { var invocationReply = new ChaosInvocationResp() { DataTypeFullName = invocation.ReturnTypeFullName }; if (invocation.IsReturnTask) { var taskResult = returnValue.GetTaskResult(); invocationReply.Data = _serializer.Serialize(taskResult.value); } else { invocationReply.Data = _serializer.Serialize(returnValue); } return(invocationReply); }
private bool IsMatchMethod(MethodInfo methodInfo, ChaosInvocation request) { if (methodInfo.Name != request.MethodName) { return(false); } var parameterInfos = methodInfo.GetParameters(); if (parameterInfos.Length != request.Parameters.Count) { return(false); } var isAllSame = parameterInfos.Zip(request.Parameters.Select(x => _typeFinder.Find(x.ParameterType)), (x, y) => x.ParameterType == y) .All(x => x == true); return(isAllSame); }
public object Call(ChaosInvocation chaosInvocation) { var promise = new PromiseInvocation() { Invocation = chaosInvocation }; Publish(promise); if (!promise.Wait(_config.WaitTimeout)) { throw new TimeoutException(); } if (promise.Exception != null) { throw promise.Exception; } return(promise.Result); }
public object Send(ChaosInvocation invocation) { var data = _binarySerializer.Serialize(invocation); object resp = null; lock (_lock) { _singal = new AutoResetEvent(false); _client.Send(data); if (!_singal.WaitOne(_config.Value.WaitTimeout)) { throw new TimeoutException(); } resp = _chaosConverter.ToData(_reply); } return(resp); }
private (MethodInfo MethodInfo, Func <object, object[], object> Func) FindMethod(ReflectionClass clazz, ChaosInvocation request) { foreach (var method in clazz.AllMethods) { if (!IsMatchMethod(method.MethodInfo, request)) { continue; } return(method.MethodInfo, method.Func); } foreach (var method in clazz.AllGenericMethods) { if (!IsMatchMethod(method.MethodInfo, request)) { continue; } var genericTypes = request.GenericTypes .Select(x => _typeFinder.Find(x.ParameterType)) .ToArray(); return(method.MethodInfo, method.GetFunc(genericTypes)); } throw new EntryPointNotFoundException(request.MethodName); }