Skip to content

Commit a6e8e84

Browse files
Merge pull request #138 from A9G-Data-Droid/AsyncReview
Fill the incomplete usage of async methods.
2 parents 73ce9cf + 050402c commit a6e8e84

13 files changed

+83
-69
lines changed

ExampleAppCore/ExampleAppCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
16+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

ExampleAppNet472/ExampleAppNET472.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
9+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

RazorEngineCore.Tests/RazorEngineCore.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
12-
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
12+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

RazorEngineCore.Tests/TestCompileAndRun.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ public void TestCompileCancellation_DynamicModel()
739739

740740
Assert.ThrowsException<OperationCanceledException>(() =>
741741
{
742-
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
742+
_ = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
743743
});
744744
}
745745
}
@@ -754,7 +754,7 @@ public async Task TestCompileCancellation_DynamicModelAsync()
754754

755755
await Assert.ThrowsExceptionAsync<OperationCanceledException>(async () =>
756756
{
757-
IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token);
757+
_ = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token);
758758
});
759759
}
760760
}
@@ -769,7 +769,7 @@ public void TestCompileCancellation_TypedModel1()
769769

770770
Assert.ThrowsException<OperationCanceledException>(() =>
771771
{
772-
IRazorEngineCompiledTemplate<TestTemplate1> template = razorEngine.Compile<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
772+
_ = razorEngine.Compile<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
773773
});
774774
}
775775
}
@@ -784,7 +784,7 @@ public async Task TestCompileCancellation_TypedModel1Async()
784784

785785
await Assert.ThrowsExceptionAsync<OperationCanceledException>(async () =>
786786
{
787-
IRazorEngineCompiledTemplate<TestTemplate1> template = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
787+
_ = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
788788
});
789789
}
790790
}

RazorEngineCore/AnonymousTypeWrapper.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
3232
return true;
3333
}
3434

35-
var type = result.GetType();
35+
//var type = result.GetType();
3636

3737
if (result.IsAnonymous())
3838
{
@@ -41,12 +41,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
4141

4242
if (result is IDictionary dictionary)
4343
{
44-
List<object> keys = new List<object>();
45-
46-
foreach(object key in dictionary.Keys)
47-
{
48-
keys.Add(key);
49-
}
44+
List<object> keys = dictionary.Keys.Cast<object>().ToList();
5045

5146
foreach(object key in keys)
5247
{
@@ -56,22 +51,13 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
5651
}
5752
}
5853
}
59-
else if (result is IEnumerable enumerable && !(result is string))
54+
else if (result is IEnumerable enumerable and not string)
6055
{
6156
result = enumerable.Cast<object>()
62-
.Select(e =>
63-
{
64-
if (e.IsAnonymous())
65-
{
66-
return new AnonymousTypeWrapper(e);
67-
}
68-
69-
return e;
70-
})
57+
.Select(e => e.IsAnonymous() ? new AnonymousTypeWrapper(e) : e)
7158
.ToList();
7259
}
73-
74-
60+
7561
return true;
7662
}
7763
}

RazorEngineCore/ObjectExtenders.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
7+
using System.Threading.Tasks;
78

89
namespace RazorEngineCore
910
{
@@ -32,18 +33,25 @@ public static bool IsAnonymous(this object obj)
3233
&& type.Attributes.HasFlag(TypeAttributes.NotPublic);
3334
}
3435

35-
public static long ReadLong(this Stream stream)
36+
public static async Task<long> ReadLong(this Stream stream)
3637
{
3738
byte[] buffer = new byte[8];
38-
stream.Read(buffer, 0, 8);
39-
39+
#if NETSTANDARD2_0
40+
_ = await stream.ReadAsync(buffer, 0, buffer.Length);
41+
#else
42+
_ = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));
43+
#endif
4044
return BitConverter.ToInt64(buffer, 0);
4145
}
4246

