/// <summary> /// 给Service加入注释 /// </summary> /// <param name="proto"></param> /// <param name="sb"></param> public static void AddServiceComment(ProtoMethodInfo proto, StringBuilder sb) { var comment = string.Empty; var handler = MetaModel.Methods.FirstOrDefault(p => p.FullName == proto.FullName)?.Handler; if (handler != null) { var fullName = handler.Method.GetPropertyValue <string>("FullName", BindingFlags.Instance | BindingFlags.NonPublic); //将方法的FullName转换成注释的FullName var xmlFullName = fullName.Replace(" ", "").Replace("`1[", "{").Replace("]", "}"); comment = GetComment("M", xmlFullName); if (!string.IsNullOrWhiteSpace(comment)) { sb.AppendLine(AddComment(comment, " ")); } } }
/// <summary> /// 生成Grpc方法(CodeFirst方式) /// </summary> /// <typeparam name="TRequest"></typeparam> /// <typeparam name="TResponse"></typeparam> /// <param name="srv"></param> /// <param name="methodName"></param> /// <param name="package"></param> /// <param name="srvName"></param> /// <param name="mType"></param> /// <returns></returns> public static Method <TRequest, TResponse> BuildMethod <TRequest, TResponse>(this IGrpcService srv, string methodName, string package = null, string srvName = null, MethodType mType = MethodType.Unary) { var serviceName = srvName ?? GrpcExtensionsOptions.Instance.GlobalService ?? (srv.GetType().FullName.StartsWith("Castle.Proxies.") ? srv.GetType().Name.Substring(0, srv.GetType().Name.Length - 5) : srv.GetType().Name); var pkg = package ?? GrpcExtensionsOptions.Instance.GlobalPackage; if (!string.IsNullOrWhiteSpace(pkg)) { serviceName = $"{pkg}.{serviceName}"; } #region 为生成proto收集信息 if (!(srv is IGrpcBaseService) || GrpcExtensionsOptions.Instance.GenBaseServiceProtoEnable) { var protoMethodInfo = new ProtoMethodInfo { ServiceName = serviceName, MethodName = methodName, RequestName = typeof(TRequest).Name, ResponseName = typeof(TResponse).Name, MethodType = mType }; if (typeof(TRequest).IsGenericType) { protoMethodInfo.RequestName = $"{protoMethodInfo.RequestName.Substring(0, protoMethodInfo.RequestName.IndexOf('`'))}_{typeof(TRequest).GetGenericArguments()[0].Name}"; } if (typeof(TResponse).IsGenericType) { protoMethodInfo.ResponseName = $"{protoMethodInfo.ResponseName.Substring(0, protoMethodInfo.ResponseName.IndexOf('`'))}_{typeof(TResponse).GetGenericArguments()[0].Name}"; } ProtoInfo.Methods.Add(protoMethodInfo); ProtoGenerator.AddProto <TRequest>(protoMethodInfo.RequestName); ProtoGenerator.AddProto <TResponse>(protoMethodInfo.ResponseName); } #endregion var request = Marshallers.Create <TRequest>((arg) => ProtobufExtensions.Serialize <TRequest>(arg), data => ProtobufExtensions.Deserialize <TRequest>(data)); var response = Marshallers.Create <TResponse>((arg) => ProtobufExtensions.Serialize <TResponse>(arg), data => ProtobufExtensions.Deserialize <TResponse>(data)); return(new Method <TRequest, TResponse>(mType, serviceName, methodName, request, response)); }