This is a sample project of how to compile and execute C # and Blazor components directly in the browser.
https://blazorcomponents.github.io/CompileBlazorInBlazor/
- Updated to latest .Net Core 3.0 RC 1
- Updated to latest .Net Core 3.0 Preview 5
License: MIT License
This is a sample project of how to compile and execute C # and Blazor components directly in the browser.
https://blazorcomponents.github.io/CompileBlazorInBlazor/
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)));
) 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!
PS2 This fork resolves some of the issues that I mentioned: https://github.com/BlazorComponents/CompileBlazorInBlazor
Best regards,
TS
Hi
I try to update this project to 3.0.0-preview4-19216-03 but I have faced this error:
var engine = RazorProjectEngine.Create(BlazorExtensionInitializer.DefaultConfiguration, fileSystem, b =>
{
BlazorExtensionInitializer.Register(b);
});
BlazorExtensionInitializer is gone!!! :-\
and I dont know how can I replace it with new version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.