/// <summary> /// Construct a method body reader given a method stub. /// </summary> /// <param name="method">Method stub</param> /// <param name="logger">Logger</param> public VirtualizedMethodBodyReader(MethodStub method, ILogger logger) : base((method != null ? method.Parent : null)) { if (method == null) throw new ArgumentNullException(); this.Method = method; this.Logger = (logger != null ? logger : DummyLogger.NoThrowInstance); this.Initialize(); }
public void TestEqualsAllSame() { var first = this.SampleAssembly .FindTypes() .Single(t => t.Name == "SimpleClass") .FindMethods() .Single(m => m.Name == "SimpleMethod"); var second = new MethodStub("SimpleMethod"); Assert.IsTrue(first.Equals(second)); Assert.IsTrue(second.Equals(first)); }
/// <summary> /// Construct a method body reader given a method stub. /// </summary> /// <param name="method">Method stub</param> /// <param name="logger">Logger</param> public VirtualizedMethodBodyReader(MethodStub method, ILogger logger) : base((method != null ? method.Parent : null)) { if (method == null) { throw new ArgumentNullException(); } this.Method = method; this.Logger = (logger != null ? logger : DummyLogger.NoThrowInstance); this.Initialize(); }
private bool AddMethod <TService, TRequest, TResponse>( string serviceName, string operationName, MethodInfo method, MethodType methodType, object state, Func <MethodInfo, Expression[], Expression>?invoker, MarshallerCache marshallerCache, ConstantExpression?service) where TService : class where TRequest : class where TResponse : class { var grpcMethod = new Method <TRequest, TResponse>(methodType, serviceName, operationName, marshallerCache.GetMarshaller <TRequest>(), marshallerCache.GetMarshaller <TResponse>()); var stub = new MethodStub <TService>(invoker, method, service); return(TryBind <TService, TRequest, TResponse>(state, grpcMethod, stub)); }
private bool AddMethod <TService, TRequest, TResponse>( string serviceName, string operationName, MethodInfo method, MethodType methodType, ServiceBindContext bindContext, Func <MethodInfo, Expression[], Expression>?invoker, MarshallerCache marshallerCache, ConstantExpression?service) where TService : class where TRequest : class where TResponse : class { var grpcMethod = new Method <TRequest, TResponse>(methodType, serviceName, operationName, marshallerCache.GetMarshaller <TRequest>(), marshallerCache.GetMarshaller <TResponse>()); var stub = new MethodStub <TService>(invoker, method, service); try { return(TryBind <TService, TRequest, TResponse>(bindContext, grpcMethod, stub)); } catch (Exception ex) { OnError(ex.Message); return(false); } }
protected override bool TryBind <TService, TRequest, TResponse>(ServiceBindContext bindContext, Method <TRequest, TResponse> method, MethodStub <TService> stub) { Methods.Add(method.Name); return(true); }
/// <summary> /// Construct a method body reader given a method stub. /// </summary> /// <param name="method">Method stub</param> public VirtualizedMethodBodyReader(MethodStub method) : this(method, null) { }
protected override bool TryBind <TService, TRequest, TResponse>(ServiceBindContext bindContext, Method <TRequest, TResponse> method, MethodStub <TService> stub) where TService : class where TRequest : class where TResponse : class { var builder = (ServerServiceDefinition.Builder)bindContext.State; switch (method.Type) { case MethodType.Unary: builder.AddMethod(method, stub.CreateDelegate <UnaryServerMethod <TRequest, TResponse> >()); break; case MethodType.ClientStreaming: builder.AddMethod(method, stub.CreateDelegate <ClientStreamingServerMethod <TRequest, TResponse> >()); break; case MethodType.ServerStreaming: builder.AddMethod(method, stub.CreateDelegate <ServerStreamingServerMethod <TRequest, TResponse> >()); break; case MethodType.DuplexStreaming: builder.AddMethod(method, stub.CreateDelegate <DuplexStreamingServerMethod <TRequest, TResponse> >()); break; default: return(false); } _log?.WriteLine($"{method.ServiceName} / {method.Name} ({method.Type}) bound to {stub.Method.DeclaringType.Name}.{stub.Method.Name}"); return(true); }
protected override bool TryBind <TService, TRequest, TResponse>(object state, Method <TRequest, TResponse> method, MethodStub <TService> stub) where TService : class where TRequest : class where TResponse : class { var metadata = new List <object>(); // Add type metadata first so it has a lower priority metadata.AddRange(typeof(TService).GetCustomAttributes(inherit: true)); // Add method metadata last so it has a higher priority metadata.AddRange(stub.Method.GetCustomAttributes(inherit: true)); var implementedMethod = typeof(TService).GetMethod(stub.Method.Name, stub.Method.GetParameters() .Select(s => s.ParameterType) .ToArray()); if (implementedMethod != null) { metadata.AddRange(implementedMethod.GetCustomAttributes(inherit: true)); } var context = (ServiceMethodProviderContext <TService>)state; switch (method.Type) { case MethodType.Unary: context.AddUnaryMethod(method, metadata, stub.CreateDelegate <UnaryServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ClientStreaming: context.AddClientStreamingMethod(method, metadata, stub.CreateDelegate <ClientStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ServerStreaming: context.AddServerStreamingMethod(method, metadata, stub.CreateDelegate <ServerStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.DuplexStreaming: context.AddDuplexStreamingMethod(method, metadata, stub.CreateDelegate <DuplexStreamingServerMethod <TService, TRequest, TResponse> >()); break; default: return(false); } return(true); }
/// <summary> /// Construct a method body reader given a method stub. /// </summary> /// <param name="method">Method stub</param> /// <param name="version">Serialization version</param> public VirtualizedMethodBodyReader(MethodStub method, SerializationVersion version = SerializationVersion.V1) : this(method, null, version) { }
/// <summary> /// The implementing binder should bind the method to the bind-state /// </summary> protected abstract bool TryBind <TService, TRequest, TResponse>(object state, Method <TRequest, TResponse> method, MethodStub <TService> stub) where TService : class where TRequest : class where TResponse : class;
protected override bool TryBind <TService, TRequest, TResponse>(ServiceBindContext bindContext, Method <TRequest, TResponse> method, MethodStub <TService> stub) where TService : class where TRequest : class where TResponse : class { var metadata = bindContext.GetMetadata(stub.Method); var context = (ServiceMethodProviderContext <TService>)bindContext.State; switch (method.Type) { case MethodType.Unary: context.AddUnaryMethod(method, metadata, stub.CreateDelegate <UnaryServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ClientStreaming: context.AddClientStreamingMethod(method, metadata, stub.CreateDelegate <ClientStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ServerStreaming: context.AddServerStreamingMethod(method, metadata, stub.CreateDelegate <ServerStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.DuplexStreaming: context.AddDuplexStreamingMethod(method, metadata, stub.CreateDelegate <DuplexStreamingServerMethod <TService, TRequest, TResponse> >()); break; default: return(false); } return(true); }
/// <summary> /// The implementing binder should bind the method to the bind-state /// </summary> protected abstract bool TryBind <TService, TRequest, TResponse>(ServiceBindContext bindContext, Method <TRequest, TResponse> method, MethodStub <TService> stub) where TService : class where TRequest : class where TResponse : class;
protected override bool OnBind <TService, TRequest, TResponse>(object state, Method <TRequest, TResponse> method, MethodStub stub, TService?service) where TService : class where TRequest : class where TResponse : class { var builder = (ServerServiceDefinition.Builder)state; switch (method.Type) { case MethodType.Unary: builder.AddMethod(method, stub.As <TService, UnaryServerMethod <TRequest, TResponse> >(service !)); break; case MethodType.ClientStreaming: builder.AddMethod(method, stub.As <TService, ClientStreamingServerMethod <TRequest, TResponse> >(service !)); break; case MethodType.ServerStreaming: builder.AddMethod(method, stub.As <TService, ServerStreamingServerMethod <TRequest, TResponse> >(service !)); break; case MethodType.DuplexStreaming: builder.AddMethod(method, stub.As <TService, DuplexStreamingServerMethod <TRequest, TResponse> >(service !)); break; default: return(false); } return(true); }
protected override bool OnBind <TService, TRequest, TResponse>(object state, Method <TRequest, TResponse> method, MethodStub stub, TService?service) where TService : class where TRequest : class where TResponse : class { var metadata = new List <object>(); // Add type metadata first so it has a lower priority metadata.AddRange(typeof(TService).GetCustomAttributes(inherit: true)); // Add method metadata last so it has a higher priority metadata.AddRange(stub.Method.GetCustomAttributes(inherit: true)); var context = (ServiceMethodProviderContext <TService>)state; switch (method.Type) { case MethodType.Unary: context.AddUnaryMethod(method, metadata, stub.As <UnaryServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ClientStreaming: context.AddClientStreamingMethod(method, metadata, stub.As <ClientStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.ServerStreaming: context.AddServerStreamingMethod(method, metadata, stub.As <ServerStreamingServerMethod <TService, TRequest, TResponse> >()); break; case MethodType.DuplexStreaming: context.AddDuplexStreamingMethod(method, metadata, stub.As <DuplexStreamingServerMethod <TService, TRequest, TResponse> >()); break; default: return(false); } return(true); }