You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, great project and job. I'd like to share my sincere appreciation.
Secondly as of 06.2023 there are some issues with the code for .NET 7.0 (and I'm talking only about .NET 7.0, I did not test it with other versions).
Basically the project doesn't work with .NET-7.0 unless changes are made,
You are missing instantiation of public List CompileLog { get; set; } in lin 18 in CompileService.cs. The new() operator is needed or NullExceptions are thrown for newer .NET versions.
.NET 7.0 has troubles with loading resource.dlls in Blazor (the default xxx.resources.dll is missing from output _framework directory, only localized ones are present under eg. fr or es directories, therefore an exception is thrown near this line: references.Add(MetadataReference.CreateFromStream(await this._http.GetStreamAsync(_uriHelper.BaseUri + "_framework/" + name)));)
For the same line as aforementioned one, there should be "_framework/" instead of: "/_framework/_bin/"
The function below seems to have some issues. The most concerning is that SyntaxTree in First part returns no diagnostic issues, and they are null, therefore the process crashes.
public async Task<Assembly> Compile(string code)
{
await Init();
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Preview));
foreach (var diagnostic in syntaxTree.GetDiagnostics()) //first place where nullexception is thrown
{
CompileLog.Add(diagnostic.ToString()); //another because CompileLog has no new() operator
}
if (syntaxTree.GetDiagnostics().Any(i => i.Severity == DiagnosticSeverity.Error)) //again null vulnerability
{
CompileLog.Add("Parse SyntaxTree Error!"); //again null vulnerability
return null;
}
CompileLog.Add("Parse SyntaxTree Success");
CSharpCompilation compilation = CSharpCompilation.Create("CompileBlazorInBlazor.Demo", new[] {syntaxTree},
references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (MemoryStream stream = new MemoryStream())
{
EmitResult result = compilation.Emit(stream);
foreach (var diagnostic in result.Diagnostics) //first place where the diagnostics actually catch the errors
{
CompileLog.Add(diagnostic.ToString());
}
if (!result.Success)
{
CompileLog.Add("Compilation error");
return null;
}
CompileLog.Add("Compilation success!");
stream.Seek(0, SeekOrigin.Begin);
// var context = new CollectibleAssemblyLoadContext();
Assembly assemby = AppDomain.CurrentDomain.Load(stream.ToArray());
return assemby;
}
return null;
}
I improved that function as follows:
public async Task<Assembly> Compile(string code)
{
await Init();
try
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Preview));
var diags = syntaxTree.GetDiagnostics();
for (int i = 0; i < diags.Count(); i++)
{
var diagnostic = diags.ElementAt(i);
CompileLog.Add(diagnostic.ToString());
}
if (syntaxTree.GetDiagnostics()?.Any(i => i.Severity == DiagnosticSeverity.Error) ?? true)
{
CompileLog.Add("Parse SyntaxTree Error!");
return null;
}
CompileLog.Add("Parse SyntaxTree Success");
CSharpCompilation compilation = CSharpCompilation.Create("CompileBlazorInBlazor.Demo", new[] { syntaxTree },
references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (MemoryStream stream = new MemoryStream())
{
EmitResult result = compilation.Emit(stream);
foreach (var diagnostic in result.Diagnostics)
{
CompileLog.Add(diagnostic.ToString());
}
if (!result.Success)
{
CompileLog.Add("Compilation error");
return null;
}
CompileLog.Add("Compilation success!");
stream.Seek(0, SeekOrigin.Begin);
Assembly assemby = AppDomain.CurrentDomain.Load(stream.ToArray());
return assemby;
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
return null;
}
return null;
}
PS Again, thanks for sharing your project. Some serious gourmet code! Great work!
First of all, great project and job. I'd like to share my sincere appreciation.
Secondly as of 06.2023 there are some issues with the code for .NET 7.0 (and I'm talking only about .NET 7.0, I did not test it with other versions).
references.Add(MetadataReference.CreateFromStream(await this._http.GetStreamAsync(_uriHelper.BaseUri + "_framework/" + name)));
)I improved that function as follows:
PS Again, thanks for sharing your project. Some serious gourmet code! Great work!
PS2 This fork resolves some of the issues that I mentioned: https://github.com/BlazorComponents/CompileBlazorInBlazor
Best regards,
TS
The text was updated successfully, but these errors were encountered: