VOOZH about

URL: https://www.nuget.org/packages/SpawnDev.BlazorJS.CodeRunner/

⇱ NuGet Gallery | SpawnDev.BlazorJS.CodeRunner 1.0.0




👁 Image
SpawnDev.BlazorJS.CodeRunner 1.0.0

dotnet add package SpawnDev.BlazorJS.CodeRunner --version 1.0.0
 
 
NuGet\Install-Package SpawnDev.BlazorJS.CodeRunner -Version 1.0.0
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SpawnDev.BlazorJS.CodeRunner" Version="1.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SpawnDev.BlazorJS.CodeRunner" Version="1.0.0" />
 
Directory.Packages.props
<PackageReference Include="SpawnDev.BlazorJS.CodeRunner" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SpawnDev.BlazorJS.CodeRunner --version 1.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SpawnDev.BlazorJS.CodeRunner, 1.0.0"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SpawnDev.BlazorJS.CodeRunner@1.0.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SpawnDev.BlazorJS.CodeRunner&version=1.0.0
 
Install as a Cake Addin
#tool nuget:?package=SpawnDev.BlazorJS.CodeRunner&version=1.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

SpawnDev.BlazorJS.CodeRunner

👁 NuGet

Provides a C# code compiler service for compiling and running C# code and Blazor components at runtime. 99% of the code compiling work was already completed by StefH and MudBlazor/TryMudBlazor. This repo just packages it in a way I find useful for my projects.

The demo also uses BlazorMonaco. A Blazor component for Microsoft Monaco Editor which powers Visual Studio Code.

Demo

Live Demo

Getting started

Add SpawnDev.BlazorJS.CodeRunner package reference to your Project.csproj

<ItemGroup>
	<PackageReference Include="SpawnDev.BlazorJS.CodeRunner" Version="0.0.17" />
</ItemGroup>

CodeRunner uses Microsoft.CodeAnalysis.CSharp. Microsoft now publishes that library on one of their Azure Nuget package hosts, referred to as dotnet-tools. That source has to be added to your project's .csproj file inside of a <RestoreAdditionalProjectSources> node.

Add the package package source to your Project.csproj
Reference: https://github.com/dotnet/aspnetcore/blob/main/NuGet.config


<PropertyGroup>
	<RestoreAdditionalProjectSources>
		https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json;
	</RestoreAdditionalProjectSources>
</PropertyGroup>

Add the service to your Program.cs

// Adds the CompilationService as a scoped service
builder.Services.AddCompilerService();

BasicBlazorExample from the demo project in this repo.

@page "/basic"

<h3>BasicBlazorExample</h3>

<div>
 <button @onclick=CompileAndRun>Run</button>
</div>
<div>
 <textarea style="width: 100%; min-height: 400px;" @bind-value="_textArea" @bind-value:event="oninput"></textarea>
</div>
@if (_compiledType != null)
{
 <DynamicComponent Type="@_compiledType"></DynamicComponent>
}
else if (_busy && _compileLog.Count == 0)
{
 <span style="padding: 4px;">Compiling...</span>
}
else if (_compileLog.Count > 0)
{
 <pre style="padding: 4px;">
 @string.Join("\r\n", _compileLog)
 </pre>
}
else
{
 <div>Click `Run` to compile the code in the text area.</div>
}

@code {
 [Inject]
 CompilationService _compilationService { get; set; } = default!;

 Type? _compiledType = null;
 List<string> _compileLog = new List<string>();
 bool _busy = false;
 string _textArea = @"<div>Hello World! It is @DateTime.Now.ToString(""s"")</div>";

 async Task CompileAndRun()
 {
 _compiledType = null;
 if (_busy) return;
 _compileLog.Clear();
 try
 {
 _busy = true;
 StateHasChanged();
 _compiledType = await _compilationService.CompileRazorCodeToBlazorComponentType(_textArea, async msg =>
 {
 _compileLog.Add(msg);
 StateHasChanged();
 await Task.Delay(100);
 });
 }
 catch (Exception ex)
 {
 Console.WriteLine(ex.Message);
 Console.WriteLine(ex.StackTrace);
 }
 finally
 {
 _busy = false;
 }
 StateHasChanged();
 }
}

Thanks to:

Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 219 3/14/2025
0.0.17 169 2/15/2025
0.0.16 170 2/15/2025