private static void ReflectionBind <TService>(ServiceBinderBase binder, TService service) { var bindMethodInfo = BindMethodFinder.GetBindMethod(typeof(TService)); // Invoke BindService(ServiceBinderBase, BaseType) bindMethodInfo.Invoke(null, new object[] { binder, service }); }
private static void ReflectionBind <TService>(ServiceBinderBase binder, TService service) { var serviceType = typeof(TService); // TService is an implementation of the gRPC service. It ultimately derives from Foo.TServiceBase base class. // We need to access the static BindService method on Foo which implicitly derives from Object. var baseType = serviceType.BaseType; // Handle services that have multiple levels of inheritence while (baseType?.BaseType?.BaseType != null) { baseType = baseType.BaseType; } // We need to call Foo.BindService from the declaring type. var declaringType = baseType?.DeclaringType; // The method we want to call is public static void BindService(ServiceBinderBase, BaseType) var bindService = declaringType?.GetMethod("BindService", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(ServiceBinderBase), baseType }, Array.Empty <ParameterModifier>()); if (bindService == null) { throw new InvalidOperationException($"Cannot locate BindService(ServiceBinderBase, ServiceBase) method for the current service type: {serviceType.FullName}."); } // Invoke BindService(ServiceBinderBase, BaseType) bindService.Invoke(null, new object[] { binder, service }); }
public void Visit(ServiceBinderBase serviceBinder) { foreach (var addMethodAction in m_addMethodActions) { addMethodAction(serviceBinder); } }
/// <summary> /// 绑定GrpcService的方法 /// </summary> /// <param name="serviceBinder"></param> /// <param name="service"></param> public static void BindService(ServiceBinderBase serviceBinder, Type service) { if (typeof(IGrpcBaseService).IsAssignableFrom(service)) { AutoRegisterMethod(service, serviceBinder, ServerConsts.BaseServicePackage, ServerConsts.BaseServiceName); } else { AutoRegisterMethod(service, serviceBinder); } }
internal void BindHandler(ServiceBinderBase binder) { var method = new Method <byte[], byte[]>(this.MethodType, this.ServiceName, this.MethodName, MagicOnionMarshallers.ThroughMarshaller, MagicOnionMarshallers.ThroughMarshaller); switch (this.MethodType) { case MethodType.Unary: { var genericMethod = this.GetType() .GetMethod(nameof(UnaryServerMethod), BindingFlags.Instance | BindingFlags.NonPublic) ! .MakeGenericMethod(RequestType, UnwrappedResponseType); var handler = (UnaryServerMethod <byte[], byte[]>)genericMethod.CreateDelegate(typeof(UnaryServerMethod <byte[], byte[]>), this); binder.AddMethod(method, handler); } break; case MethodType.ClientStreaming: { var genericMethod = this.GetType() .GetMethod(nameof(ClientStreamingServerMethod), BindingFlags.Instance | BindingFlags.NonPublic) ! .MakeGenericMethod(RequestType, UnwrappedResponseType); var handler = (ClientStreamingServerMethod <byte[], byte[]>)genericMethod.CreateDelegate(typeof(ClientStreamingServerMethod <byte[], byte[]>), this); binder.AddMethod(method, handler); } break; case MethodType.ServerStreaming: { var genericMethod = this.GetType() .GetMethod(nameof(ServerStreamingServerMethod), BindingFlags.Instance | BindingFlags.NonPublic) ! .MakeGenericMethod(RequestType, UnwrappedResponseType); var handler = (ServerStreamingServerMethod <byte[], byte[]>)genericMethod.CreateDelegate(typeof(ServerStreamingServerMethod <byte[], byte[]>), this); binder.AddMethod(method, handler); } break; case MethodType.DuplexStreaming: { var genericMethod = this.GetType() .GetMethod(nameof(DuplexStreamingServerMethod), BindingFlags.Instance | BindingFlags.NonPublic) ! .MakeGenericMethod(RequestType, UnwrappedResponseType); var handler = (DuplexStreamingServerMethod <byte[], byte[]>)genericMethod.CreateDelegate(typeof(DuplexStreamingServerMethod <byte[], byte[]>), this); binder.AddMethod(method, handler); } break; default: throw new InvalidOperationException("Unknown RegisterType:" + this.MethodType); } }
// This method should grow as we find necessary to interact with other services. private void BindServices(ServiceBinderBase server, RemoteObjectStores stores, LldbMockFactories mockFactories) { var remoteFrameRpc = new RemoteFrameRpcServiceImpl(stores.Value, stores.Function, stores.Symbol, stores.Module, stores.Frame, stores.Thread); var remoteModuleRpc = new SbModuleRpcServiceImpl( stores.Module, stores.Address, stores.Section, mockFactories.FileSpec); var remoteThreadRpc = new RemoteThreadRpcServiceImpl( stores.Process, stores.Thread, stores.Frame, stores.Module); RemoteFrameRpcService.BindService(server, remoteFrameRpc); SbModuleRpcService.BindService(server, remoteModuleRpc); RemoteThreadRpcService.BindService(server, remoteThreadRpc); }
private static void ReflectionBind <TService>(ServiceBinderBase binder, TService service) { var baseType = GetServiceBaseType(typeof(TService)); // We need to call Foo.BindService from the declaring type. var declaringType = baseType?.DeclaringType; // The method we want to call is public static void BindService(ServiceBinderBase, BaseType) var bindService = declaringType?.GetMethod("BindService", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(ServiceBinderBase), baseType }, Array.Empty <ParameterModifier>()); if (bindService == null) { throw new InvalidOperationException($"Cannot locate BindService(ServiceBinderBase, ServiceBase) method for the current service type: {typeof(TService).FullName}."); } // Invoke BindService(ServiceBinderBase, BaseType) bindService.Invoke(null, new object[] { binder, service }); }
public static void BindMethod(ServiceBinderBase binder, ErrorService errorService) { throw new Exception("Error!"); }
public static void BindService(ServiceBinderBase serviceBinder, GreeterBase serviceImpl) { serviceBinder.AddMethod(__Method_SayHello, (UnaryServerMethod <global::Greet.HelloRequest, global::Greet.HelloReply>)null !); serviceBinder.AddMethod(__Method_SayHellos, (ServerStreamingServerMethod <global::Greet.HelloRequest, global::Greet.HelloReply>)null !); }
/// <summary> /// 自动注册服务方法 /// </summary> /// <param name="srv"></param> /// <param name="serviceBinder"></param> /// <param name="package"></param> /// <param name="serviceName"></param> public static void AutoRegisterMethod(Type srv, ServiceBinderBase serviceBinder, string package = null, string serviceName = null) { var methods = srv.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (var method in methods) { if (!method.ReturnType.Name.StartsWith("Task")) { continue; } var parameters = method.GetParameters(); if (parameters[parameters.Length - 1].ParameterType != typeof(ServerCallContext) || method.CustomAttributes.Any(x => x.AttributeType == typeof(NotGrpcMethodAttribute))) { continue; } Type inputType = parameters[0].ParameterType; Type inputType2 = parameters[1].ParameterType; Type outputType = method.ReturnType.IsGenericType ? method.ReturnType.GenericTypeArguments[0] : method.ReturnType; var addMethod = unaryAddMethod; var serverMethodType = typeof(UnaryServerMethod <,>); var methodType = MethodType.Unary; var reallyInputType = inputType; var reallyOutputType = outputType; //非一元方法 if ((inputType.IsGenericType || inputType2.IsGenericType)) { if (inputType.Name == "IAsyncStreamReader`1") { reallyInputType = inputType.GenericTypeArguments[0]; if (inputType2.Name == "IServerStreamWriter`1")//双向流 { addMethod = duplexStreamingAddMethod; methodType = MethodType.DuplexStreaming; serverMethodType = typeof(DuplexStreamingServerMethod <,>); reallyOutputType = inputType2.GenericTypeArguments[0]; } else//客户端流 { addMethod = clientStreamingAddMethod; methodType = MethodType.ClientStreaming; serverMethodType = typeof(ClientStreamingServerMethod <,>); } } else if (inputType2.Name == "IServerStreamWriter`1")//服务端流 { addMethod = serverStreamingAddMethod; methodType = MethodType.ServerStreaming; serverMethodType = typeof(ServerStreamingServerMethod <,>); reallyOutputType = inputType2.GenericTypeArguments[0]; } } var buildMethodResult = buildMethod.MakeGenericMethod(reallyInputType, reallyOutputType) .Invoke(null, new object[] { srv, method.Name, package, serviceName, methodType }); Delegate serverMethodDelegate = method.CreateDelegate(serverMethodType .MakeGenericType(reallyInputType, reallyOutputType), null); addMethod.MakeGenericMethod(reallyInputType, reallyOutputType).Invoke(serviceBinder, new[] { buildMethodResult, serverMethodDelegate }); } }
public static void BindMethod(ServiceBinderBase binder, MagicOnionGlueService service) { // no-op at here. // The MagicOnion service methods are bound by `MagicOnionGlueServiceMethodProvider<TService>` }