Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NativeAOT support#555

Unanswered
ivanjx asked this question inQ&A
Aug 5, 2023· 4 comments
Discussion options

hi,

is this library supported in a native aot compiled .net application (i am on version .net 8)? thanks.

You must be logged in to vote

Replies: 4 comments

Comment options

i tested a simple case with JsonDocument and it seem to work:

usingSystem.Text.Json;usingHandlebarsDotNet;usingHandlebarsDotNet.Extension.Json;varmodel=JsonDocument.Parse("{\"Key1\":\"Val1\",\"Key2\":\"Val2\"}");varsource="{{#each this}}{{@key}}={{@value}}\n{{/each}}";varhandlebars=Handlebars.Create();handlebars.Configuration.UseJson();vartemplate=handlebars.Compile(source);varoutput=template(model);Console.WriteLine(output);
<ProjectSdk="Microsoft.NET.Sdk">  <PropertyGroup>    <OutputType>Exe</OutputType>    <TargetFramework>net8.0</TargetFramework>    <ImplicitUsings>enable</ImplicitUsings>    <Nullable>enable</Nullable>  </PropertyGroup>  <PropertyGroupCondition="'$(Configuration)'=='Release'">    <PublishAot>true</PublishAot>  </PropertyGroup>  <ItemGroup>    <PackageReferenceInclude="Handlebars.Net"Version="2.1.4" />    <PackageReferenceInclude="Handlebars.Net.Extension.Json"Version="1.0.0" />  </ItemGroup></Project>
You must be logged in to vote
0 replies
Comment options

sadly i cant make it work with a simple dictionary:

source=@"<diventry"">  <h1>{{title}}</h1>  <divbody"">    {{body}}  </div></div>";template=Handlebars.Compile(source);Dictionary<string,string>data=new(){{"title","my new post"},{"body","this is my post"}};varresult=template(data);Console.WriteLine(result);
Unhandled Exception: HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> System.ArgumentException: The handle is invalid.   at System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.GetMethodFromHandle(RuntimeMethodHandle, RuntimeTypeHandle) + 0xc7   at HandlebarsDotNet.Compiler.PathBinder.VisitPathExpression(PathExpression) + 0x9ef   at HandlebarsDotNet.Compiler.PathBinder.VisitStatementExpression(StatementExpression) + 0x67   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor, BlockExpression) + 0x4e   at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression) + 0x1d   at HandlebarsDotNet.Compiler.FunctionBuilder.Reduce(Expression, CompilationContext, IReadOnlyList`1&) + 0x1bc   at HandlebarsDotNet.Compiler.FunctionBuilder.CreateExpression(IEnumerable`1, CompilationContext, IReadOnlyList`1&) + 0xf3   --- End of inner exception stack trace ---   at HandlebarsDotNet.Compiler.FunctionBuilder.CreateExpression(IEnumerable`1, CompilationContext, IReadOnlyList`1&) + 0x1e4   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1, CompilationContext, IReadOnlyList`1&) + 0x14   --- End of inner exception stack trace ---   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1, CompilationContext, IReadOnlyList`1&) + 0xd8   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(ExtendedStringReader, CompilationContext) + 0xd9   at HandlebarsDotNet.HandlebarsEnvironment.Compile(TextReader) + 0x126   at HandlebarsDotNet.HandlebarsEnvironment.Compile(String) + 0x62   at Program.<Main>$(String[] args) + 0xf3   at naot!<BaseAddress>+0x340110Aborted (core dumped)
You must be logged in to vote
0 replies
Comment options

also thankfully it works correctly with custom objects with source generator:

usingSystem.Text.Json;usingSystem.Text.Json.Serialization;usingHandlebarsDotNet;usingHandlebarsDotNet.Extension.Json;varmodel=JsonSerializer.SerializeToDocument(new[]{newModel("ivan","programmer"),newModel("jane","cook"),newModel("joe","athlete")},MyContext.Default.ModelArray);varsource="{{#each this}}Name: {{name}}\nJob: {{job}}\n\n{{/each}}";varhandlebars=Handlebars.Create();handlebars.Configuration.UseJson();vartemplate=handlebars.Compile(source);varoutput=template(model);Console.WriteLine(output);Console.WriteLine();publicclassModel{[JsonPropertyName("name")]publicstringName{get;set;}[JsonPropertyName("job")]publicstringJob{get;set;}publicModel(stringname,stringjob){Name=name;Job=job;}}[JsonSerializable(typeof(Model[]))]publicpartialclassMyContext:JsonSerializerContext{}
You must be logged in to vote
0 replies
Comment options

the only problematic part is that i have to do this in the csproj otherwise it will have runtime error on Debug (not release):

  <PropertyGroupCondition="'$(Configuration)'=='Release'">    <PublishAot>true</PublishAot>  </PropertyGroup>

the error:

Unhandled exception. HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.   at System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported()   at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod)   at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes)   at System.Dynamic.Utils.DelegateHelpers.CreateObjectArrayDelegateRefEmit(Type delegateType, Func`2 handler)   at System.Linq.Expressions.Expression`1.Compile()   at HandlebarsDotNet.Features.DefaultCompilerFeature.DefaultExpressionCompiler.Compile[T](Expression`1 expression)   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   --- End of inner exception stack trace ---   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   at HandlebarsDotNet.Compiler.IteratorBinder.VisitIteratorExpression(IteratorExpression iex)   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor visitor, BlockExpression block)   at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)   at HandlebarsDotNet.Compiler.FunctionBuilder.Reduce(Expression expression, CompilationContext context, IReadOnlyList`1& decorators)   at HandlebarsDotNet.Compiler.FunctionBuilder.CreateExpression(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   --- End of inner exception stack trace ---   at HandlebarsDotNet.Compiler.FunctionBuilder.CreateExpression(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   --- End of inner exception stack trace ---   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, CompilationContext compilationContext, IReadOnlyList`1& decorators)   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(ExtendedStringReader source, CompilationContext compilationContext)   at HandlebarsDotNet.HandlebarsEnvironment.Compile(TextReader template)   at HandlebarsDotNet.HandlebarsEnvironment.Compile(String template)   at Program.<Main>$(String[] args) in /home/x/naot/naot/Program.cs:line 20

i am not familiar of how the library work but if it is possible to disable dynamic code gen for JsonDocument then i think i wont have to do the csproj thing.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@ivanjx

[8]ページ先頭

©2009-2025 Movatter.jp