Ejemplo n.º 1
0
        private static void AddUnaryMethod <TRequest, TResponse>(
            IServiceMethodBinder <TService> binder,
            IMethod grpcMethod,
            MethodInfo contractMethodDefinition,
            IList <object> metadata,
            MethodInfo channelMethod,
            object channelInstance)
            where TRequest : class
            where TResponse : class
        {
            var method  = (Method <TRequest, TResponse>)grpcMethod;
            var handler = channelMethod.CreateDelegate <Func <TService, TRequest, ServerCallContext, Task <TResponse> > >(channelInstance);

            binder.AddUnaryMethod(
                method,
                () => contractMethodDefinition,
                metadata,
                handler);
        }
Ejemplo n.º 2
0
        private static void AddServerStreamingMethod <TRequest, TResponseHeader, TResponse>(
            IServiceMethodBinder <TService> binder,
            IMethod grpcMethod,
            MethodInfo contractMethodDefinition,
            object?responseHeaderMarshaller,
            IList <object> metadata,
            MethodInfo channelMethod,
            object channelInstance)
            where TRequest : class
            where TResponseHeader : class
        {
            var method  = (Method <TRequest, Message <TResponse> >)grpcMethod;
            var handler = channelMethod.CreateDelegate <Func <TService, TRequest, ServerCallContext, ValueTask <(TResponseHeader?, IAsyncEnumerable <TResponse>)> > >(channelInstance);

            binder.AddServerStreamingMethod(
                method,
                () => contractMethodDefinition,
                (Marshaller <TResponseHeader>?)responseHeaderMarshaller,
                metadata,
                handler);
        }
Ejemplo n.º 3
0
        public void Bind(IServiceMethodBinder <TService> binder)
        {
            var contract        = EmitContractBuilder.CreateFactory(_contractType)(binder.MarshallerFactory);
            var channelInstance = EmitServiceEndpointBuilder.CreateFactory(_channelType)();
            var serviceType     = typeof(TService);

            foreach (var interfaceDescription in _description.Services)
            {
                foreach (var operation in interfaceDescription.Operations)
                {
                    var message          = operation.Message;
                    var channelMethod    = _channelType.InstanceMethod(operation.OperationName);
                    var metadata         = TryGetMethodMetadata(interfaceDescription.InterfaceType, message.Operation);
                    var grpcMethodMethod = (IMethod)_contractType.InstanceFiled(operation.GrpcMethodName).GetValue(contract);

                    object?requestHeaderMarshaller = null;
                    if (message.HeaderRequestType != null)
                    {
                        requestHeaderMarshaller = _contractType.InstanceFiled(operation.GrpcMethodInputHeaderName).GetValue(contract);
                    }

                    object?responseHeaderMarshaller = null;
                    if (message.HeaderResponseType != null)
                    {
                        responseHeaderMarshaller = _contractType.InstanceFiled(operation.GrpcMethodOutputHeaderName).GetValue(contract);
                    }

                    _logger?.LogDebug("Bind service method {0}.{1}.", serviceType.FullName, message.Operation.Name);
                    if (grpcMethodMethod.Type == MethodType.Unary)
                    {
                        var addMethod = _serviceBinderAddUnaryMethod
                                        .MakeGenericMethod(message.RequestType, message.ResponseType)
                                        .CreateDelegate <Action <IServiceMethodBinder <TService>, IMethod, MethodInfo, IList <object>, MethodInfo, object> >();
                        addMethod(binder, grpcMethodMethod, message.Operation, metadata, channelMethod, channelInstance);
                    }
                    else if (grpcMethodMethod.Type == MethodType.ClientStreaming)
                    {
                        var addMethod = _serviceBinderAddClientStreamingMethod
                                        .MakeGenericMethod(
                            message.HeaderRequestType ?? typeof(Message),
                            message.RequestType.GenericTypeArguments[0],
                            message.ResponseType)
                                        .CreateDelegate <Action <IServiceMethodBinder <TService>, IMethod, MethodInfo, object?, IList <object>, MethodInfo, object> >();
                        addMethod(binder, grpcMethodMethod, message.Operation, requestHeaderMarshaller, metadata, channelMethod, channelInstance);
                    }
                    else if (grpcMethodMethod.Type == MethodType.ServerStreaming)
                    {
                        var addMethod = _serviceBinderAddServerStreamingMethod
                                        .MakeGenericMethod(
                            message.RequestType,
                            message.HeaderResponseType ?? typeof(Message),
                            message.ResponseType.GenericTypeArguments[0])
                                        .CreateDelegate <Action <IServiceMethodBinder <TService>, IMethod, MethodInfo, object?, IList <object>, MethodInfo, object> >();
                        addMethod(binder, grpcMethodMethod, message.Operation, responseHeaderMarshaller, metadata, channelMethod, channelInstance);
                    }
                    else if (grpcMethodMethod.Type == MethodType.DuplexStreaming)
                    {
                        var addMethod = _serviceBinderAddDuplexStreamingMethod
                                        .MakeGenericMethod(
                            message.HeaderRequestType ?? typeof(Message),
                            message.RequestType.GenericTypeArguments[0],
                            message.HeaderResponseType ?? typeof(Message),
                            message.ResponseType.GenericTypeArguments[0])
                                        .CreateDelegate <Action <IServiceMethodBinder <TService>, IMethod, MethodInfo, object?, object?, IList <object>, MethodInfo, object> >();
                        addMethod(binder, grpcMethodMethod, message.Operation, requestHeaderMarshaller, responseHeaderMarshaller, metadata, channelMethod, channelInstance);
                    }
                    else
                    {
                        throw new NotImplementedException("{0} operation is not implemented.".FormatWith(grpcMethodMethod.Type));
                    }
                }
            }
        }