/// <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().Name;
            var pkg = package ?? GrpcExtensionsOptions.Instance.GlobalPackage;

            if (!string.IsNullOrWhiteSpace(pkg))
            {
                serviceName = $"{pkg}.{serviceName}";
            }
            #region 为生成proto收集信息
            if (!(srv is IGrpcBaseService) || GrpcExtensionsOptions.Instance.GenBaseServiceProtoEnable)
            {
                ProtoInfo.Methods.Add(new ProtoMethodInfo
                {
                    ServiceName  = serviceName,
                    MethodName   = methodName,
                    RequestName  = typeof(TRequest).Name,
                    ResponseName = typeof(TResponse).Name,
                    MethodType   = mType
                });
                ProtoGenerator.AddProto <TRequest>(typeof(TRequest).Name);
                ProtoGenerator.AddProto <TResponse>(typeof(TResponse).Name);
            }
            #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));
        }
Example #2
0
 /// <summary>
 /// 添加proto
 /// </summary>
 internal static void AddProto <TEntity>(string entityName)
 {
     if (!ProtoMethodInfo.Protos.ContainsKey(entityName))
     {
         ProtoMethodInfo.Protos.TryAdd(entityName, ProtoGenerator.FilterHead(Serializer.GetProto <TEntity>(ProtoBuf.Meta.ProtoSyntax.Proto3)));
     }
 }
        /// <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));
        }
 /// <summary>
 /// CodeFirst生成proto文件
 /// </summary>
 /// <param name="dir"></param>
 /// <returns></returns>
 public ServerBuilder UseProtoGenerate(string dir)
 {
     ProtoGenerator.Gen(dir);
     return(this);
 }