diff --git a/src/csharp/Grpc.Core.Api/ChannelBase.cs b/src/csharp/Grpc.Core.Api/ChannelBase.cs new file mode 100644 index 00000000000..54546300e81 --- /dev/null +++ b/src/csharp/Grpc.Core.Api/ChannelBase.cs @@ -0,0 +1,52 @@ +#region Copyright notice and license + +// Copyright 2019 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#endregion + +using System; +using Grpc.Core.Utils; + +namespace Grpc.Core +{ + /// + /// Base class for gRPC channel. Channels are an abstraction of long-lived connections to remote servers. + /// + public abstract class ChannelBase + { + private readonly string target; + + /// + /// Initializes a new instance of class that connects to a specific host. + /// + /// Target of the channel. + protected ChannelBase(string target) + { + this.target = GrpcPreconditions.CheckNotNull(target, nameof(target)); + } + + /// The original target used to create the channel. + public string Target + { + get { return this.target; } + } + + /// + /// Create a new for the channel. + /// + /// A new . + public abstract CallInvoker CreateCallInvoker(); + } +} diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index 97f79b0fb4f..fb58c077c70 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -30,7 +30,7 @@ namespace Grpc.Core /// More client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking /// a remote call so in general you should reuse a single channel for as many calls as possible. /// - public class Channel + public class Channel : ChannelBase { static readonly ILogger Logger = GrpcEnvironment.Logger.ForType(); @@ -38,7 +38,6 @@ namespace Grpc.Core readonly AtomicCounter activeCallCounter = new AtomicCounter(); readonly CancellationTokenSource shutdownTokenSource = new CancellationTokenSource(); - readonly string target; readonly GrpcEnvironment environment; readonly CompletionQueueSafeHandle completionQueue; readonly ChannelSafeHandle handle; @@ -64,9 +63,8 @@ namespace Grpc.Core /// Target of the channel. /// Credentials to secure the channel. /// Channel options. - public Channel(string target, ChannelCredentials credentials, IEnumerable options) + public Channel(string target, ChannelCredentials credentials, IEnumerable options) : base(target) { - this.target = GrpcPreconditions.CheckNotNull(target, "target"); this.options = CreateOptionsDictionary(options); EnsureUserAgentChannelOption(this.options); this.environment = GrpcEnvironment.AddRef(); @@ -179,15 +177,6 @@ namespace Grpc.Core } } - /// The original target used to create the channel. - public string Target - { - get - { - return this.target; - } - } - /// /// Returns a token that gets cancelled once ShutdownAsync is invoked. /// @@ -257,6 +246,15 @@ namespace Grpc.Core await GrpcEnvironment.ReleaseAsync().ConfigureAwait(false); } + /// + /// Create a new for the channel. + /// + /// A new . + public override CallInvoker CreateCallInvoker() + { + return new DefaultCallInvoker(this); + } + internal ChannelSafeHandle Handle { get diff --git a/src/csharp/Grpc.Core/ClientBase.cs b/src/csharp/Grpc.Core/ClientBase.cs index 05edce7467d..243c35e20c1 100644 --- a/src/csharp/Grpc.Core/ClientBase.cs +++ b/src/csharp/Grpc.Core/ClientBase.cs @@ -51,7 +51,7 @@ namespace Grpc.Core /// Initializes a new instance of ClientBase class. /// /// The channel to use for remote call invocation. - public ClientBase(Channel channel) : base(channel) + public ClientBase(ChannelBase channel) : base(channel) { } @@ -113,7 +113,7 @@ namespace Grpc.Core /// Initializes a new instance of ClientBase class. /// /// The channel to use for remote call invocation. - public ClientBase(Channel channel) : this(new DefaultCallInvoker(channel)) + public ClientBase(ChannelBase channel) : this(channel.CreateCallInvoker()) { }