Exemplo n.º 1
0
        public void BeforeAllTests()
        {
            _description = new ContractDescription(typeof(IContract));

            var builder = new EmitContractBuilder(_description);

            _contractType = builder.Build(ProxyAssembly.CreateModule(nameof(EmitContractBuilderTest)));
            _factory      = EmitContractBuilder.CreateFactory(_contractType);
        }
Exemplo 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)();
        }
Exemplo n.º 3
0
        public void BeforeAllTests()
        {
            var description = new ContractDescription(typeof(IGenericContract <int, string>));

            var moduleBuilder = ProxyAssembly.CreateModule(nameof(EmitClientBuilderGenericTest));

            var contractBuilder = new EmitContractBuilder(description);
            var contractType    = contractBuilder.Build(moduleBuilder);
            var contractFactory = EmitContractBuilder.CreateFactory(contractType);

            var sut           = new EmitClientBuilder(description, contractType);
            var clientType    = sut.Build(moduleBuilder);
            var clientFactory = sut.CreateFactory <IGenericContract <int, string> >(clientType);

            Factory = () => clientFactory(CallInvoker.Object, contractFactory(DataContractMarshallerFactory.Default), null);
        }
Exemplo n.º 4
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));
                    }
                }
            }
        }