diff --git a/ExampleAppCore/ExampleAppCore.csproj b/ExampleAppCore/ExampleAppCore.csproj index 341c87a..7a16981 100644 --- a/ExampleAppCore/ExampleAppCore.csproj +++ b/ExampleAppCore/ExampleAppCore.csproj @@ -13,7 +13,7 @@ - + diff --git a/ExampleAppNet472/ExampleAppNET472.csproj b/ExampleAppNet472/ExampleAppNET472.csproj index 55895bc..3622b8a 100644 --- a/ExampleAppNet472/ExampleAppNET472.csproj +++ b/ExampleAppNet472/ExampleAppNET472.csproj @@ -6,7 +6,7 @@ - + diff --git a/RazorEngineCore.Tests/RazorEngineCore.Tests.csproj b/RazorEngineCore.Tests/RazorEngineCore.Tests.csproj index 1ca2f7b..f194a4c 100644 --- a/RazorEngineCore.Tests/RazorEngineCore.Tests.csproj +++ b/RazorEngineCore.Tests/RazorEngineCore.Tests.csproj @@ -7,9 +7,9 @@ - - - + + + diff --git a/RazorEngineCore.Tests/TestCompileAndRun.cs b/RazorEngineCore.Tests/TestCompileAndRun.cs index 2103666..cab85a3 100644 --- a/RazorEngineCore.Tests/TestCompileAndRun.cs +++ b/RazorEngineCore.Tests/TestCompileAndRun.cs @@ -739,7 +739,7 @@ public void TestCompileCancellation_DynamicModel() Assert.ThrowsException(() => { - IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token); + _ = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token); }); } } @@ -754,7 +754,7 @@ public async Task TestCompileCancellation_DynamicModelAsync() await Assert.ThrowsExceptionAsync(async () => { - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token); + _ = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token); }); } } @@ -769,7 +769,7 @@ public void TestCompileCancellation_TypedModel1() Assert.ThrowsException(() => { - IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token); + _ = razorEngine.Compile("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token); }); } } @@ -784,7 +784,7 @@ public async Task TestCompileCancellation_TypedModel1Async() await Assert.ThrowsExceptionAsync(async () => { - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token); + _ = await razorEngine.CompileAsync("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token); }); } } diff --git a/RazorEngineCore/AnonymousTypeWrapper.cs b/RazorEngineCore/AnonymousTypeWrapper.cs index e01d269..ae36107 100644 --- a/RazorEngineCore/AnonymousTypeWrapper.cs +++ b/RazorEngineCore/AnonymousTypeWrapper.cs @@ -32,7 +32,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result) return true; } - var type = result.GetType(); + //var type = result.GetType(); if (result.IsAnonymous()) { @@ -41,12 +41,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result) if (result is IDictionary dictionary) { - List keys = new List(); - - foreach(object key in dictionary.Keys) - { - keys.Add(key); - } + List keys = dictionary.Keys.Cast().ToList(); foreach(object key in keys) { @@ -56,22 +51,13 @@ public override bool TryGetMember(GetMemberBinder binder, out object result) } } } - else if (result is IEnumerable enumerable && !(result is string)) + else if (result is IEnumerable enumerable and not string) { result = enumerable.Cast() - .Select(e => - { - if (e.IsAnonymous()) - { - return new AnonymousTypeWrapper(e); - } - - return e; - }) + .Select(e => e.IsAnonymous() ? new AnonymousTypeWrapper(e) : e) .ToList(); } - - + return true; } } diff --git a/RazorEngineCore/ObjectExtenders.cs b/RazorEngineCore/ObjectExtenders.cs index c15c13d..857bf1c 100644 --- a/RazorEngineCore/ObjectExtenders.cs +++ b/RazorEngineCore/ObjectExtenders.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace RazorEngineCore { @@ -32,18 +33,25 @@ public static bool IsAnonymous(this object obj) && type.Attributes.HasFlag(TypeAttributes.NotPublic); } - public static long ReadLong(this Stream stream) + public static async Task ReadLong(this Stream stream) { byte[] buffer = new byte[8]; - stream.Read(buffer, 0, 8); - +#if NETSTANDARD2_0 + _ = await stream.ReadAsync(buffer, 0, buffer.Length); +#else + _ = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length)); +#endif return BitConverter.ToInt64(buffer, 0); } - public static void WriteLong(this Stream stream, long value) + public static async Task WriteLong(this Stream stream, long value) { byte[] buffer = BitConverter.GetBytes(value); - stream.Write(buffer, 0, buffer.Length); +#if NETSTANDARD2_0 + await stream.WriteAsync(buffer, 0, buffer.Length); +#else + await stream.WriteAsync(buffer); +#endif } } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngine.cs b/RazorEngineCore/RazorEngine.cs index 4143d7d..89838d2 100644 --- a/RazorEngineCore/RazorEngine.cs +++ b/RazorEngineCore/RazorEngine.cs @@ -30,7 +30,7 @@ public IRazorEngineCompiledTemplate Compile(string content, Action> CompileAsync(string content, Action builderAction = null, CancellationToken cancellationToken = default) where T : IRazorEngineTemplate { - return Task.Factory.StartNew(() => this.Compile(content: content, builderAction: builderAction, cancellationToken: cancellationToken)); + return Task.Run(() => this.Compile(content: content, builderAction: builderAction, cancellationToken: cancellationToken)); } public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null, CancellationToken cancellationToken = default) @@ -46,7 +46,7 @@ public IRazorEngineCompiledTemplate Compile(string content, Action CompileAsync(string content, Action builderAction = null, CancellationToken cancellationToken = default) { - return Task.Factory.StartNew(() => this.Compile( + return Task.Run(() => this.Compile( content, builderAction, cancellationToken)); @@ -105,7 +105,9 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin }) .Concat(options.MetadataReferences) .ToList(), - new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) + .WithOptimizationLevel(OptimizationLevel.Release) + .WithOverflowChecks(true)); MemoryStream assemblyStream = new MemoryStream(); diff --git a/RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs b/RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs index 0b3c9b3..0ea6e82 100644 --- a/RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs +++ b/RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs @@ -63,9 +63,10 @@ private string RenderTypeName(Type type) string result = string.Join(".", elements.Where(e => !string.IsNullOrWhiteSpace(e))); - if (result.Contains('`')) + int tildeLocation = result.IndexOf('`'); + if (tildeLocation > -1) { - result = result.Substring(0, result.IndexOf("`")); + result = result.Substring(0, tildeLocation); } if (type.GenericTypeArguments.Length == 0) diff --git a/RazorEngineCore/RazorEngineCompiledTemplate.cs b/RazorEngineCore/RazorEngineCompiledTemplate.cs index d1dccf5..83bff64 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplate.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplate.cs @@ -22,13 +22,17 @@ public static RazorEngineCompiledTemplate LoadFromFile(string fileName) public static async Task LoadFromFileAsync(string fileName) { +#if NETSTANDARD2_0 using (FileStream fileStream = new FileStream( - path: fileName, - mode: FileMode.Open, - access: FileAccess.Read, - share: FileShare.None, - bufferSize: 4096, - useAsync: true)) +#else + await using (FileStream fileStream = new FileStream( +#endif + path: fileName, + mode: FileMode.Open, + access: FileAccess.Read, + share: FileShare.None, + bufferSize: 4096, + useAsync: true)) { return await LoadFromStreamAsync(fileStream); } diff --git a/RazorEngineCore/RazorEngineCompiledTemplateBase.cs b/RazorEngineCore/RazorEngineCompiledTemplateBase.cs index 514b26c..f6f5b56 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplateBase.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplateBase.cs @@ -1,6 +1,5 @@ using System.IO; using System; -using System.Text; using System.Threading.Tasks; namespace RazorEngineCore @@ -19,7 +18,11 @@ public void SaveToFile(string fileName) public async Task SaveToFileAsync(string fileName) { +#if NETSTANDARD2_0 using (FileStream fileStream = new FileStream( +#else + await using (FileStream fileStream = new FileStream( +#endif path: fileName, mode: FileMode.OpenOrCreate, access: FileAccess.Write, @@ -36,9 +39,9 @@ public void SaveToStream(Stream stream) this.SaveToStreamAsync(stream).GetAwaiter().GetResult(); } - public async Task SaveToStreamAsync(Stream stream) + public Task SaveToStreamAsync(Stream stream) { - await this.Meta.Write(stream); + return this.Meta.Write(stream); } public void EnableDebugging(string debuggingOutputDirectory = null) diff --git a/RazorEngineCore/RazorEngineCompiledTemplateMeta.cs b/RazorEngineCore/RazorEngineCompiledTemplateMeta.cs index f424abf..b72a525 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplateMeta.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplateMeta.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using System.Text; using System.Threading.Tasks; @@ -16,7 +15,7 @@ public class RazorEngineCompiledTemplateMeta public async Task Write(Stream stream) { - stream.WriteLong(10001); + await stream.WriteLong(10001); await this.WriteBuffer(stream, this.AssemblyByteCode); await this.WriteBuffer(stream, this.PdbByteCode); @@ -28,7 +27,7 @@ public async Task Write(Stream stream) public static async Task Read(Stream stream) { - long version = stream.ReadLong(); + long version = await stream.ReadLong(); if (version == 10001) { @@ -51,22 +50,26 @@ private static async Task LoadVersion1(Stream s }; } - private async Task WriteString(Stream stream, string value) + private Task WriteString(Stream stream, string value) { byte[] buffer = value == null ? null : Encoding.UTF8.GetBytes(value); - await this.WriteBuffer(stream, buffer); + return this.WriteBuffer(stream, buffer); } private async Task WriteBuffer(Stream stream, byte[] buffer) { if (buffer == null) { - stream.WriteLong(0); + await stream.WriteLong(0); return; } - stream.WriteLong(buffer.Length); + await stream.WriteLong(buffer.Length); +#if NETSTANDARD2_0 await stream.WriteAsync(buffer, 0, buffer.Length); +#else + await stream.WriteAsync(buffer); +#endif } private static async Task ReadString(Stream stream) @@ -77,7 +80,7 @@ private static async Task ReadString(Stream stream) private static async Task ReadBuffer(Stream stream) { - long length = stream.ReadLong(); + long length = await stream.ReadLong(); if (length == 0) { @@ -85,8 +88,11 @@ private static async Task ReadBuffer(Stream stream) } byte[] buffer = new byte[length]; - await stream.ReadAsync(buffer, 0, buffer.Length); - +#if NETSTANDARD2_0 + _ = await stream.ReadAsync(buffer, 0, buffer.Length); +#else + _ = await stream.ReadAsync(buffer); +#endif return buffer; } } diff --git a/RazorEngineCore/RazorEngineCompiledTemplateT.cs b/RazorEngineCore/RazorEngineCompiledTemplateT.cs index 011012f..6350b1c 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplateT.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplateT.cs @@ -2,7 +2,6 @@ using System.IO; using System.Reflection; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CSharp.Syntax; namespace RazorEngineCore { @@ -24,13 +23,17 @@ public static RazorEngineCompiledTemplate LoadFromFile(string fileName) public static async Task> LoadFromFileAsync(string fileName) { +#if NETSTANDARD2_0 using (FileStream fileStream = new FileStream( - path: fileName, - mode: FileMode.Open, - access: FileAccess.Read, - share: FileShare.None, - bufferSize: 4096, - useAsync: true)) +#else + await using (FileStream fileStream = new FileStream( +#endif + path: fileName, + mode: FileMode.Open, + access: FileAccess.Read, + share: FileShare.None, + bufferSize: 4096, + useAsync: true)) { return await LoadFromStreamAsync(fileStream); } @@ -53,7 +56,7 @@ public string Run(Action initializer) public async Task RunAsync(Action initializer) { - T instance = (T) Activator.CreateInstance(this.TemplateType); + var instance = (T) Activator.CreateInstance(this.TemplateType); initializer(instance); if (this.IsDebuggerEnabled && instance is RazorEngineTemplateBase instance2) diff --git a/RazorEngineCore/RazorEngineCore.csproj b/RazorEngineCore/RazorEngineCore.csproj index 99a82e2..f4e4a23 100644 --- a/RazorEngineCore/RazorEngineCore.csproj +++ b/RazorEngineCore/RazorEngineCore.csproj @@ -12,6 +12,7 @@ Alexander Selishchev true key.snk + latest true @@ -20,9 +21,9 @@ true - - - + + + MIT