public RpcProxyBase <GrpcProxyMethod> CreateProxy <TService>() where TService : class
        {
            var generator = new GrpcProxyGenerator();

            var factory = generator.GenerateObjectProxyFactory <TService>(null, null);
            var proxy   = (RpcProxyBase <GrpcProxyMethod>)factory(RpcObjectId.Empty, new GrpcConnection(new RpcConnectionInfo(new Uri("grpc://localhost"))), null);

            return(proxy);
        }
Exemple #2
0
        internal GrpcConnection(
            RpcConnectionInfo connectionInfo,
            GrpcCore.ChannelCredentials credentials,
            IRpcClientOptions?options,
            GrpcProxyGenerator proxyGenerator,
            IEnumerable <GrpcCore.ChannelOption>?channelOptions)
            : base(connectionInfo, options, proxyGenerator)
        {
            if (connectionInfo?.HostUrl?.Scheme == GrpcConnectionProvider.GrpcScheme)
            {
                GrpcCore.ChannelCredentials actualCredentials = credentials;

                if (options != null)
                {
                    var callInterceptors = options.Interceptors;
                    if (callInterceptors != null)
                    {
                        int nInterceptors = callInterceptors.Count;
                        if (nInterceptors > 0)
                        {
                            GrpcCore.CallCredentials callCredentials;
                            if (nInterceptors > 1)
                            {
                                GrpcCore.CallCredentials[] allCallCredentials = new GrpcCore.CallCredentials[nInterceptors];
                                for (int index = 0; index < nInterceptors; index++)
                                {
                                    var callInterceptor = callInterceptors[index];
                                    allCallCredentials[index] = GrpcCore.CallCredentials.FromInterceptor((context, metadata) => callInterceptor(new GrpcCallMetadata(metadata)));
                                }

                                callCredentials = GrpcCore.CallCredentials.Compose(allCallCredentials);
                            }
                            else
                            {
                                var callInterceptor = callInterceptors[0];
                                callCredentials = GrpcCore.CallCredentials.FromInterceptor((context, metadata) => callInterceptor(new GrpcCallMetadata(metadata)));
                            }

                            actualCredentials = GrpcCore.ChannelCredentials.Create(actualCredentials, callCredentials);
                        }
                    }
                }

                var allOptions = ExtractOptions(options, channelOptions);

                this.Channel = new GrpcCore.Channel(connectionInfo.HostUrl.Host, connectionInfo.HostUrl.Port, actualCredentials, allOptions);

                this.CallInvoker = new GrpcCore.DefaultCallInvoker(this.Channel);

                this.isSecure = credentials != null && credentials != GrpcCore.ChannelCredentials.Insecure;
            }
            else
            {
                throw new NotImplementedException($"GrpcConnection is only implemented for the '{nameof(GrpcConnectionProvider.GrpcScheme)}' scheme.");
            }
        }
        internal NetGrpcConnection(
            RpcConnectionInfo connectionInfo,
            IRpcClientOptions?options,
            GrpcProxyGenerator proxyGenerator,
            GrpcNet.Client.GrpcChannelOptions?channelOptions)
            : base(connectionInfo, options, proxyGenerator)
        {
            if (connectionInfo is null)
            {
                throw new ArgumentNullException(nameof(connectionInfo));
            }

            var scheme = connectionInfo.HostUrl?.Scheme;

            if (connectionInfo.HostUrl != null &&
                (scheme == WellKnownRpcSchemes.Grpc || scheme == "https" || scheme == "http"))
            {
                GrpcNet.Client.GrpcChannelOptions actualChannelOptions = ExtractOptions(options, channelOptions);

                this.isSecure = scheme == "https" || scheme == WellKnownRpcSchemes.Grpc;

                var interceptors  = options?.Interceptors ?? ImmutableList <RpcClientCallInterceptor> .Empty;
                int nInterceptors = interceptors.Count;
                if (nInterceptors > 0)
                {
                    GrpcCore.CallCredentials callCredentials;
                    if (nInterceptors > 1)
                    {
                        GrpcCore.CallCredentials[] allCallCredentials = new GrpcCore.CallCredentials[nInterceptors];
                        for (int index = 0; index < nInterceptors; index++)
                        {
                            var callInterceptor = interceptors[index];
                            allCallCredentials[index] = GrpcCore.CallCredentials.FromInterceptor((context, metadata) => callInterceptor(new GrpcCallMetadata(metadata)));
                        }

                        callCredentials = GrpcCore.CallCredentials.Compose(allCallCredentials);
                    }
                    else
                    {
                        var callInterceptor = interceptors[0];
                        callCredentials = GrpcCore.CallCredentials.FromInterceptor((context, metadata) => callInterceptor(new GrpcCallMetadata(metadata)));
                    }

                    var channelCredentials = actualChannelOptions.Credentials;
                    if (channelCredentials == null)
                    {
                        if (this.isSecure)
                        {
                            channelCredentials = new GrpcCore.SslCredentials();
                        }
                        else
                        {
                            channelCredentials = GrpcCore.ChannelCredentials.Insecure;
                        }
                    }

                    actualChannelOptions.Credentials = GrpcCore.ChannelCredentials.Create(channelCredentials, callCredentials);
                }


                var channelUri = scheme == WellKnownRpcSchemes.Grpc
                    ? new Uri($"https://{connectionInfo.HostUrl.Authority}/")
                    : connectionInfo.HostUrl;

                this.Channel = GrpcNet.Client.GrpcChannel.ForAddress(channelUri, actualChannelOptions);

                this.CallInvoker = this.Channel.CreateCallInvoker();
            }
            else
            {
                throw new NotImplementedException($"NetGrpcConnection is only implemented for the '{WellKnownRpcSchemes.Grpc}' scheme.");
            }
        }