Rhino 8 Rhino.Inside console app not launching in .net 7.0 (possible bug)

I have been trying to get Rhino 8 to run in Rhino.Inside mode as part of a unit testing project, and have failed to get it to work when the tests run on the .Net 7 runtime.

I have replicated the issue in a small console app project that is very similar to the rhino developer samples on Github but with extra console logging (see attached).

This is the output when run on .net framework (it works):

App starts
CLR version: 4.0.30319.42000
Rhinocommon info: RhinoCommon, Version=8.15.25019.13001, Culture=neutral, PublicKeyToken=552281e97c755530
Resolver.FindRhinoSystemDirectory: C:\Program Files\Rhino 8\System
HostUtils.RhinoAssemblyDirectory: C:\Program Files\Rhino 8\System
About to call RhinoCore ctor
RhinoCore ctor returns successfully
Running in netfx
Created sphere mesh, total vertices = 8385

This is the output when run on .net 7 runtime (it crashes when call the RhinoCore constructor):

App starts
CLR version: 7.0.20
Rhinocommon info: RhinoCommon, Version=8.15.25019.13001, Culture=neutral, PublicKeyToken=552281e97c755530
Resolver.FindRhinoSystemDirectory: C:\Program Files\Rhino 8\System
HostUtils.RhinoAssemblyDirectory: C:\Program Files\Rhino 8
HostUtils.RhinoAssemblyDirectory != Resolver.RhinoSystemDirectory
About to call RhinoCore ctor

C:\path\to\rhino-inside-bug\RhinoInConsole\bin\Release\net7.0-windows\RhinoInConsole.exe (process 43556) exited with code 3 (0x3).

I noticed that HostUtils.RhinoAssemblyDirectory pointed one directory up when in .net 7 mode, which doesn’t seem correct.

Is there anything I am missing, or is this a bug?

rhino-inside-bug.zip (3.6 KB)

Got it working, quite a few extra steps:

Added UseWPF to project file

<UseWPF>true</UseWPF>

Added Rhino System Directory to PATH environment variable in Resolver.Initialize():

public static void Initialize()
{
    if (System.IntPtr.Size != 8)
        throw new Exception("Only 64 bit applications can use RhinoInside");
#if NET7_0_OR_GREATER
    var PATH = Environment.GetEnvironmentVariable("PATH");
    Environment.SetEnvironmentVariable("PATH", PATH + ";" + RhinoSystemDirectory);
#endif
    AppDomain.CurrentDomain.AssemblyResolve += ResolveForRhinoAssemblies;
}

Prioritise resolving from the netcore subdirectory in Rhino (I also added some Console.Writelines):

static Assembly ResolveForRhinoAssemblies(object sender, ResolveEventArgs args)
{
    var assemblyName = new AssemblyName(args.Name).Name;
    Console.WriteLine($"Resolving {assemblyName}");

    string path;
#if NET7_0_OR_GREATER
    path = System.IO.Path.Combine(RhinoSystemDirectory, "netcore", assemblyName + ".dll");
    if (System.IO.File.Exists(path))
    {
        Console.WriteLine($"Found at {path}");
        return Assembly.LoadFrom(path);
    }
#endif
    path = System.IO.Path.Combine(RhinoSystemDirectory, assemblyName + ".dll");
    if (System.IO.File.Exists(path))
    {
        Console.WriteLine($"Found at {path}");
        return Assembly.LoadFrom(path);
    }
    Console.WriteLine($"Not found {assemblyName}");
    return null;
}

Feels like quite the workaround - took a bit of debugging to figure that out.