Skip to content

Commit 5e524a6

Browse files
committed
Remove System.Web dependency from core React assembly. Now the System.Web dependency is solely contained in the React.Web assembly.
Also disabled "warnings as errors" for debug builds (still on for release builds)
1 parent a3b3409 commit 5e524a6

File tree

9 files changed

+142
-95
lines changed

9 files changed

+142
-95
lines changed

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ of patent rights can be found in the PATENTS file in the same directory.
1010
<Project ToolsVersion="4.0" DefaultTargets="Build;Test;Package" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1111
<PropertyGroup>
1212
<Major>0</Major>
13-
<Minor>1</Minor>
13+
<Minor>2</Minor>
1414
<Build>0</Build>
1515
<Revision>0</Revision>
1616
<DevBuild>true</DevBuild>

src/React.Web/React.Web.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<DocumentationFile>..\..\bin\Debug\React.Web\React.Web.XML</DocumentationFile>
24-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
24+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
2525
<NoWarn>1607</NoWarn>
2626
</PropertyGroup>
2727
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
@@ -65,6 +65,7 @@
6565
<Compile Include="AspNetFileSystem.cs" />
6666
<Compile Include="AssemblyRegistration.cs" />
6767
<Compile Include="IJsxHandler.cs" />
68+
<Compile Include="TinyIoCAspNetExtensions.cs" />
6869
<Compile Include="WebInitializer.cs" />
6970
<Compile Include="JsxHandler.cs" />
7071
<Compile Include="JsxHandlerFactory.cs" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.Linq;
12+
using System.Web;
13+
using React.TinyIoC;
14+
15+
namespace React.Web.TinyIoC
16+
{
17+
/// <summary>
18+
/// Scopes IoC registrations to the context of an ASP.NET web request. All instantiated
19+
/// components will be automatically disposed at the end of the request.
20+
/// </summary>
21+
public class HttpContextLifetimeProvider : TinyIoCContainer.ITinyIoCObjectLifetimeProvider
22+
{
23+
/// <summary>
24+
/// Prefix to use on HttpContext items
25+
/// </summary>
26+
private const string PREFIX = "TinyIoC.HttpContext.";
27+
/// <summary>
28+
/// Name of the key for this particular registration
29+
/// </summary>
30+
private readonly string _keyName = PREFIX + Guid.NewGuid();
31+
32+
/// <summary>
33+
/// Gets the stored object if it exists, or null if not
34+
/// </summary>
35+
/// <returns>Object instance or null</returns>
36+
public object GetObject()
37+
{
38+
return HttpContext.Current.Items[_keyName];
39+
}
40+
41+
/// <summary>
42+
/// Store the object
43+
/// </summary>
44+
/// <param name="value">Object to store</param>
45+
public void SetObject(object value)
46+
{
47+
HttpContext.Current.Items[_keyName] = value;
48+
}
49+
50+
/// <summary>
51+
/// Release the object
52+
/// </summary>
53+
public void ReleaseObject()
54+
{
55+
var item = GetObject() as IDisposable;
56+
57+
if (item != null)
58+
item.Dispose();
59+
60+
SetObject(null);
61+
}
62+
63+
/// <summary>
64+
/// Disposes all instantiated components
65+
/// </summary>
66+
public static void DisposeAll()
67+
{
68+
var items = HttpContext.Current.Items;
69+
var disposableItems = items.Keys.OfType<string>()
70+
.Where(key => key.StartsWith(PREFIX))
71+
.Select(key => items[key])
72+
.Where(item => item is IDisposable);
73+
74+
foreach (var item in disposableItems)
75+
{
76+
((IDisposable)item).Dispose();
77+
}
78+
}
79+
}
80+
}

src/React.Web/WebInitializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
using System.Web;
1111
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
12-
using React.TinyIoC;
1312
using React.Web;
13+
using React.Web.TinyIoC;
1414

1515
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebInitializer), "Initialize")]
1616

@@ -26,7 +26,7 @@ internal static class WebInitializer
2626
/// </summary>
2727
public static void Initialize()
2828
{
29-
Initializer.Initialize(isInAspNet: true);
29+
Initializer.Initialize(() => new HttpContextLifetimeProvider());
3030
DynamicModuleUtility.RegisterModule(typeof(IocPerRequestDisposal));
3131
}
3232

src/React/AssemblyRegistration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void Register(TinyIoCContainer container)
4242
EngineMode = JsEngineMode.ChakraActiveScript
4343
});
4444

