public void Intercept(IInvocation invocation) { var parameters = new Dictionary <string, object>(); _logger?.LogDebug("invoke {0}", invocation.Method); //设置调用参数 var names = invocation.Method.GetParameters().Select(p => p.Name).ToArray(); for (var i = 0; i < invocation.Arguments.Length; i++) { parameters[names[i]] = invocation.Arguments[i]; } using (var env = new ServiceExecutionEnv(_loggerFactory, _selector)) { var desc = _typeDesc.GetActionOrTypeDesc(invocation.Method); invocation.ReturnValue = env.Execute(invocation.Method.ReturnType, desc, service => { var url = desc.GetUrl(service, invocation.Method); var ctx = new TypedCobRequestContext() { ServiceName = desc.ServiceName, TargetAddress = service.Address, Url = url, Parameters = parameters, ReturnType = invocation.Method.ReturnType, Method = invocation.Method }; //, Timeout = desc.Timeout return(_requestResolver.Get(desc.Transport).DoRequest(ctx, null)); }, invocation.Method, parameters); } }
public void Intercept(IInvocation invocation) { var parameters = new Dictionary <string, object>(); _logger?.LogDebug("invoke {0}", invocation.Method); //设置调用参数 var names = invocation.Method.GetParameters().Select(p => p.Name).ToArray(); for (var i = 0; i < invocation.Arguments.Length; i++) { parameters[names[i]] = invocation.Arguments[i]; } using (var env = new ServiceExecutionEnv(_loggerFactory, _selector, _requestOptions)) { var desc = _typeDesc.GetActionOrTypeDesc(invocation.Method); invocation.ReturnValue = env.Execute(invocation.Method.ReturnType, desc, async service => { //try //{ //} //catch (Exception ex) //{ // return Task.FromException<object>(ex.GetInnerException()); //} var url = desc.GetUrl(service, invocation.Method); var ctx = new TypedCobRequestContext() { ServiceName = desc.ServiceName, TargetAddress = service.Address, Url = url, Parameters = new Dictionary <string, object>(parameters), ReturnType = invocation.Method.ReturnType, Method = invocation.Method };//, Timeout = desc.Timeout //设置请求头 ctx.Extensions = new Dictionary <string, string>() { { CobMvcDefaults.HeaderUserAgent, $"{CobMvcDefaults.UserAgentValue}/{CobMvcDefaults.HeaderUserVersion}" }, { CobMvcDefaults.HeaderTraceID, _contextAccessor.Current.TraceID.ToString() }, { CobMvcDefaults.HeaderJump, (_contextAccessor.Current.Jump + 1).ToString() } }; if (desc.Filters != null) { desc.Filters.ForEach(f => f.OnBeforeRequest(ctx)); } return(await _requestResolver.Get(desc.Transport).DoRequest(ctx, null)); }, invocation.Method, parameters); } }
public T Invoke <T>(string action, Dictionary <string, object> parameters, object state) { using (var env = new ServiceExecutionEnv(_loggerFactory, _selector, _requestOptions)) { return((T)env.Execute(typeof(T), _desc, service => { var url = _desc.GetUrl(service, action); var ctx = new CobRequestContext { ServiceName = _desc.ServiceName, TargetAddress = service.Address, Parameters = parameters, ReturnType = typeof(T), Url = url }; //, Timeout = _desc.Timeout return _requestResolver.Get(_desc.Transport).DoRequest(ctx, state); })); } }