TauriDotNetBridge 2.1.0
See the version list below for details.
dotnet add package TauriDotNetBridge --version 2.1.0
NuGet\Install-Package TauriDotNetBridge -Version 2.1.0
<PackageReference Include="TauriDotNetBridge" Version="2.1.0" />
paket add TauriDotNetBridge --version 2.1.0
#r "nuget: TauriDotNetBridge, 2.1.0"
// Install TauriDotNetBridge as a Cake Addin #addin nuget:?package=TauriDotNetBridge&version=2.1.0 // Install TauriDotNetBridge as a Cake Tool #tool nuget:?package=TauriDotNetBridge&version=2.1.0
Tauri DotNet Bridge
Enables implementation of Tauri commands in DotNet
Getting Started
The initial setup might seem a bit complicated, but it's actually quite simple.
First, create a new Tauri app as usual:
pnpm create tauri-app
pnpm i
In the root of your project, create a folder called src-dotnet
alongside src-tauri
.
Inside src-dotnet
, create a .NET class library, e.g.:
cd src-dotnet
dotnet new classlib --name MyApp.TauriPlugIn
cd ..
Make sure the name of your class library ends with .TauriPlugIn
.
Add the following PropertyGroup to the .csproj
file:
<PropertyGroup>
<OutputPath>..\..\src-tauri\target\$(Configuration)\dotnet</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Add the TauriDotNetBridge
and Microsoft.Extensions.DependencyInjection
NuGet packages:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="TauriDotNetBridge" Version="2.0.0" />
</ItemGroup>
Create a plugin to register your controllers:
public class PlugIn : IPlugIn
{
public void Initialize(IServiceCollection services)
{
services.AddSingleton<HomeController>();
}
}
Add a sample controller like this:
using TauriDotNetBridge.Contracts;
public class LogInInfo
{
public string? User { get; set; }
public string? Password { get; set; }
}
public class HomeController
{
public string Login(LogInInfo loginInfo)
{
return RouteResponse.Ok($"User '{loginInfo.User}' logged in successfully");
}
}
Build the .NET project to verify the changes and ensure the C# DLLs are copied to the Tauri target folder.
In src-tauri/Cargo.toml
, add:
[dependencies]
tauri-dotnet-bridge-host = "0.7.0"
And then configure Tauri in your main.rs
or lib.rs
as follows:
use tauri_dotnet_bridge_host;
#[tauri::command]
fn dotnet_request(request: &str) -> String {
tauri_dotnet_bridge_host::process_request(request)
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![dotnet_request])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Run cargo build in src-tauri
to verify the changes.
To call call your DotNet controllers in TypeScript, you can use the following code snippet:
import { invoke } from '@tauri-apps/api/core'
export type PluginRequest = {
controller: string
action: string
data?: object
}
export type RouteResponse<T> = {
errorMessage?: string
data?: T
}
export class TauriApi {
public static async invokePlugin<T>(request: PluginRequest): Promise<T | null> {
let response = (await invoke('dotnet_request', { request: JSON.stringify(request) })) as string
let jsonResponse = JSON.parse(response) as RouteResponse<T>
if (jsonResponse.errorMessage) throw new Error(jsonResponse.errorMessage)
return jsonResponse.data ?? (null as T | null)
}
}
And then use use it like this:
async function login(user: string): Promise<string | null> {
let userData = { user: user, pass: '<secret>' }
return await TauriApi.invokePlugin<string>({ controller: 'home', action: 'login', data: userData })
}
In plain JavaScript you can call a controller like this:
async function login() {
const response = await invoke('dotnet_request', {
request: JSON.stringify({ controller: 'home', action: 'login', data: { user: name.value, password: '<secret>' } })
})
greetMsg.value = JSON.parse(response).data
}
Finally, run the app
pnpm run tauri dev
and enjoy coding a tauri-app with DotNet backend 😊
Packaging
Make sure the dotnet plugin is built in release
cd src-dotnet
dotnet build -c Release
cd ..
Then build the tauri packages
pnpm run tauri build
To redistribute the package you can now copy the generated rust executable and the "dotnet" folder.
Sample project
A sample project can be found here: https://github.com/plainionist/TauriNET
Credits
Inspired by: https://github.com/RubenPX/TauriNET
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.