Ejemplo n.º 1
0
        public IServiceEndpointBinder <TService> GenerateServiceEndpointBinder <TService>(Type?serviceInstanceType)
        {
            if (serviceInstanceType != null && !ServiceContract.IsServiceInstanceType(serviceInstanceType))
            {
                throw new ArgumentOutOfRangeException(nameof(serviceInstanceType));
            }

            var serviceType = typeof(TService);

            ContractDescription description;
            Type contractType;
            Type channelType;

            lock (ProxyAssembly.SyncRoot)
            {
                (description, contractType) = GenerateContract(serviceType);
                channelType = ProxyAssembly.DefaultModule.GetType(ContractDescription.GetEndpointClassName(serviceType), false, false) !;
                if (channelType == null)
                {
                    var serviceBuilder = new EmitServiceEndpointBuilder(description);
                    channelType = serviceBuilder.Build(ProxyAssembly.DefaultModule, Logger);
                }
            }

            return(new EmitServiceEndpointBinder <TService>(description, serviceInstanceType, contractType, channelType, Logger));
        }
Ejemplo n.º 2
0
        public void BeforeAllTest()
        {
            var description  = new ContractDescription(typeof(IContract));
            var contractType = new EmitContractBuilder(description).Build(ProxyAssembly.DefaultModule, nameof(EmitServiceEndpointBuilderTest) + "Contract");

            var sut = new EmitServiceEndpointBuilder(description);

            ChannelType = sut.Build(ProxyAssembly.DefaultModule, className: nameof(EmitServiceEndpointBuilderTest) + "Channel");

            var contract = EmitContractBuilder.CreateFactory(contractType)(DataContractMarshallerFactory.Default);

            Channel = EmitServiceEndpointBuilder.CreateFactory(ChannelType)();
        }
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));
                    }
                }
            }
        }