Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron app with dotnet dynamic #415

Closed
dashkan opened this issue Jan 2, 2025 · 1 comment
Closed

Electron app with dotnet dynamic #415

dashkan opened this issue Jan 2, 2025 · 1 comment

Comments

@dashkan
Copy link

dashkan commented Jan 2, 2025

Great project. Keep up the great work!!

I got native aot working w/ electron without much fuss. Now I am trying to get dynamic dotnet working. I first hit the asar issue which makes sense as dotnet is not equipped to read files inside asar. I turned off asar in my electron-build config and now met with the following error:

> NativeHost.InitializeModule(13C000F00E0, 13C007506F0)
< NativeHost.InitializeModule()
> NativeHost.InitializeManagedHost(net8.0, /Users/ashkan/Projects/my-app/release/2.2.0/mac-arm64/my-app.app/Contents/Resources/app/node_modules/node-api-dotnet/net8.0/Microsoft.JavaScript.NodeApi.DotNetHost.dll)
    Initializing .NET 8.0
> NativeHost.InitializeManagedRuntime(/Users/ashkan/Projects/my-app/release/2.2.0/mac-arm64/my-app.app/Contents/Resources/app/node_modules/node-api-dotnet/net8.0/Microsoft.JavaScript.NodeApi.runtimeconfig.json)
> HostFxr.Initialize()
    .NET root: ./release/2.2.0/mac-arm64/my-app.app/Contents/Frameworks/dotnet.framework/
    HostFxr path: ./release/2.2.0/mac-arm64/my-app.app/Contents/Frameworks/dotnet.framework/host/fxr/8.0.11/libhostfxr.dylib
< HostFxr.Initialize()
    Initializing runtime...
< NativeHost.InitializeManagedRuntime()
    Getting runtime load-assembly delegate...
    Loading managed host type: Microsoft.JavaScript.NodeApi.DotNetHost.ManagedHost, Microsoft.JavaScript.NodeApi.DotNetHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Failed to initialize managed host: System.Exception: Failed to load managed host assembly. HRESULT: 0x80131509
   at Microsoft.JavaScript.NodeApi.DotNetHost.NativeHost.CheckStatus(HostFxr.hostfxr_status, String) + 0x278
   at Microsoft.JavaScript.NodeApi.DotNetHost.NativeHost.InitializeDotNetHost(Version, String, JSValue, JSValue) + 0x320
   at Microsoft.JavaScript.NodeApi.DotNetHost.NativeHost.InitializeManagedHost(JSCallbackArgs args) + 0x608
< NativeHost.InitializeManagedHost()

AOT typescript code that works

import { Example } from "aot-npm-package"

const result = Example.hello(".NET AOT")
// console.log(process.env)
console.log(result)

dotnet dynamic code that fails

import dotnet from "node-api-dotnet";
const Console = dotnet.System.Console;
Console.WriteLine('Hello from DYNAMIC .NET!');

A couple of notes:

  • Works fine w/ vite dev mode
  • I have tried using with and without setting DOTNET_ROOT. Same error except the path to .NET root is different.

Any help is greatly appreciated

@dashkan
Copy link
Author

dashkan commented Jan 3, 2025

Never mind.

I was able to solve my issue by using the following settings in electron-builder.json

  "asar": true,
  "extraResources": [
    {
      "from": "node_modules/node-api-dotnet",
      "to": "node_modules/node-api-dotnet",
      "filter": ["**/*"]
    }
  ],
  "files": [
    "dist-electron",
    "dist",
    {
      "filter": "!node_modules/node-api-dotnet/**/*"
    }
  ]

I explicitly exclude node_modules/node-api-dotnet and copy it to the resource directory using extraResources. I am also able to continue using asar.

I am also copying dotnet 8 framework into the output if anybody is interested in that:

  "mac": {
    "artifactName": "${productName}_${version}.${ext}",
    "extraFiles": [
      {
        "from": "dotnet/dotnet-sdk-8.0.404-osx-${arch}",
        "to": "Frameworks/dotnet.framework/dotnet-sdk-8.0.404-osx-${arch}",
        "filter": ["**/*"]
      }
    ],
    "target": [
      "dmg",
      "zip"
    ]
  },

@dashkan dashkan closed this as completed Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant