Skip to content

Commit

Permalink
Better handling for ASP.NET IoC lifetime when calling from outside an…
Browse files Browse the repository at this point in the history
… ASP.NET request. References #39
  • Loading branch information
Daniel15 committed Nov 16, 2014
1 parent d2f7332 commit 8484122
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/React.Web/Exceptions/ReactAspNetException.cs
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

using System;
using System.Runtime.Serialization;
using React.Exceptions;

namespace React.Web.Exceptions
{
[Serializable]
public class ReactAspNetException : ReactException
{
/// <summary>
/// Initializes a new instance of the <see cref="ReactAspNetException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public ReactAspNetException(string message) : base(message) { }

/// <summary>
/// Initializes a new instance of the <see cref="ReactAspNetException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <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>
public ReactAspNetException(string message, Exception innerException)
: base(message, innerException) { }

/// <summary>
/// Used by deserialization
/// </summary>
protected ReactAspNetException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
}
1 change: 1 addition & 0 deletions src/React.Web/React.Web.csproj
Expand Up @@ -67,6 +67,7 @@
<Compile Include="AspNetCache.cs" />
<Compile Include="AspNetFileSystem.cs" />
<Compile Include="AssemblyRegistration.cs" />
<Compile Include="Exceptions\ReactAspNetException.cs" />
<Compile Include="IJsxHandler.cs" />
<Compile Include="TinyIoCAspNetExtensions.cs" />
<Compile Include="WebInitializer.cs" />
Expand Down
14 changes: 12 additions & 2 deletions src/React.Web/TinyIoCAspNetExtensions.cs
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Web;
using React.TinyIoC;
using React.Web.Exceptions;

namespace React.Web.TinyIoC
{
Expand All @@ -35,7 +36,8 @@ public class HttpContextLifetimeProvider : TinyIoCContainer.ITinyIoCObjectLifeti
/// <returns>Object instance or null</returns>
public object GetObject()
{
return HttpContext.Current.Items[_keyName];
var context = HttpContext.Current;
return context == null ? null : context.Items[_keyName];
}

/// <summary>
Expand All @@ -44,7 +46,15 @@ public object GetObject()
/// <param name="value">Object to store</param>
public void SetObject(object value)
{
HttpContext.Current.Items[_keyName] = value;
var context = HttpContext.Current;
if (context == null)
{
throw new ReactAspNetException(
"Trying to store item in HttpContext.Current while not in an ASP.NET " +
"request!"
);
}
context.Items[_keyName] = value;
}

/// <summary>
Expand Down

0 comments on commit 8484122

Please sign in to comment.