Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Prevent NullReferenceException when disposing Context.Scope (#782).
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira committed Jun 9, 2016
1 parent 525b925 commit 0f79bff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void DisposeContext(Context context, Exception exception)
}
}

context.Scope.Dispose();
context.Scope?.Dispose();

_httpContextFactory.Dispose(httpContext);
}
Expand Down
51 changes: 51 additions & 0 deletions test/Microsoft.AspNetCore.Hosting.Tests/HostingApplicationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
using Xunit;

namespace Microsoft.AspNetCore.Hosting.Tests
{
public class HostingApplicationTests
{
[Fact]
public void DisposeContextDoesNotThrowWhenContextScopeIsNull()
{
// Arrange
var httpContextFactory = new HttpContextFactory(new DefaultObjectPoolProvider(), Options.Create(new FormOptions()), new HttpContextAccessor());
var hostingApplication = new HostingApplication(ctx => Task.FromResult(0), new NullScopeLogger(), new NoopDiagnosticSource(), httpContextFactory);
var context = hostingApplication.CreateContext(new FeatureCollection());

// Act/Assert
hostingApplication.DisposeContext(context, null);
}

private class NullScopeLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state) => null;

public bool IsEnabled(LogLevel logLevel) => true;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
}
}

private class NoopDiagnosticSource : DiagnosticSource
{
public override bool IsEnabled(string name) => true;

public override void Write(string name, object value)
{
}
}
}
}

0 comments on commit 0f79bff

Please sign in to comment.