45-
container.Register<IReactEnvironment, ReactEnvironment>().AsReactSingleton();
46-
container.Register<IJavaScriptEngineFactory, JavaScriptEngineFactory>().AsReactSingleton();
45+
container.Register<IReactEnvironment, ReactEnvironment>().AsPerRequestSingleton();
46+
container.Register<IJavaScriptEngineFactory, JavaScriptEngineFactory>().AsPerRequestSingleton();
4747
}
4848
}
4949
}

src/React/Initializer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System;
1111
using System.Linq;
1212
using System.Reflection;
13-
using System.Web;
1413
using React.TinyIoC;
1514

1615
namespace React
@@ -23,19 +22,18 @@ public static class Initializer
2322
/// <summary>
2423
/// Intialise ReactJS.NET
2524
/// </summary>
26-
public static void Initialize(bool isInAspNet)
25+
public static void Initialize(Func<TinyIoCContainer.ITinyIoCObjectLifetimeProvider> requestLifetimeProviderFactory)
2726
{
28-
InitializeIoC(isInAspNet);
27+
InitializeIoC(requestLifetimeProviderFactory);
2928
}
3029

3130
/// <summary>
3231
/// Initialises the IoC container by finding all <see cref="IAssemblyRegistration"/>
3332
/// implementations and calling their <see cref="IAssemblyRegistration.Register"/> methods.
3433
/// </summary>
35-
private static void InitializeIoC(bool isInAspNet)
34+
private static void InitializeIoC(Func<TinyIoCContainer.ITinyIoCObjectLifetimeProvider> requestLifetimeProviderFactory)
3635
{
37-
TinyIoCAspNetExtensions.IsInAspNet = isInAspNet;
38-
36+
TinyIoCExtensions.RequestLifetimeProviderFactory = requestLifetimeProviderFactory;
3937
var types = AppDomain.CurrentDomain.GetAssemblies()
4038
// Only bother checking React assemblies
4139
.Where(IsReactAssembly)

src/React/React.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<WarningLevel>4</WarningLevel>
2323
<PlatformTarget>AnyCPU</PlatformTarget>
2424
<DocumentationFile>..\..\bin\Debug\React\React.XML</DocumentationFile>
25-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
25+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
2626
<NoWarn>1607</NoWarn>
2727
</PropertyGroup>
2828
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
@@ -66,7 +66,6 @@
6666
<Reference Include="System.Configuration" />
6767
<Reference Include="System.Core" />
6868
<Reference Include="Microsoft.CSharp" />
69-
<Reference Include="System.Web" />
7069
</ItemGroup>
7170
<ItemGroup>
7271
<Compile Include="..\SharedAssemblyInfo.cs">
@@ -93,8 +92,8 @@
9392
<Compile Include="ReactComponent.cs" />
9493
<Compile Include="ReactEnvironment.cs" />
9594
<Compile Include="ReactSiteConfiguration.cs" />
95+
<Compile Include="TinyIoCExtensions.cs" />
9696
<Compile Include="TinyIoC\TinyIoC.cs" />
97-
<Compile Include="TinyIoC\TinyIoCAspNetExtensions.cs" />
9897
</ItemGroup>
9998
<ItemGroup>
10099
<EmbeddedResource Include="Resources\JSXTransformer.js" />

src/React/TinyIoC/TinyIoCAspNetExtensions.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/React/TinyIoCExtensions.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 React.TinyIoC;
12+
13+
namespace React
14+
{
15+
/// <summary>
16+
/// ReactJS.NET extensions to TinyIoC
17+
/// </summary>
18+
public static class TinyIoCExtensions
19+
{
20+
/// <summary>
21+
/// Gets or sets the factory used to create per-request lifetime providers
22+
/// </summary>
23+
internal static Func<TinyIoCContainer.ITinyIoCObjectLifetimeProvider> RequestLifetimeProviderFactory { private get; set; }
24+
25+
/// <summary>
26+
/// Registers a class in IoC that uses a singleton per "request". This is generally in the
27+
/// context of a web request.
28+
/// </summary>
29+
/// <param name="registerOptions">Class registration options</param>
30+
/// <returns>The class registration (fluent interface)</returns>
31+
public static TinyIoCContainer.RegisterOptions AsPerRequestSingleton(this TinyIoCContainer.RegisterOptions registerOptions)
32+
{
33+
if (RequestLifetimeProviderFactory == null)
34+
{
35+
throw new Exception(
36+
"RequestLifetimeProviderFactory needs to be set for per-request ReactJS.NET " +
37+
"assembly registrations to work. Please ensure you are calling " +
38+
"React.Initializer.Initialize() before using any ReactJS.NET functionality."
39+
);
40+
}
41+
42+
return TinyIoCContainer.RegisterOptions.ToCustomLifetimeManager(
43+
registerOptions,
44+
RequestLifetimeProviderFactory(),
45+
"per request singleton"
46+
);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)