示例#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));
        }
示例#2
0
        internal Type GetServiceInstanceType()
        {
            var serviceInstanceType = typeof(TService);

            if (ServiceContract.IsServiceInstanceType(serviceInstanceType))
            {
                return(serviceInstanceType);
            }

            try
            {
                return(_serviceProvider.GetRequiredService <TService>().GetType());
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          "A gRPC service binding is registered via {0}. Failed to resolve the implementation: {1}.".FormatWith(serviceInstanceType.GetShortAssemblyQualifiedName(), ex.Message),
                          ex);
            }
        }
示例#3
0
        private IServiceEndpointBinder <TService> GetOrCreateEndpointBinder()
        {
            if (_endpointBinder != null)
            {
                return(_endpointBinder);
            }

            var logger = new LogAdapter(_options?.Logger);

            var serviceInstanceType = typeof(TService);

            if (!ServiceContract.IsServiceInstanceType(serviceInstanceType))
            {
                serviceInstanceType = null;
            }

            return(new EmitGenerator {
                Logger = logger
            }.GenerateServiceEndpointBinder <TService>(serviceInstanceType));
        }