Skip to content

Commit

Permalink
refactor: Remove unnecesary service provider extension method.
Browse files Browse the repository at this point in the history
There's an equivalent method offered by Microsoft.Extensions.DependencyInjection so we don't need our own.
  • Loading branch information
amanda-tarafa committed May 24, 2021
1 parent 0c006c6 commit 0c00d2c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Google.Cloud.Diagnostics.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using Xunit;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void GetServiceCheckNotNull()
var accessor = new HttpContextAccessor();
var mockProvider = new Mock<IServiceProvider>();
mockProvider.Setup(p => p.GetService(typeof(IHttpContextAccessor))).Returns(accessor);
var service = mockProvider.Object.GetServiceCheckNotNull<IHttpContextAccessor>();
var service = mockProvider.Object.GetRequiredService<IHttpContextAccessor>();
Assert.Equal(accessor, service);
}

Expand All @@ -90,7 +91,7 @@ public void GetServiceCheckNotNull_Throws()
{
var mockProvider = new Mock<IServiceProvider>();
Assert.Throws<InvalidOperationException>(
() => mockProvider.Object.GetServiceCheckNotNull<IHttpContextAccessor>());
() => mockProvider.Object.GetRequiredService<IHttpContextAccessor>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,8 +112,8 @@ public static IServiceCollection AddGoogleExceptionLogging(
/// </summary>
private static IExceptionLogger CreateExceptionLogger(IServiceProvider provider)
{
var accessor = provider.GetServiceCheckNotNull<IHttpContextAccessor>();
var contextLogger = provider.GetServiceCheckNotNull<IContextExceptionLogger>();
var accessor = provider.GetRequiredService<IHttpContextAccessor>();
var contextLogger = provider.GetRequiredService<IContextExceptionLogger>();
return new GoogleExceptionLogger(contextLogger, accessor);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +57,7 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
var loggerFactory = app.ApplicationServices.GetServiceCheckNotNull<ILoggerFactory>();
var loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
loggerFactory.AddGoogle(app.ApplicationServices, _projectId, _loggerOptions);
app.UseGoogleExceptionLogging();
app.UseGoogleTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -141,8 +140,8 @@ public static IServiceCollection AddGoogleTrace(
/// </summary>
internal static TraceHeaderContext CreateTraceHeaderContext(IServiceProvider provider)
{
var accessor = provider.GetServiceCheckNotNull<IHttpContextAccessor>();
var traceDecisionPredicate = provider.GetServiceCheckNotNull<TraceDecisionPredicate>();
var accessor = provider.GetRequiredService<IHttpContextAccessor>();
var traceDecisionPredicate = provider.GetRequiredService<TraceDecisionPredicate>();

string header = accessor.HttpContext?.Request?.Headers[TraceHeaderContext.TraceHeader];
Func<bool?> shouldTraceFunc = () =>
Expand Down

0 comments on commit 0c00d2c

Please sign in to comment.