Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill the incomplete usage of async methods. #138

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ExampleAppCore/ExampleAppCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ExampleAppNet472/ExampleAppNET472.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions RazorEngineCore.Tests/RazorEngineCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions RazorEngineCore.Tests/TestCompileAndRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public void TestCompileCancellation_DynamicModel()

Assert.ThrowsException<OperationCanceledException>(() =>
{
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
_ = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
});
}
}
Expand All @@ -754,7 +754,7 @@ public async Task TestCompileCancellation_DynamicModelAsync()

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

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

await Assert.ThrowsExceptionAsync<OperationCanceledException>(async () =>
{
IRazorEngineCompiledTemplate<TestTemplate1> template = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
_ = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
});
}
}
Expand Down
24 changes: 5 additions & 19 deletions RazorEngineCore/AnonymousTypeWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -41,12 +41,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)

if (result is IDictionary dictionary)
{
List<object> keys = new List<object>();

foreach(object key in dictionary.Keys)
{
keys.Add(key);
}
List<object> keys = dictionary.Keys.Cast<object>().ToList();

foreach(object key in keys)
{
Expand All @@ -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<object>()
.Select(e =>
{
if (e.IsAnonymous())
{
return new AnonymousTypeWrapper(e);
}

return e;
})
.Select(e => e.IsAnonymous() ? new AnonymousTypeWrapper(e) : e)
.ToList();
}



return true;
}
}
Expand Down
18 changes: 13 additions & 5 deletions RazorEngineCore/ObjectExtenders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace RazorEngineCore
{
Expand Down Expand Up @@ -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<long> 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
}
}
}
8 changes: 5 additions & 3 deletions RazorEngineCore/RazorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorE

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

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

public Task<IRazorEngineCompiledTemplate> CompileAsync(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default)
{
return Task.Factory.StartNew(() => this.Compile(
return Task.Run(() => this.Compile(
content,
builderAction,
cancellationToken));
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions RazorEngineCore/RazorEngineCompiledTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ public static RazorEngineCompiledTemplate LoadFromFile(string fileName)

public static async Task<RazorEngineCompiledTemplate> 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);
}
Expand Down
9 changes: 6 additions & 3 deletions RazorEngineCore/RazorEngineCompiledTemplateBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.IO;
using System;
using System.Text;
using System.Threading.Tasks;

namespace RazorEngineCore
Expand All @@ -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,
Expand All @@ -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)
Expand Down
28 changes: 17 additions & 11 deletions RazorEngineCore/RazorEngineCompiledTemplateMeta.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -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);
Expand All @@ -28,7 +27,7 @@ public async Task Write(Stream stream)

public static async Task<RazorEngineCompiledTemplateMeta> Read(Stream stream)
{
long version = stream.ReadLong();
long version = await stream.ReadLong();

if (version == 10001)
{
Expand All @@ -51,22 +50,26 @@ private static async Task<RazorEngineCompiledTemplateMeta> 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<string> ReadString(Stream stream)
Expand All @@ -77,16 +80,19 @@ private static async Task<string> ReadString(Stream stream)

private static async Task<byte[]> ReadBuffer(Stream stream)
{
long length = stream.ReadLong();
long length = await stream.ReadLong();

if (length == 0)
{
return null;
}

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;
}
}
Expand Down
19 changes: 11 additions & 8 deletions RazorEngineCore/RazorEngineCompiledTemplateT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RazorEngineCore
{
Expand All @@ -24,13 +23,17 @@ public static RazorEngineCompiledTemplate<T> LoadFromFile(string fileName)

public static async Task<RazorEngineCompiledTemplate<T>> 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);
}
Expand All @@ -53,7 +56,7 @@ public string Run(Action<T> initializer)

public async Task<string> RunAsync(Action<T> initializer)
{
T instance = (T) Activator.CreateInstance(this.TemplateType);
var instance = (T) Activator.CreateInstance(this.TemplateType);
initializer(instance);

if (this.IsDebuggerEnabled && instance is RazorEngineTemplateBase instance2)
Expand Down
7 changes: 4 additions & 3 deletions RazorEngineCore/RazorEngineCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Company>Alexander Selishchev</Company>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -20,9 +21,9 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.25" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down