Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions perf/benchmarkapps/QpsWorker/Infrastructure/ClientRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private Task RunClientAsync(GrpcChannel channel, IInterarrivalTimer timer)
return RunGenericStreamingAsync(channel, timer);
}

GrpcPreconditions.CheckNotNull(_payloadConfig.SimpleParams);
GrpcPreconditions.CheckNotNull(_payloadConfig.SimpleParams, "SimpleParams");
if (_clientType == ClientType.SyncClient)
{
GrpcPreconditions.CheckArgument(_rpcType == RpcType.Unary, "Sync client can only be used for Unary calls in C#");
Expand All @@ -326,7 +326,7 @@ private Task RunClientAsync(GrpcChannel channel, IInterarrivalTimer timer)

private SimpleRequest CreateSimpleRequest()
{
GrpcPreconditions.CheckNotNull(_payloadConfig.SimpleParams);
GrpcPreconditions.CheckNotNull(_payloadConfig.SimpleParams, "SimpleParams");
return new SimpleRequest
{
Payload = CreateZerosPayload(_payloadConfig.SimpleParams.ReqSize),
Expand Down
5 changes: 1 addition & 4 deletions src/Grpc.Auth/GoogleAuthInterceptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

#endregion

using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Grpc.Core;
using Grpc.Core.Utils;
Expand Down Expand Up @@ -83,7 +80,7 @@ public static AsyncAuthInterceptor FromCredential(ITokenAccessWithHeaders creden
/// <returns>The interceptor.</returns>
public static AsyncAuthInterceptor FromAccessToken(string accessToken)
{
GrpcPreconditions.CheckNotNull(accessToken);
GrpcPreconditions.CheckNotNull(accessToken, nameof(accessToken));
return new AsyncAuthInterceptor((context, metadata) =>
{
metadata.Add(CreateBearerTokenHeader(accessToken));
Expand Down
8 changes: 2 additions & 6 deletions src/Grpc.Core.Api/AsyncAuthInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

#endregion

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Grpc.Core.Internal;
using Grpc.Core.Utils;

namespace Grpc.Core;
Expand All @@ -46,8 +42,8 @@ public class AuthInterceptorContext
/// </summary>
public AuthInterceptorContext(string serviceUrl, string methodName)
{
this.serviceUrl = GrpcPreconditions.CheckNotNull(serviceUrl);
this.methodName = GrpcPreconditions.CheckNotNull(methodName);
this.serviceUrl = GrpcPreconditions.CheckNotNull(serviceUrl, nameof(serviceUrl));
this.methodName = GrpcPreconditions.CheckNotNull(methodName, nameof(methodName));
}

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions src/Grpc.Core.Api/AuthContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Grpc.Core.Utils;
Expand All @@ -31,8 +30,8 @@ namespace Grpc.Core;
/// </summary>
public class AuthContext
{
string? peerIdentityPropertyName;
Dictionary<string, List<AuthProperty>> properties;
private readonly string? peerIdentityPropertyName;
private readonly Dictionary<string, List<AuthProperty>> properties;

/// <summary>
/// Initializes a new instance of the <see cref="T:Grpc.Core.AuthContext"/> class.
Expand All @@ -42,7 +41,7 @@ public class AuthContext
public AuthContext(string? peerIdentityPropertyName, Dictionary<string, List<AuthProperty>> properties)
{
this.peerIdentityPropertyName = peerIdentityPropertyName;
this.properties = GrpcPreconditions.CheckNotNull(properties);
this.properties = GrpcPreconditions.CheckNotNull(properties, nameof(properties));
}

/// <summary>
Expand Down Expand Up @@ -101,8 +100,7 @@ public IEnumerable<AuthProperty> Properties
/// </summary>
public IEnumerable<AuthProperty> FindPropertiesByName(string propertyName)
{
List<AuthProperty>? result;
if (!properties.TryGetValue(propertyName, out result))
if (!properties.TryGetValue(propertyName, out var result))
{
return Enumerable.Empty<AuthProperty>();
}
Expand Down
15 changes: 8 additions & 7 deletions src/Grpc.Core.Api/AuthProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ namespace Grpc.Core;
public class AuthProperty
{
static readonly Encoding EncodingUTF8 = System.Text.Encoding.UTF8;
string name;
byte[] valueBytes;
string? lazyValue;

private readonly string name;
private readonly byte[] valueBytes;
private string? lazyValue;

private AuthProperty(string name, byte[] valueBytes)
{
this.name = GrpcPreconditions.CheckNotNull(name);
this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes);
this.name = GrpcPreconditions.CheckNotNull(name, nameof(name));
this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes, nameof(valueBytes));
}

/// <summary>
Expand All @@ -57,7 +58,7 @@ public string Value
{
get
{
return lazyValue ?? (lazyValue = EncodingUTF8.GetString(this.valueBytes));
return lazyValue ??= EncodingUTF8.GetString(this.valueBytes);
}
}

Expand All @@ -81,7 +82,7 @@ public byte[] ValueBytes
/// <param name="valueBytes">the binary value of the property</param>
public static AuthProperty Create(string name, byte[] valueBytes)
{
GrpcPreconditions.CheckNotNull(valueBytes);
GrpcPreconditions.CheckNotNull(valueBytes, nameof(valueBytes));
var valueCopy = new byte[valueBytes.Length];
Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
return new AuthProperty(name, valueCopy);
Expand Down
5 changes: 1 addition & 4 deletions src/Grpc.Core.Api/CallCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
#endregion

using System.Collections.Generic;
using System.Collections.ObjectModel;

using Grpc.Core.Internal;
using Grpc.Core.Utils;

namespace Grpc.Core;
Expand Down Expand Up @@ -78,7 +75,7 @@ private class AsyncAuthInterceptorCredentials : CallCredentials

public AsyncAuthInterceptorCredentials(AsyncAuthInterceptor interceptor)
{
this.interceptor = GrpcPreconditions.CheckNotNull(interceptor);
this.interceptor = GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor));
}

public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object? state)
Expand Down
9 changes: 2 additions & 7 deletions src/Grpc.Core.Api/ChannelCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Grpc.Core.Internal;
using Grpc.Core.Utils;

namespace Grpc.Core;
Expand Down Expand Up @@ -121,8 +116,8 @@ private sealed class CompositeChannelCredentials : ChannelCredentials
/// <param name="callCredentials">channelCredentials to compose</param>
public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
{
this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials, nameof(channelCredentials));
this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials, nameof(callCredentials));
}

public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Core.Api/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ internal ClientBaseConfigurationInfo(string? host, CallOptions callOptions)

internal ClientBaseConfiguration(CallInvoker undecoratedCallInvoker, string? host)
{
this.undecoratedCallInvoker = GrpcPreconditions.CheckNotNull(undecoratedCallInvoker);
this.undecoratedCallInvoker = GrpcPreconditions.CheckNotNull(undecoratedCallInvoker, nameof(undecoratedCallInvoker));
this.host = host;
}

Expand Down
1 change: 0 additions & 1 deletion src/Grpc.Core.Api/Grpc.Core.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<PackageReadmeFile>README.md</PackageReadmeFile>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<!-- grpc-dotnet global usings break the build. -->
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
Expand Down
11 changes: 6 additions & 5 deletions src/Grpc.Core.Api/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ public void Add(string key, byte[] valueBytes)

#region IList members


/// <summary>
/// <see cref="T:IList`1"/>
/// </summary>
Expand All @@ -163,7 +162,7 @@ public int IndexOf(Metadata.Entry item)
/// </summary>
public void Insert(int index, Metadata.Entry item)
{
GrpcPreconditions.CheckNotNull(item);
GrpcPreconditions.CheckNotNull(item, nameof(item));
CheckWriteable();
entries.Insert(index, item);
}
Expand All @@ -189,7 +188,7 @@ public Metadata.Entry this[int index]

set
{
GrpcPreconditions.CheckNotNull(value);
GrpcPreconditions.CheckNotNull(value, nameof(value));
CheckWriteable();
entries[index] = value;
}
Expand All @@ -200,7 +199,7 @@ public Metadata.Entry this[int index]
/// </summary>
public void Add(Metadata.Entry item)
{
GrpcPreconditions.CheckNotNull(item);
GrpcPreconditions.CheckNotNull(item, nameof(item));
CheckWriteable();
entries.Add(item);
}
Expand Down Expand Up @@ -462,8 +461,10 @@ private static bool IsValidKey(string input, out bool isLowercase)
'0' <= c && c <= '9' ||
c == '.' ||
c == '_' ||
c == '-' )
c == '-')
{
continue;
}

if ('A' <= c && c <= 'Z')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Core.Api/RpcException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public RpcException(Status status, Metadata trailers) : this(status, trailers, s
public RpcException(Status status, Metadata trailers, string message) : base(message, status.DebugException)
{
this.status = status;
this.trailers = GrpcPreconditions.CheckNotNull(trailers);
this.trailers = GrpcPreconditions.CheckNotNull(trailers, nameof(trailers));
}

/// <summary>
Expand Down
37 changes: 29 additions & 8 deletions src/Grpc.Core.Api/Utils/GrpcPreconditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public static void CheckArgument(bool condition)
{
if (!condition)
{
throw new ArgumentException();
Throw();
}

static void Throw()
=> throw new ArgumentException();
}

/// <summary>
Expand All @@ -46,21 +49,32 @@ public static void CheckArgument(bool condition, string errorMessage)
{
if (!condition)
{
throw new ArgumentException(errorMessage);
Throw(errorMessage);
}

static void Throw(string errorMessage)
=> throw new ArgumentException(errorMessage);
}

/// <summary>
/// Throws <see cref="ArgumentNullException"/> if reference is null.
/// </summary>
/// <param name="reference">The reference.</param>
#if DEBUG
// this method is on the public API; don't want to break anything
// external, but: prevent additional usage locally (i.e. in DEBUG mode)
[Obsolete("Specify parameter name when possible", error: true)]
#endif
public static T CheckNotNull<T>(T reference)
{
if (reference == null)
if (reference is null)
{
throw new ArgumentNullException();
Throw();
}
return reference;

static void Throw()
=> throw new ArgumentNullException();
}

/// <summary>
Expand All @@ -70,11 +84,14 @@ public static T CheckNotNull<T>(T reference)
/// <param name="paramName">The parameter name.</param>
public static T CheckNotNull<T>(T reference, string paramName)
{
if (reference == null)
if (reference is null)
{
throw new ArgumentNullException(paramName);
Throw(paramName);
}
return reference;

static void Throw(string paramName)
=> throw new ArgumentNullException(paramName);
}

/// <summary>
Expand All @@ -85,8 +102,10 @@ public static void CheckState(bool condition)
{
if (!condition)
{
throw new InvalidOperationException();
Throw();
}
static void Throw()
=> throw new InvalidOperationException();
}

/// <summary>
Expand All @@ -98,7 +117,9 @@ public static void CheckState(bool condition, string errorMessage)
{
if (!condition)
{
throw new InvalidOperationException(errorMessage);
Throw(errorMessage);
}
static void Throw(string errorMessage)
=> throw new InvalidOperationException(errorMessage);
}
}
3 changes: 0 additions & 3 deletions src/Grpc.HealthCheck/Grpc.HealthCheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>net462;netstandard1.5;netstandard2.0</TargetFrameworks>
<PackageReadmeFile>README.md</PackageReadmeFile>

<!-- TODO(jtattermusch): re-enable nullable -->
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 4 additions & 7 deletions src/Grpc.HealthCheck/HealthServiceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
// limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;

using Grpc.Core;
using Grpc.Core.Utils;
using Grpc.Health.V1;

namespace Grpc.HealthCheck;
Expand Down Expand Up @@ -227,11 +224,10 @@ private void NotifyStatus(string service, HealthCheckResponse.Types.ServingStatu

private HealthCheckResponse GetHealthCheckResponse(string service, bool throwOnNotFound)
{
HealthCheckResponse response = null;
HealthCheckResponse response;
lock (statusLock)
{
HealthCheckResponse.Types.ServingStatus status;
if (!statusMap.TryGetValue(service, out status))
if (!statusMap.TryGetValue(service, out HealthCheckResponse.Types.ServingStatus status))
{
if (throwOnNotFound)
{
Expand All @@ -251,6 +247,7 @@ private HealthCheckResponse GetHealthCheckResponse(string service, bool throwOnN

private HealthCheckResponse.Types.ServingStatus GetServiceStatus(string service)
{
GrpcPreconditions.CheckNotNull(service, nameof(service));
if (statusMap.TryGetValue(service, out HealthCheckResponse.Types.ServingStatus s))
{
return s;
Expand Down
3 changes: 0 additions & 3 deletions src/Grpc.Reflection/Grpc.Reflection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>net462;netstandard1.5;netstandard2.0</TargetFrameworks>
<PackageReadmeFile>README.md</PackageReadmeFile>

<!-- TODO(jtattermusch): re-enable nullable -->
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading