Hello HttpContextAccessor

This commit is contained in:
Hao Kung 2015-08-12 13:08:58 -07:00
parent 282c50876b
commit 8487e42a68
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,10 @@
// 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.
namespace Microsoft.AspNet.Http
{
public interface IHttpContextAccessor
{
HttpContext HttpContext { get; set; }
}
}

View File

@ -0,0 +1,47 @@
// 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;
#if DNX451
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
#elif DNXCORE50
using System.Threading;
#endif
namespace Microsoft.AspNet.Http.Internal
{
public class HttpContextAccessor : IHttpContextAccessor
{
#if DNX451
private const string LogicalDataKey = "__HttpContext_Current__";
public HttpContext HttpContext
{
get
{
var handle = CallContext.LogicalGetData(LogicalDataKey) as ObjectHandle;
return handle != null ? handle.Unwrap() as HttpContext : null;
}
set
{
CallContext.LogicalSetData(LogicalDataKey, new ObjectHandle(value));
}
}
#elif DNXCORE50
private AsyncLocal<HttpContext> _httpContextCurrent = new AsyncLocal<HttpContext>();
public HttpContext HttpContext
{
get
{
return _httpContextCurrent.Value;
}
set
{
_httpContextCurrent.Value = value;
}
}
#endif
}
}