Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 8487e42

Browse files
committed
Hello HttpContextAccessor
1 parent 282c508 commit 8487e42

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.Http
5+
{
6+
public interface IHttpContextAccessor
7+
{
8+
HttpContext HttpContext { get; set; }
9+
}
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
#if DNX451
6+
using System.Runtime.Remoting.Messaging;
7+
using System.Runtime.Remoting;
8+
#elif DNXCORE50
9+
using System.Threading;
10+
#endif
11+
12+
namespace Microsoft.AspNet.Http.Internal
13+
{
14+
public class HttpContextAccessor : IHttpContextAccessor
15+
{
16+
#if DNX451
17+
private const string LogicalDataKey = "__HttpContext_Current__";
18+
19+
public HttpContext HttpContext
20+
{
21+
get
22+
{
23+
var handle = CallContext.LogicalGetData(LogicalDataKey) as ObjectHandle;
24+
return handle != null ? handle.Unwrap() as HttpContext : null;
25+
}
26+
set
27+
{
28+
CallContext.LogicalSetData(LogicalDataKey, new ObjectHandle(value));
29+
}
30+
}
31+
32+
#elif DNXCORE50
33+
private AsyncLocal<HttpContext> _httpContextCurrent = new AsyncLocal<HttpContext>();
34+
public HttpContext HttpContext
35+
{
36+
get
37+
{
38+
return _httpContextCurrent.Value;
39+
}
40+
set
41+
{
42+
_httpContextCurrent.Value = value;
43+
}
44+
}
45+
#endif
46+
}
47+
}

0 commit comments

Comments
 (0)