Skip to content

Commit 8484122

Browse files
committed
Better handling for ASP.NET IoC lifetime when calling from outside an ASP.NET request. References #39
1 parent d2f7332 commit 8484122

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using System.Runtime.Serialization;
12+
using React.Exceptions;
13+
14+
namespace React.Web.Exceptions
15+
{
16+
[Serializable]
17+
public class ReactAspNetException : ReactException
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ReactAspNetException"/> class.
21+
/// </summary>
22+
/// <param name="message">The message that describes the error.</param>
23+
public ReactAspNetException(string message) : base(message) { }
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="ReactAspNetException"/> class.
27+
/// </summary>
28+
/// <param name="message">The error message that explains the reason for the exception.</param>
29+
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
30+
public ReactAspNetException(string message, Exception innerException)
31+
: base(message, innerException) { }
32+
33+
/// <summary>
34+
/// Used by deserialization
35+
/// </summary>
36+
protected ReactAspNetException(SerializationInfo info, StreamingContext context)
37+
: base(info, context) { }
38+
}
39+
}

src/React.Web/React.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="AspNetCache.cs" />
6868
<Compile Include="AspNetFileSystem.cs" />
6969
<Compile Include="AssemblyRegistration.cs" />
70+
<Compile Include="Exceptions\ReactAspNetException.cs" />
7071
<Compile Include="IJsxHandler.cs" />
7172
<Compile Include="TinyIoCAspNetExtensions.cs" />
7273
<Compile Include="WebInitializer.cs" />

src/React.Web/TinyIoCAspNetExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using System.Web;
1313
using React.TinyIoC;
14+
using React.Web.Exceptions;
1415

1516
namespace React.Web.TinyIoC
1617
{
@@ -35,7 +36,8 @@ public class HttpContextLifetimeProvider : TinyIoCContainer.ITinyIoCObjectLifeti
3536
/// <returns>Object instance or null</returns>
3637
public object GetObject()
3738
{
38-
return HttpContext.Current.Items[_keyName];
39+
var context = HttpContext.Current;
40+
return context == null ? null : context.Items[_keyName];
3941
}
4042

4143
/// <summary>
@@ -44,7 +46,15 @@ public object GetObject()
4446
/// <param name="value">Object to store</param>
4547
public void SetObject(object value)
4648
{
47-
HttpContext.Current.Items[_keyName] = value;
49+
var context = HttpContext.Current;
50+
if (context == null)
51+
{
52+
throw new ReactAspNetException(
53+
"Trying to store item in HttpContext.Current while not in an ASP.NET " +
54+
"request!"
55+
);
56+
}
57+
context.Items[_keyName] = value;
4858
}
4959

5060
/// <summary>

0 commit comments

Comments
 (0)