43-
public static void WriteLong(this Stream stream, long value)
47+
public static async Task WriteLong(this Stream stream, long value)
4448
{
4549
byte[] buffer = BitConverter.GetBytes(value);
46-
stream.Write(buffer, 0, buffer.Length);
50+
#if NETSTANDARD2_0
51+
await stream.WriteAsync(buffer, 0, buffer.Length);
52+
#else
53+
await stream.WriteAsync(buffer);
54+
#endif
4755
}
4856
}
4957
}

RazorEngineCore/RazorEngine.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorE
3030

3131
public Task<IRazorEngineCompiledTemplate<T>> CompileAsync<T>(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default) where T : IRazorEngineTemplate
3232
{
33-
return Task.Factory.StartNew(() => this.Compile<T>(content: content, builderAction: builderAction, cancellationToken: cancellationToken));
33+
return Task.Run(() => this.Compile<T>(content: content, builderAction: builderAction, cancellationToken: cancellationToken));
3434
}
3535

3636
public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default)
@@ -46,7 +46,7 @@ public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineC
4646

4747
public Task<IRazorEngineCompiledTemplate> CompileAsync(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default)
4848
{
49-
return Task.Factory.StartNew(() => this.Compile(
49+
return Task.Run(() => this.Compile(
5050
content,
5151
builderAction,
5252
cancellationToken));
@@ -105,7 +105,9 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin
105105
})
106106
.Concat(options.MetadataReferences)
107107
.ToList(),
108-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
108+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
109+
.WithOptimizationLevel(OptimizationLevel.Release)
110+
.WithOverflowChecks(true));
109111

110112

111113
MemoryStream assemblyStream = new MemoryStream();

RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ private string RenderTypeName(Type type)
6363

6464
string result = string.Join(".", elements.Where(e => !string.IsNullOrWhiteSpace(e)));
6565

66-
if (result.Contains('`'))
66+
int tildeLocation = result.IndexOf('`');
67+
if (tildeLocation > -1)
6768
{
68-
result = result.Substring(0, result.IndexOf("`"));
69+
result = result.Substring(0, tildeLocation);
6970
}
7071

7172
if (type.GenericTypeArguments.Length == 0)

RazorEngineCore/RazorEngineCompiledTemplate.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public static RazorEngineCompiledTemplate LoadFromFile(string fileName)
2222

2323
public static async Task<RazorEngineCompiledTemplate> LoadFromFileAsync(string fileName)
2424
{
25+
#if NETSTANDARD2_0
2526
using (FileStream fileStream = new FileStream(
26-
path: fileName,
27-
mode: FileMode.Open,
28-
access: FileAccess.Read,
29-
share: FileShare.None,
30-
bufferSize: 4096,
31-
useAsync: true))
27+
#else
28+
await using (FileStream fileStream = new FileStream(
29+
#endif
30+
path: fileName,
31+
mode: FileMode.Open,
32+
access: FileAccess.Read,
33+
share: FileShare.None,
34+
bufferSize: 4096,
35+
useAsync: true))
3236
{
3337
return await LoadFromStreamAsync(fileStream);
3438
}

RazorEngineCore/RazorEngineCompiledTemplateBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.IO;
22
using System;
3-
using System.Text;
43
using System.Threading.Tasks;
54

65
namespace RazorEngineCore
@@ -19,7 +18,11 @@ public void SaveToFile(string fileName)
1918

2019
public async Task SaveToFileAsync(string fileName)
2120
{
21+
#if NETSTANDARD2_0
2222
using (FileStream fileStream = new FileStream(
23+
#else
24+
await using (FileStream fileStream = new FileStream(
25+
#endif
2326
path: fileName,
2427
mode: FileMode.OpenOrCreate,
2528
access: FileAccess.Write,
@@ -36,9 +39,9 @@ public void SaveToStream(Stream stream)
3639
this.SaveToStreamAsync(stream).GetAwaiter().GetResult();
3740
}
3841

39-
public async Task SaveToStreamAsync(Stream stream)
42+
public Task SaveToStreamAsync(Stream stream)
4043
{
41-
await this.Meta.Write(stream);
44+
return this.Meta.Write(stream);
4245
}
4346

4447
public void EnableDebugging(string debuggingOutputDirectory = null)

0 commit comments

Comments
 (0)