public static void CreateChannel(string address, Action <GrpcChannel, Metadata> action, Action <RpcException> exAction = null, Action <ChannelCustomerOptions> customerOptions = null, GrpcChannelOptions options = null) { var headers = new Metadata(); var cusOptions = new ChannelCustomerOptions(); if (customerOptions != null) { customerOptions(cusOptions); } var token = cusOptions.GetToken(); if (!string.IsNullOrWhiteSpace(token)) { headers.Add($"{AuthUtil.AUTH_KEY}", token.AddBearerToken()); } var eventId = cusOptions.GetEventId(); if (!string.IsNullOrWhiteSpace(eventId)) { headers.Add(App.EVENT_ID_KEY, eventId); } var channel = options == null?GrpcChannel.ForAddress(address) : GrpcChannel.ForAddress(address, options); ExecCallBusiness(address, cusOptions, channel, headers, eventId, action, exAction); }
/// <summary> /// 创建http客户端 /// </summary> /// <param name="customerOptions">自定义选项配置</param> /// <returns>http客户端</returns> public static HttpClient CreateHttpClient(Action <ChannelCustomerOptions> customerOptions = null) { var httpClient = new HttpClient(); var cusOptions = new ChannelCustomerOptions(); if (customerOptions != null) { customerOptions(cusOptions); } var token = cusOptions.GetToken(); if (!string.IsNullOrWhiteSpace(token)) { httpClient.DefaultRequestHeaders.Add($"{AuthUtil.AUTH_KEY}", token.AddBearerToken()); } var eventId = cusOptions.GetEventId(); if (!string.IsNullOrWhiteSpace(eventId)) { httpClient.DefaultRequestHeaders.Add(App.EVENT_ID_KEY, eventId); } return(httpClient); }
/// <summary> /// 请求请求JSON /// </summary> /// <param name="httpClient">http客户端</param> /// <param name="url">URL</param> /// <param name="method">方法</param> /// <param name="callbackRequest">回调请求</param> /// <param name="data">数据</param> /// <param name="customerOptions">自定义选项配置</param> /// <returns>返回字符串</returns> public static string RequestJson(this HttpClient httpClient, string url, string method, Func <HttpContent, Task <HttpResponseMessage> > callbackRequest, object data = null, Action <ChannelCustomerOptions> customerOptions = null) { HttpContent content = null; if (data != null) { string dataJson = data.ToJsonString(); if (string.IsNullOrWhiteSpace(dataJson)) { content = new StringContent(string.Empty); } else { content = new StringContent(dataJson, Encoding.UTF8); } } else { content = new StringContent(string.Empty); } content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); Task <HttpResponseMessage> task = null; httpClient.DefaultRequestHeaders.Add("Method", method); var cusOptions = new ChannelCustomerOptions(); if (customerOptions != null) { customerOptions(cusOptions); } var token = cusOptions.GetToken(); if (!string.IsNullOrWhiteSpace(token)) { httpClient.DefaultRequestHeaders.Add($"{AuthUtil.AUTH_KEY}", token.AddBearerToken()); } var eventId = cusOptions.GetEventId(); if (!string.IsNullOrWhiteSpace(eventId)) { content.Headers.Add(App.EVENT_ID_KEY, eventId); } Exception exce = null; var watch = new Stopwatch(); try { watch.Start(); task = callbackRequest(content); task.Wait(); watch.Stop(); } catch (Exception ex) { watch.Stop(); throw new Exception(ex.Message, ex); } finally { if (App.InfoEvent != null) { App.InfoEvent.RecordAsync($"http发起请求地址:{url}.方法:{method}.接口:{cusOptions.Api}.耗时:{watch.ElapsedMilliseconds}ms", exce, "RequestJson", eventId, url, cusOptions.Api); } } if (task.Result.StatusCode == System.Net.HttpStatusCode.OK) { var readTask = task.Result.Content.ReadAsStringAsync(); readTask.Wait(); return(readTask.Result); } else { throw new Exception(task.Result.StatusCode.ToString()); } }
/// <summary> /// 执行GRpc客户端 /// 需要在App.GetGRpcClient里设置获取GRpc客户端工厂的实现 /// </summary> /// <typeparam name="GRpcClientT">GRpc客户端类型</typeparam> /// <param name="client">客户端</param> /// <param name="action">回调业务处理方法</param> /// <param name="exAction">发生异常回调,如果为null,则不会捕获异常</param> /// <param name="customerOptions">自定义选项配置</param> /// <returns>GRpc客户端</returns> private static void ExecGRpcClient <GRpcClientT>(GRpcClientT client, Action <GRpcClientT, Metadata> action, Action <RpcException> exAction = null, Action <ChannelCustomerOptions> customerOptions = null) where GRpcClientT : ClientBase <GRpcClientT> { if (action == null) { return; } var headers = new Metadata(); var cusOptions = new ChannelCustomerOptions(); if (customerOptions != null) { customerOptions(cusOptions); } var token = cusOptions.GetToken(); if (!string.IsNullOrWhiteSpace(token)) { headers.Add($"{AuthUtil.AUTH_KEY}", token.AddBearerToken()); } var eventId = cusOptions.GetEventId(); if (!string.IsNullOrWhiteSpace(eventId)) { headers.Add(App.EVENT_ID_KEY, eventId); } RpcException rpcEx = null; Exception exce = null; Stopwatch watch = new Stopwatch(); try { watch.Start(); action(client, headers); watch.Stop(); } catch (RpcException ex) { watch.Stop(); exce = rpcEx = ex; } catch (Exception ex) { watch.Stop(); exce = ex; } if (App.InfoEvent != null) { App.InfoEvent.RecordAsync($"grpc发起请求.接口:{cusOptions.Api}.耗时:{watch.ElapsedMilliseconds}ms", exce, "GetGRpcClient", eventId, cusOptions.Api); } if (rpcEx != null && exAction != null) { exAction(rpcEx); } else if (exce != null) { throw new Exception(exce.Message, exce); } }