예제 #1
0
        public static IServiceCollection AddKadderGrpcClient(
            this IServiceCollection services, Action <GrpcClientBuilder> builderAction)
        {
            var builder = new GrpcClientBuilder();

            builderAction(builder);

            var serviceCallBuilder = new GrpcServiceCallBuilder();

            services.AddSingleton(builder);
            services.AddSingleton(serviceCallBuilder);
            services.RegSerializer(null, builder.BinarySerializer);

            foreach (var interceptor in builder.Interceptors)
            {
                services.AddSingleton(interceptor);
            }

            foreach (var clientMetadata in builder.ClientMetadatas)
            {
                var grpcClient = new GrpcClient(clientMetadata, builder, serviceCallBuilder);
                foreach (var interceptor in clientMetadata.PrivateInterceptors)
                {
                    services.AddSingleton(interceptor);
                }
                foreach (var item in grpcClient.GrpcServiceDic)
                {
                    services.AddSingleton(item.Key, item.Value);
                }
            }

            return(services);
        }
예제 #2
0
        public static IServiceCollection AddKadderGrpcClient(
            this IServiceCollection services,
            Action <GrpcClientBuilder> builderAction)
        {
            var builder = new GrpcClientBuilder();

            builderAction(builder);
            services.AddSingleton <GrpcClientBuilder>();
            services.AddSingleton <GrpcServiceCallBuilder>();
            services.RegSerializer(null, builder.BinarySerializer);

            var provider           = services.BuildServiceProvider();
            var serviceCallBuilder = provider.GetService <GrpcServiceCallBuilder>();
            var binarySerializer   = provider.GetService <IBinarySerializer>();

            foreach (var options in builder.ClientOptions)
            {
                var grpcClient = new GrpcClient(options, serviceCallBuilder, binarySerializer);
                foreach (var item in grpcClient.GrpcServiceDic)
                {
                    services.AddSingleton(item.Key, item.Value);
                }
            }

            return(services);
        }
예제 #3
0
        public IDictionary <Type, string> GenerateHandler(
            GrpcOptions options,
            GrpcClient client,
            ref CodeBuilder codeBuilder)
        {
            var types = RefelectionHelper.GetImplInterfaceTypes(
                typeof(IMessagingServicer), true, options.GetScanAssemblies());
            var grpcServiceDic = new Dictionary <Type, string>();

            foreach (var typeService in types)
            {
                var className       = typeService.Name.Remove(0, 1);
                var classDescripter = new ClassDescripter(
                    className,
                    codeBuilder.Namespace)
                                      .SetBaseType(typeService.Name)
                                      .AddUsing("System.Threading.Tasks", typeService.Namespace)
                                      .AddUsing("Kadder")
                                      .SetAccess(AccessType.Public);
                grpcServiceDic.Add(typeService, $"{codeBuilder.Namespace}.{className}");

                var baseInterfaces = typeService.GetInterfaces();
                foreach (var method in typeService.GetMethods())
                {
                    var notGrpcMethodCount = method.CustomAttributes
                                             .Count(p =>
                                                    p.AttributeType == typeof(NotGrpcMethodAttribute));
                    if (notGrpcMethodCount > 0)
                    {
                        continue;
                    }

                    var parameters = method.GetParameters();
                    if (parameters.Length != 1)
                    {
                        continue;
                    }

                    var requestName  = parameters[0].ParameterType.Name.ToLower();
                    var responseType = GetMethodReturn(method.ReturnType);
                    var methodName   = method.Name.Replace("Async", "");
                    classDescripter.CreateMember(
                        new MethodDescripter(method.Name, true)
                        .SetAccess(AccessType.Public)
                        .SetReturn($"Task<{responseType.Name}>")
                        .SetParams(
                            new ParameterDescripter(
                                parameters[0].ParameterType.Name, requestName))
                        .AppendCode($@"var client=GrpcClientExtension.ClientDic[""{client.ID.ToString()}""];")
                        .AppendCode($@"return await client.CallAsync<{parameters[0].ParameterType.Name},{responseType.Name}>({requestName}, ""{methodName}"");"))
                    .AddUsing(responseType.Namespace)
                    .AddUsing(parameters[0].ParameterType.Namespace);
                }
                codeBuilder.CreateClass(classDescripter)
                .AddAssemblyRefence(typeService.Assembly.Location);
            }
            codeBuilder.AddAssemblyRefence(this.GetType().Assembly.Location);
            return(grpcServiceDic);
        }
        public IDictionary <Type, string> GenerateHandler(
            GrpcOptions options, GrpcClient client, ref CodeBuilder codeBuilder)
        {
            var types          = options.GetKServicers();
            var grpcServiceDic = new Dictionary <Type, string>();

            foreach (var typeService in types)
            {
                var className       = $"{typeService.Name}GrpcService";
                var classDescripter = new ClassDescripter(className, codeBuilder.Namespace)
                                      .SetBaseType(typeService.Name)
                                      .AddUsing("System.Threading.Tasks", typeService.Namespace)
                                      .AddUsing("Kadder")
                                      .SetAccess(AccessType.Public);
                grpcServiceDic.Add(typeService, $"{codeBuilder.Namespace}.{className}");

                var baseInterfaces = typeService.GetInterfaces();
                foreach (var method in typeService.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    var notGrpcMethodCount = method.CustomAttributes.Count(
                        p => p.AttributeType == typeof(NotGrpcMethodAttribute));
                    if (notGrpcMethodCount > 0)
                    {
                        continue;
                    }

                    var parameters = RpcParameterInfo.Convert(method.GetParameters());
                    if (parameters.Count == 0)
                    {
                        var emptyMessageType = typeof(EmptyMessage);
                        parameters.Add(new RpcParameterInfo()
                        {
                            Name          = emptyMessageType.Name,
                            ParameterType = emptyMessageType,
                            IsEmpty       = true
                        });
                    }

                    var requestName    = parameters[0].ParameterType.Name.ToLower();
                    var responseType   = GetMethodReturn(method);
                    var returnTypeCode = $"new Task<{responseType.Name}>";
                    var returnCode     = "return ";
                    var requestCode    = requestName;
                    if (parameters[0].IsEmpty)
                    {
                        requestCode = "new EmptyMessage()";
                    }
                    if (responseType.IsEmpty)
                    {
                        returnTypeCode = $"new {method.ReturnType.Name}";
                        returnCode     = string.Empty;
                    }
                    var methodName       = method.Name.Replace("Async", "");
                    var methodDescripter = new MethodDescripter(method.Name, true)
                                           .SetAccess(AccessType.Public)
                                           .SetReturn(returnTypeCode)
                                           .AppendCode($@"var client = GrpcClientExtension.ClientDic[""{client.ID.ToString()}""];")
                                           .AppendCode($@"{returnCode}await client.CallAsync<{parameters[0].ParameterType.Name},{responseType.Name}>({requestCode}, ""{methodName}"", ""{typeService.Name}"");");
                    if (!parameters[0].IsEmpty)
                    {
                        methodDescripter.SetParams(new ParameterDescripter(parameters[0].ParameterType.Name, requestName));
                    }
                    classDescripter.CreateMember(methodDescripter)
                    .AddUsing(responseType.Namespace).AddUsing(parameters[0].ParameterType.Namespace);
                }
                codeBuilder.CreateClass(classDescripter)
                .AddAssemblyRefence(typeService.Assembly.Location);
            }
            codeBuilder.AddAssemblyRefence(this.GetType().Assembly.Location);
            return(grpcServiceDic);
        }