diff --git a/src/Microsoft.AspNet.Http.Abstractions/IHttpContextAccessor.cs b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextAccessor.cs new file mode 100644 index 0000000000..8f4046da2b --- /dev/null +++ b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextAccessor.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http/HttpContextAccessor.cs b/src/Microsoft.AspNet.Http/HttpContextAccessor.cs new file mode 100644 index 0000000000..384bb961ca --- /dev/null +++ b/src/Microsoft.AspNet.Http/HttpContextAccessor.cs @@ -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 _httpContextCurrent = new AsyncLocal(); + public HttpContext HttpContext + { + get + { + return _httpContextCurrent.Value; + } + set + { + _httpContextCurrent.Value = value; + } + } +#endif + } +}