Skip to content

Commit 89bc9cb

Browse files
committed
Changes required to get ReactJS.NET compiling under .NET Core
This still needs testing to be done and the examples updated to be compiled for .NET Core, but it's a start. References #294
1 parent 96f8c79 commit 89bc9cb

30 files changed

+287
-42
lines changed

build.proj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ of patent rights can be found in the PATENTS file in the same directory.
6565
<!-- Prepend date to build version if a dev build-->
6666
<PropertyGroup Condition="$(BuildType) == 'Release'">
6767
<BuildSuffix></BuildSuffix>
68+
<BuildWithSuffix>$(Build)</BuildWithSuffix>
6869
</PropertyGroup>
6970
<PropertyGroup Condition="$(BuildType) != 'Release'">
7071
<BuildSuffix>dev-$(Date)</BuildSuffix>
72+
<BuildWithSuffix>$(Build)-$(BuildSuffix)</BuildWithSuffix>
7173
</PropertyGroup>
7274
<!-- Set version for assemblies -->
7375
<AssemblyInfo
7476
CodeLanguage="CS"
7577
OutputFile="src\SharedAssemblyVersionInfo.cs"
7678
AssemblyVersion="$(Major).$(Minor)"
77-
AssemblyFileVersion="$(Major).$(Minor).$(Build)$(BuildSuffix).$(Revision)"
78-
AssemblyInformationalVersion="$(Major).$(Minor).$(Build)$(BuildSuffix)"
79+
AssemblyFileVersion="$(Major).$(Minor).$(BuildWithSuffix).$(Revision)"
80+
AssemblyInformationalVersion="$(Major).$(Minor).$(BuildWithSuffix)"
7981
/>
8082
<!-- Ensure version numbers in project.json files are in sync -->
8183
<UpdateAspNetProjectVersion

src/React.AspNet/AssemblyRegistration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public class AssemblyRegistration : IAssemblyRegistration
2424
public void Register(TinyIoCContainer container)
2525
{
2626
container.Register<IFileSystem, AspNetFileSystem>().AsSingleton();
27+
#if NET451
2728
container.Register<ICache, MemoryFileCache>().AsSingleton();
29+
#else
30+
container.Register<ICache, MemoryFileCacheCore>().AsSingleton();
31+
#endif
2832
}
2933
}
3034
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2016-Present, 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+
#if NETSTANDARD1_6
11+
using System;
12+
using System.Collections.Generic;
13+
using Microsoft.Extensions.Caching.Memory;
14+
using System.Linq;
15+
using Microsoft.Extensions.FileProviders;
16+
17+
namespace React.AspNet
18+
{
19+
/// <summary>
20+
/// Memory cache implementation for React.ICache. Uses IMemoryCache from .NET Core.
21+
/// </summary>
22+
public class MemoryFileCacheCore : ICache
23+
{
24+
private readonly IMemoryCache _cache;
25+
private readonly IFileProvider _fileProvider;
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="MemoryFileCacheCore" /> class.
29+
/// </summary>
30+
/// <param name="cache">The cache to use</param>
31+
/// <param name="fileProvider">The file provider.</param>
32+
public MemoryFileCacheCore(IMemoryCache cache, IFileProvider fileProvider)
33+
{
34+
_cache = cache;
35+
_fileProvider = fileProvider;
36+
}
37+
38+
/// <summary>
39+
/// Get an item from the cache. Returns <paramref name="fallback"/> if the item does
40+
/// not exist.
41+
/// </summary>
42+
/// <typeparam name="T">Type of data</typeparam>
43+
/// <param name="key">The cache key</param>
44+
/// <param name="fallback">Value to return if item is not in the cache</param>
45+
/// <returns>Data from cache, otherwise <paramref name="fallback"/></returns>
46+
public T Get<T>(string key, T fallback = default(T))
47+
{
48+
return (T)(_cache.Get(key) ?? fallback);
49+
}
50+
51+
/// <summary>
52+
/// Sets an item in the cache.
53+
/// </summary>
54+
/// <typeparam name="T">Type of data</typeparam>
55+
/// <param name="key">The cache key</param>
56+
/// <param name="data">Data to cache</param>
57+
/// <param name="slidingExpiration">
58+
/// Sliding expiration, if cache key is not accessed in this time period it will
59+
/// automatically be removed from the cache
60+
/// </param>
61+
/// <param name="cacheDependencyFiles">
62+
/// Filenames this cached item is dependent on. If any of these files change, the cache
63+
/// will be cleared automatically
64+
/// </param>
65+
/// <param name="cacheDependencyKeys">
66+
/// Other cache keys this cached item is dependent on. If any of these keys change, the
67+
/// cache will be cleared automatically
68+
/// </param>
69+
public void Set<T>(string key, T data, TimeSpan slidingExpiration, IEnumerable<string> cacheDependencyFiles = null, IEnumerable<string> cacheDependencyKeys = null)
70+
{
71+
if (data == null)
72+
{
73+
_cache.Remove(key);
74+
return;
75+
}
76+
77+
var options = new MemoryCacheEntryOptions
78+
{
79+
SlidingExpiration = slidingExpiration,
80+
};
81+
82+
if (cacheDependencyFiles != null)
83+
{
84+
foreach (var file in cacheDependencyFiles)
85+
{
86+
options.AddExpirationToken(_fileProvider.Watch(file));
87+
}
88+
}
89+
90+
if (cacheDependencyKeys != null && cacheDependencyKeys.Any())
91+
{
92+
// https://github.com/aspnet/Docs/issues/1938
93+
throw new NotImplementedException();
94+
}
95+
96+
_cache.Set(key, data, options);
97+
}
98+
}
99+
}
100+
#endif

