/// <summary> /// Returns new instance of <see cref="CallOptions"/> with /// <c>Credentials</c> set to the value provided. Values of all other fields are preserved. /// </summary> /// <param name="credentials">The call credentials.</param> public CallOptions WithCredentials(CallCredentials credentials) { var newOptions = this; newOptions.credentials = credentials; return(newOptions); }
/// <summary> /// Returns a new <see cref="CallSettings"/> with the specified call credentials, /// merged with the (optional) original settings specified by <paramref name="settings"/>. /// </summary> /// <param name="settings">Original settings. May be null, in which case the returned settings /// will only contain call credentials.</param> /// <param name="credentials">Call credentials for the new call settings. /// This may be null, in which case any call credentials in <paramref name="settings"/> are /// not present in the new call settings. If both this and <paramref name="settings"/> are null, /// the return value is null.</param> /// <returns>A new set of call settings, or null if both parameters are null.</returns> public static CallSettings WithCallCredentials( this CallSettings settings, CallCredentials credentials) => settings == null ? CallSettings.FromCallCredentials(credentials) : new CallSettings(settings.CancellationToken, credentials, settings.Timing, settings.HeaderMutation, settings.WriteOptions, settings.PropagationToken);
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."); } }
/// <summary> /// Creates a new instance of <c>CallOptions</c> struct. /// </summary> /// <param name="headers">Headers to be sent with the call.</param> /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param> /// <param name="cancellationToken">Can be used to request cancellation of the call.</param> /// <param name="writeOptions">Write options that will be used for this call.</param> /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param> /// <param name="credentials">Credentials to use for this call.</param> public CallOptions(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken), WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null, CallCredentials credentials = null) { this.headers = headers; this.deadline = deadline; this.cancellationToken = cancellationToken; this.writeOptions = writeOptions; this.propagationToken = propagationToken; this.credentials = credentials; }
/// <summary> /// Creates a new instance of <c>CallOptions</c> struct. /// </summary> /// <param name="headers">Headers to be sent with the call.</param> /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param> /// <param name="cancellationToken">Can be used to request cancellation of the call.</param> /// <param name="writeOptions">Write options that will be used for this call.</param> /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param> /// <param name="credentials">Credentials to use for this call.</param> public CallOptions(Metadata headers = null, DateTime?deadline = null, GrpcCancellationToken cancellationToken = default(GrpcCancellationToken), WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null, CallCredentials credentials = null) { this.headers = headers; this.deadline = deadline; this.cancellationToken = cancellationToken; this.writeOptions = writeOptions; this.propagationToken = propagationToken; this.credentials = credentials; }
/// <summary> /// Initializes a new instance of <c>CompositeChannelCredentials</c> class. /// The resulting credentials object will be composite of all the credentials specified as parameters. /// </summary> /// <param name="channelCredentials">channelCredentials to compose</param> /// <param name="callCredentials">channelCredentials to compose</param> public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) { this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials); this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials); if (!channelCredentials.IsComposable) { throw new ArgumentException(string.Format("CallCredentials can't be composed with {0}. CallCredentials must be used with secure channel credentials like SslCredentials.", channelCredentials.GetType().Name)); } }
/// <summary> /// Constructs an instance with the specified settings. /// </summary> /// <param name="cancellationToken">Cancellation token that can be used for cancelling the call.</param> /// <param name="credentials">Credentials to use for the call.</param> /// <param name="timing"><see cref="CallTiming"/> to use, or null for default retry/expiration behavior.</param> /// <param name="headerMutation">Action to modify the headers to send at the beginning of the call.</param> /// <param name="writeOptions"><see cref="global::Grpc.Core.WriteOptions"/> that will be used for the call.</param> /// <param name="propagationToken"><see cref="ContextPropagationToken"/> for propagating settings from a parent call.</param> public CallSettings( CancellationToken? cancellationToken, CallCredentials credentials, CallTiming timing, Action<Metadata> headerMutation, WriteOptions writeOptions, ContextPropagationToken propagationToken) { CancellationToken = cancellationToken; Credentials = credentials; Timing = timing; HeaderMutation = headerMutation; WriteOptions = writeOptions; PropagationToken = propagationToken; }
/// <summary> /// Creates a new instance of <c>ChannelCredentials</c> class by composing /// given channel credentials with call credentials. /// </summary> /// <param name="channelCredentials">Channel credentials.</param> /// <param name="callCredentials">Call credentials.</param> /// <returns>The new composite <c>ChannelCredentials</c></returns> public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials) { return new CompositeChannelCredentials(channelCredentials, callCredentials); }
/// <summary> /// Initializes a new instance of <c>CompositeChannelCredentials</c> class. /// The resulting credentials object will be composite of all the credentials specified as parameters. /// </summary> /// <param name="channelCredentials">channelCredentials to compose</param> /// <param name="callCredentials">channelCredentials to compose</param> public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) { this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials); this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials); GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition."); }
/// <summary> /// Configures the credentials to use composite channel credentials (a composite of channel credentials and call credentials). /// </summary> public abstract void SetCompositeCredentials(object state, ChannelCredentials channelCredentials, CallCredentials callCredentials);
/// <summary> /// Creates a new instance of <c>ChannelCredentials</c> by wrapping /// an instance of <c>CallCredentials</c>. /// </summary> /// <param name="callCredentials">Call credentials.</param> /// <returns>The <c>ChannelCredentials</c> wrapping given call credentials.</returns> public static ChannelCredentials Create(CallCredentials callCredentials) { return(new WrappedCallCredentials(callCredentials)); }
/// <summary> /// Wraps instance of <c>CallCredentials</c> as <c>ChannelCredentials</c>. /// </summary> /// <param name="callCredentials">credentials to wrap</param> public WrappedCallCredentials(CallCredentials callCredentials) { this.callCredentials = Preconditions.CheckNotNull(callCredentials); }
/// <summary> /// Initializes a new instance of <c>CompositeChannelCredentials</c> class. /// The resulting credentials object will be composite of all the credentials specified as parameters. /// </summary> /// <param name="channelCredentials">channelCredentials to compose</param> /// <param name="callCredentials">channelCredentials to compose</param> public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) { this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials); this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials); GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition."); }
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."); } }
/// <summary> /// Returns new instance of <see cref="CallOptions"/> with /// <c>Credentials</c> set to the value provided. Values of all other fields are preserved. /// </summary> /// <param name="credentials">The call credentials.</param> public CallOptions WithCredentials(CallCredentials credentials) { var newOptions = this; newOptions.credentials = credentials; return newOptions; }
/// <summary> /// Creates a new instance of <c>ChannelCredentials</c> by wrapping /// an instance of <c>CallCredentials</c>. /// </summary> /// <param name="callCredentials">Call credentials.</param> /// <returns>The <c>ChannelCredentials</c> wrapping given call credentials.</returns> public static ChannelCredentials Create(CallCredentials callCredentials) { return new WrappedCallCredentials(callCredentials); }
/// <summary> /// Wraps instance of <c>CallCredentials</c> as <c>ChannelCredentials</c>. /// </summary> /// <param name="callCredentials">credentials to wrap</param> public WrappedCallCredentials(CallCredentials callCredentials) { this.callCredentials = Preconditions.CheckNotNull(callCredentials); }
/// <summary> /// Creates a new instance of <c>ChannelCredentials</c> class by composing /// given channel credentials with call credentials. /// </summary> /// <param name="channelCredentials">Channel credentials.</param> /// <param name="callCredentials">Call credentials.</param> /// <returns>The new composite <c>ChannelCredentials</c></returns> public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials) { return(new CompositeChannelCredentials(channelCredentials, callCredentials)); }
/// <summary> /// Creates a <see cref="CallSettings"/> for the specified call credentials, or returns null /// if <paramref name="credentials"/> is null. /// </summary> /// <param name="credentials">The call credentials for the new settings.</param> /// <returns>A new instance, or null if <paramref name="credentials"/> is null.</returns> public static CallSettings FromCallCredentials(CallCredentials credentials) => credentials == null ? null : new CallSettings(null, credentials, null, null, null, null);