示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="method"></param>
        /// <param name="headers"></param>
        /// <param name="requestObject"></param>
        /// <returns></returns>
        public Task <object> CallGrpcAsync(GrpcMethodHandlerInfo method, IDictionary <string, string> headers, object requestObject)
        {
            object requests;

            if (requestObject != null && typeof(IEnumerable <>).MakeGenericType(method.RequestType).IsAssignableFrom(requestObject.GetType()))
            {
                requests = requestObject;
            }
            else
            {
                Array ary = Array.CreateInstance(method.RequestType, 1);
                ary.SetValue(requestObject, 0);
                requests = ary;
            }

            System.Reflection.MethodInfo m = typeof(GatewayMiddleware).GetMethod("CallGrpcAsyncCore", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            Task <object> task = (Task <object>)m.MakeGenericMethod(new Type[] { method.RequestType, method.ResponseType }).Invoke(this, new object[] { method, headers, requests });

            return(task);
        }
示例#2
0
        private Task <object> CallGrpcAsyncCore <TRequest, TResponse>(GrpcMethodHandlerInfo method, IDictionary <string, string> headers, IEnumerable <TRequest> requests) where TRequest : class where TResponse : class
        {
            Channel     channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            CallInvoker invoker = new MiddlewareCallInvoker(channel, MiddlewarePipeline);

            CallOptions option = CreateCallOptions(headers);

            Method <TRequest, TResponse> rpc = (Method <TRequest, TResponse>)method.Method;

            switch (rpc.Type)
            {
            case MethodType.Unary:

                Task <TResponse> taskUnary = AsyncUnaryCall(invoker, rpc, option, requests.FirstOrDefault());

                var s = taskUnary.Result;
                return(Task.FromResult <object>(taskUnary.Result));

            case MethodType.ClientStreaming:

                Task <TResponse> taskClientStreaming = AsyncClientStreamingCall(invoker, rpc, option, requests);

                return(Task.FromResult <object>(taskClientStreaming.Result));

            case MethodType.ServerStreaming:

                Task <IList <TResponse> > taskServerStreaming = AsyncServerStreamingCall(invoker, rpc, option, requests.FirstOrDefault());

                return(Task.FromResult <object>(taskServerStreaming.Result));

            case MethodType.DuplexStreaming:

                Task <IList <TResponse> > taskDuplexStreaming = AsyncDuplexStreamingCall(invoker, rpc, option, requests);

                return(Task.FromResult <object>(taskDuplexStreaming.Result));

            default:
                throw new NotSupportedException(string.Format("MethodType '{0}' is not supported.", rpc.Type));
            }
        }
示例#3
0
 private static Method <TRequest, TResponse> CreateMethodCore <TRequest, TResponse>(string serviceName, GrpcMethodHandlerInfo handler, IGrpcMarshallerFactory marshallerFactory)
 {
     return(new Method <TRequest, TResponse>(handler.MethodType, serviceName, handler.Handler.Name, marshallerFactory.GetMarshaller <TRequest>(), marshallerFactory.GetMarshaller <TResponse>()));
 }
示例#4
0
        public static IMethod CreateMethod(string serviceName, GrpcMethodHandlerInfo handler, IGrpcMarshallerFactory marshallerFactory)
        {
            MethodInfo m = typeof(GrpcReflection).GetMethod("CreateMethodCore", BindingFlags.Static | BindingFlags.NonPublic);

            return((IMethod)m.MakeGenericMethod(new Type[] { handler.RequestType, handler.ResponseType }).Invoke(null, new object[] { serviceName, handler, marshallerFactory }));
        }