src/React.AspNet/project.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
}
3737
},
3838
"frameworks": {
39-
"net451": {
40-
}
39+
"net451": {},
40+
"netstandard16": {
41+
"dependencies": {
42+
"Microsoft.Extensions.Caching.Abstractions": "1.0.0"
43+
}
44+
}
4145
},
4246
"buildOptions": {
4347
"compile": {

src/React.Core/Exceptions/BabelException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Thrown when an error occurs when transforming a JavaScript file via Babel.
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class BabelException : ReactException
2022
{
2123
/// <summary>
@@ -31,10 +33,12 @@ public BabelException(string message) : base(message) { }
3133
public BabelException(string message, Exception innerException)
3234
: base(message, innerException) { }
3335

36+
#if NET40
3437
/// <summary>
3538
/// Used by deserialization
3639
/// </summary>
3740
protected BabelException(SerializationInfo info, StreamingContext context)
3841
: base(info, context) { }
42+
#endif
3943
}
4044
}

src/React.Core/Exceptions/BabelNotLoadedException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Thrown when Babel is required but has not been loaded.
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class BabelNotLoadedException : ReactException
2022
{
2123
/// <summary>
2224
/// Initializes a new instance of the <see cref="BabelNotLoadedException"/> class.
2325
/// </summary>
2426
public BabelNotLoadedException() : base(GetMessage()) { }
2527

28+
#if NET40
2629
/// <summary>
2730
/// Used by deserialization
2831
/// </summary>
2932
protected BabelNotLoadedException(SerializationInfo info, StreamingContext context)
3033
: base(info, context)
3134
{ }
35+
#endif
3236

3337
/// <summary>
3438
/// Gets a message that describes the current exception.

src/React.Core/Exceptions/ClearScriptV8InitialisationException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Thrown when the ClearScript V8 JavaScript engine fails to initialise
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class ClearScriptV8InitialisationException : ReactException
2022
{
2123
/// <summary>
@@ -24,11 +26,13 @@ public class ClearScriptV8InitialisationException : ReactException
2426
public ClearScriptV8InitialisationException(Exception innerException) :
2527
base(GetMessage(innerException), innerException) { }
2628

29+
#if NET40
2730
/// <summary>
2831
/// Used by deserialization
2932
/// </summary>
3033
protected ClearScriptV8InitialisationException(SerializationInfo info, StreamingContext context)
3134
: base(info, context) { }
35+
#endif
3236

3337
/// <summary>
3438
/// Gets a message that describes the current exception.

src/React.Core/Exceptions/ReactConfigurationException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Thrown when an error occurs while reading a site configuration file.
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class ReactConfigurationException : ReactException
2022
{
2123
/// <summary>
@@ -30,10 +32,13 @@ public ReactConfigurationException(string message) : base(message) { }
3032
/// <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>
3133
public ReactConfigurationException(string message, Exception innerException)
3234
: base(message, innerException) { }
35+
36+
#if NET40
3337
/// <summary>
3438
/// Used by deserialization
3539
/// </summary>
3640
protected ReactConfigurationException(SerializationInfo info, StreamingContext context)
3741
: base(info, context) { }
42+
#endif
3843
}
3944
}

src/React.Core/Exceptions/ReactEngineNotFoundException.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Thrown when no valid JavaScript engine is found.
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class ReactEngineNotFoundException : ReactException
2022
{
2123
/// <summary>
2224
/// Initializes a new instance of the <see cref="ReactEngineNotFoundException"/> class.
2325
/// </summary>
2426
public ReactEngineNotFoundException() : base(GetMessage()) { }
2527

28+
#if NET40
2629
/// <summary>
2730
/// Used by deserialization
2831
/// </summary>
2932
protected ReactEngineNotFoundException(SerializationInfo info, StreamingContext context)
3033
: base(info, context) { }
34+
#endif
3135

3236
/// <summary>
3337
/// Gets a message that describes the current exception.
@@ -41,4 +45,4 @@ private static string GetMessage()
4145
"documentation for more details.";
4246
}
4347
}
44-
}
48+
}

src/React.Core/Exceptions/ReactException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace React.Exceptions
1515
/// <summary>
1616
/// Base class for all ReactJS.NET exceptions
1717
/// </summary>
18+
#if NET40
1819
[Serializable]
20+
#endif
1921
public class ReactException : Exception
2022
{
2123
/// <summary>
@@ -34,11 +36,14 @@ public ReactException(string message) : base(message) { }
3436
/// <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>
3537
public ReactException(string message, Exception innerException)
3638
: base(message, innerException) { }
39+
40+
#if NET40
3741
/// <summary>
3842
/// Used by deserialization
3943
/// </summary>
4044
protected ReactException(SerializationInfo info, StreamingContext context)
4145
: base(info, context)
4246
{ }
47+
#endif
4348
}
4449
}

0 commit comments

Comments
 (0)