Skip to content

Example to execute .NET Core dll from Rust & Tauri

License

Notifications You must be signed in to change notification settings

RubenPX/TauriNET

Repository files navigation

Tauri.NET

This template should help get you started developing with Tauri, .NET and Vite.

TODOS

  • Call C# code from Tauri
  • Call C# code from tauri with parameters and return string
  • Allow comunication betwen JSON data
  • Call Tauri code from C#
  • Hide exposed dlls to make a single exe (Embed files)

Debuging

Debug screenshot

You can debug .net dll ataching VisualStudio to process to loaded tauri app

How it works

Comunication betwen frameworks

Comunication betwen framewowrks are transitive and it's defined using Rust code.

Rust manage comunication betwen frameworks

Show me the code

You can replace frontend as you want

To invoke NetHost code, you need install tauri API and run this code (It's an example)

invocation in clientside

import { TauriLib } from "./TauriComunication";

async function login(user: string): Promise<string | null> {
    let userData = {user: user, pass: "Hmm..."}

    try {
      return await TauriLib.invokePlugin<string>({ plugin: "TestApp", method: "login", data: userData });
    } catch (error) {
      return "ERR: " + error;
    }
}

let message = await login("RubenPX")

invocation in backend

public static class Main {

   class User {
      public string user { get; set; }
      public string pass { get; set; }
   }

   [RouteMethod] // <-- This is required
   public static RouteResponse login(RouteRequest request, RouteResponse response) {
      User? loginInfo = null;

      if (request.data != null) {
          try {
              loginInfo = Utils.ParseObject<User>(request.data);
          } catch (Exception ex) {
              return response.Error($"Failed to parse JSON User object: {ex.Message}");
          }
      }

      if (loginInfo == null || loginInfo.user == "" || loginInfo.user == null) {
          return response.Error("Woops... User is empty");
      }

      return response.Ok($"Loged in {loginInfo.user}");
   }
}

Working enviroment example

[✔] Environment
    - OS: Windows 10.0.22631 X64
    ✔ WebView2: 122.0.2365.66
    ✔ rustc: 1.74.1 (a28077b28 2023-12-04)
    ✔ cargo: 1.74.1 (ecb9851af 2023-10-18)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 21.6.2
    - pnpm: 8.6.1
    - npm: 9.6.6

[-] Packages
    - tauri [RUST]: 1.6.1
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.7
    - @tauri-apps/api [NPM]: 1.5.3
    - @tauri-apps/cli [NPM]: 1.5.10

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - bundler: Vite

Latest working build example: Download

References