diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Trace/CloudTraceExtensionTest.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Trace/CloudTraceExtensionTest.cs index 8de9d5d10e09..0901a63e1fca 100644 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Trace/CloudTraceExtensionTest.cs +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Trace/CloudTraceExtensionTest.cs @@ -14,6 +14,7 @@ using Google.Cloud.Diagnostics.Common; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; using Moq; using System; using Xunit; @@ -81,7 +82,7 @@ public void GetServiceCheckNotNull() var accessor = new HttpContextAccessor(); var mockProvider = new Mock(); mockProvider.Setup(p => p.GetService(typeof(IHttpContextAccessor))).Returns(accessor); - var service = mockProvider.Object.GetServiceCheckNotNull(); + var service = mockProvider.Object.GetRequiredService(); Assert.Equal(accessor, service); } @@ -90,7 +91,7 @@ public void GetServiceCheckNotNull_Throws() { var mockProvider = new Mock(); Assert.Throws( - () => mockProvider.Object.GetServiceCheckNotNull()); + () => mockProvider.Object.GetRequiredService()); } } } \ No newline at end of file diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/ErrorReporting/ErrorReportingExceptionLoggerExtension.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/ErrorReporting/ErrorReportingExceptionLoggerExtension.cs index 8b32c0561941..75ad4328bcdc 100644 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/ErrorReporting/ErrorReportingExceptionLoggerExtension.cs +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/ErrorReporting/ErrorReportingExceptionLoggerExtension.cs @@ -17,7 +17,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using System; #if NETCOREAPP3_1 @@ -113,8 +112,8 @@ public static IServiceCollection AddGoogleExceptionLogging( /// private static IExceptionLogger CreateExceptionLogger(IServiceProvider provider) { - var accessor = provider.GetServiceCheckNotNull(); - var contextLogger = provider.GetServiceCheckNotNull(); + var accessor = provider.GetRequiredService(); + var contextLogger = provider.GetRequiredService(); return new GoogleExceptionLogger(contextLogger, accessor); } } diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Extensions.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Extensions.cs deleted file mode 100644 index 216dc866d744..000000000000 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Extensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2017 Google Inc. All Rights Reserved. -// -// 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. - -using Google.Api.Gax; -using System; -using Microsoft.Extensions.DependencyInjection; - -#if NETCOREAPP3_1 -namespace Google.Cloud.Diagnostics.AspNetCore3 -#elif NETSTANDARD2_0 -namespace Google.Cloud.Diagnostics.AspNetCore -#else -#error unknown target framework -#endif -{ - /// - /// Shared extensions for AspNetCore code. - /// - internal static class Extensions - { - /// - /// Extension for the that will call and return - /// . If the result of the - /// call is null it will throw. - /// - internal static T GetServiceCheckNotNull(this IServiceProvider provider) - { - var message = $"No {typeof(T)} service found. Ensure the Google service is properly set up."; - T service = provider.GetService(); - GaxPreconditions.CheckState(service != null, message); - return service; - } - } -} diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/GoogleDiagnosticsStartupFilter.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/GoogleDiagnosticsStartupFilter.cs index bd774ef88774..7b6879c7793e 100644 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/GoogleDiagnosticsStartupFilter.cs +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/GoogleDiagnosticsStartupFilter.cs @@ -12,12 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; -using Google.Api; -using Google.Cloud.Diagnostics.Common; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System; #if NETCOREAPP3_1 namespace Google.Cloud.Diagnostics.AspNetCore3 @@ -58,7 +57,7 @@ public Action Configure(Action next) { return app => { - var loggerFactory = app.ApplicationServices.GetServiceCheckNotNull(); + var loggerFactory = app.ApplicationServices.GetRequiredService(); loggerFactory.AddGoogle(app.ApplicationServices, _projectId, _loggerOptions); app.UseGoogleExceptionLogging(); app.UseGoogleTrace(); diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Trace/CloudTraceExtension.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Trace/CloudTraceExtension.cs index af93d2b8f8da..52c7d664e44a 100644 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Trace/CloudTraceExtension.cs +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Trace/CloudTraceExtension.cs @@ -18,7 +18,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using System; #if NETCOREAPP3_1 @@ -141,8 +140,8 @@ public static IServiceCollection AddGoogleTrace( /// internal static TraceHeaderContext CreateTraceHeaderContext(IServiceProvider provider) { - var accessor = provider.GetServiceCheckNotNull(); - var traceDecisionPredicate = provider.GetServiceCheckNotNull(); + var accessor = provider.GetRequiredService(); + var traceDecisionPredicate = provider.GetRequiredService(); string header = accessor.HttpContext?.Request?.Headers[TraceHeaderContext.TraceHeader]; Func shouldTraceFunc = () =>