BlazorJSInterop.SourceGenerator 6.0.0-RC2

This is a prerelease version of BlazorJSInterop.SourceGenerator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package BlazorJSInterop.SourceGenerator --version 6.0.0-RC2
                    
NuGet\Install-Package BlazorJSInterop.SourceGenerator -Version 6.0.0-RC2
                    
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="BlazorJSInterop.SourceGenerator" Version="6.0.0-RC2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorJSInterop.SourceGenerator" Version="6.0.0-RC2" />
                    
Directory.Packages.props
<PackageReference Include="BlazorJSInterop.SourceGenerator" />
                    
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 BlazorJSInterop.SourceGenerator --version 6.0.0-RC2
                    
#r "nuget: BlazorJSInterop.SourceGenerator, 6.0.0-RC2"
                    
#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 BlazorJSInterop.SourceGenerator@6.0.0-RC2
                    
#: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=BlazorJSInterop.SourceGenerator&version=6.0.0-RC2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BlazorJSInterop.SourceGenerator&version=6.0.0-RC2&prerelease
                    
Install as a Cake Tool

BlazorJSInterop

This is a Blazor library that uses .NET source generators in order to create implementations of JS Service files and include them into the dependency injection collection.

Using the library

  1. Include the source generator as a dependency into your Blazor project file.
dotnet add package BlazorJSInterop.SourceGenerator --version 6.0.0-RC2
  1. Prototype your service interfaces using the BlazorJSInteropSource attribute in the interface and the BlazorJSInteropMethod attributes in all its methods. All methods in the interfaces must return ValueTask or ValueTask<T> and all the method attributes must include the JS name function as a parameter.

For example:

[BlazorJSInteropSource]
public interface IJSService
{
    [BlazorJSInteropMethod("showHelloWorldAlert")]
    ValueTask ShowHelloWorldAlert();

    [BlazorJSInteropMethod("showNamePrompt")]
    ValueTask<string> ShowNamePrompt(string title);

    [BlazorJSInteropMethod("showNameAlert")]
    ValueTask ShowNameAlert(string name);
}
  1. In your Program.cs file include the following using statement:
using BlazorJSInterop.SourceGenerator.Extensions;
  1. In your Main method in Program.cs include the following line builder.AddJSInteropImplementations(Assembly.GetExecutingAssembly()); such as:
public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);

    // Add this line to register the generated code into the dependency injection collection.
    builder.AddJSInteropImplementations(Assembly.GetExecutingAssembly());

    builder.RootComponents.Add<App>("app");

    builder.Services.AddTransient(sp => new HttpClient
        {BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)});

    await builder.Build().RunAsync();
}
  1. Inject your interface into any of your files as you would do with any other service.

Testing the library locally as a project reference

  1. If you want to test this project locally as a project reference, it is important to keep ReferenceOutputAssembly to true as there are some dependencies of this assembly that will be needed in your project.
<ProjectReference Include="..\..\src\BlazorJSInterop.SourceGenerator\BlazorJSInterop.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  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
6.0.10 627 10/12/2022
6.0.9 590 9/14/2022
6.0.8 561 8/21/2022
6.0.7 561 8/5/2022
6.0.6 592 7/10/2022
6.0.5 606 5/24/2022
6.0.4 602 4/13/2022
6.0.3 595 3/28/2022
6.0.2 594 2/13/2022
6.0.2-RC1 298 2/12/2022
6.0.1 450 12/22/2021
6.0.1-RC1 308 12/21/2021
6.0.0 430 12/21/2021
6.0.0-RC2 314 12/20/2021
6.0.0-RC1 798 12/1/2021
1.0.0 702 1/5/2021
0.1.0 551 12/15/2020

Added support for .NET 6. Version aligned with ASP.NET Core. Fixed generator not being packed in RC1