agracio / edge-js

Run .NET and Node.js code in-process on Windows, macOS, and Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trying to use System.DirectoryServices.AccountManagement in Node.js

corey-Robinson1337 opened this issue · comments

I have an electron app where I would like to grab the email of the current user automatically. To do this I'm using edge-js,

however, after I get everything set up I get this error --

PS C:\Users\crobinson\Desktop\git_repos\electron-edge-js-quick-start> npm start

> electron-edge-js-quick-start@1.0.0 start
> electron --core .
C:\Users\crobinson\Desktop\git_repos\electron-edge-js-quick-start\src\QuickStart.Core\bin\Debug\net8.0
username crobinson
Platform: Win32NT
Version: 10.0.22000.0
Service Pack:
Error: System.DirectoryServices.AccountManagement is not supported on this platform.

this is the relevant snippet of C# code

using System;
using System.Threading.Tasks;
using System.DirectoryServices.AccountManagement;
using System.Reflection.PortableExecutable;
namespace QuickStart.Core
{
    public class LocalMethods
    {
        public async Task<object> GetAppDomainDirectory(dynamic input)
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public async Task<object> GetCurrentTime(dynamic input)
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public async Task<object> UseDynamicInput(dynamic input)
        {
            return $".NET Core welcomes {input}";
        }

        public async Task<object> ThrowException(dynamic input)
        {
            throw new Exception("Sample Exception");
        }
        public async Task<object> GetCurrentUserEmail(dynamic input)
        {
            try
            {
                Console.WriteLine("username "+Environment.UserName);
                OperatingSystem os = Environment.OSVersion;

                Console.WriteLine($"Platform: {os.Platform}");
                Console.WriteLine($"Version: {os.Version}");
                Console.WriteLine($"Service Pack: {os.ServicePack}");
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
                {
                    UserPrincipal user = UserPrincipal.FindByIdentity(context, Environment.UserName);

                    if (user != null)
                    {
                        return user.EmailAddress;
                    }
                }
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }

            return null;
        }
    }
}

and this is how I'm calling it in my node js application

// main.js
const { app, BrowserWindow, Tray, screen, nativeImage } = require('electron');
//const electron = require('electron');
const path = require('path');
const os = require('os');
const configFile = path.join(__dirname, 'config.json');
const fs = require('fs');

let mainWindow;
let tray;

let namespace = "QuickStart.Core"
const baseNetAppPath = path.join(__dirname, '\\src\\QuickStart.Core\\bin\\Debug\\net8.0');

process.env.EDGE_USE_CORECLR = 1;
process.env.EDGE_APP_ROOT = baseNetAppPath;

var edge = require('electron-edge-js');

var baseDll = path.join(baseNetAppPath, namespace + '.dll');
var localTypeName = 'QuickStart.Core.LocalMethods';
var externalTypeName = 'QuickStart.Core.ExternalMethods';
var getAppDomainDirectory = edge.func({
  assemblyFile: baseDll,
  typeName: localTypeName,
  methodName: 'GetAppDomainDirectory'
});
getAppDomainDirectory('', function(error, result) {
  if (error) throw error;
  console.log(result);
});


var GetCurrentUserEmail = edge.func({
  assemblyFile: baseDll,
  typeName: localTypeName,
  methodName: 'GetCurrentUserEmail'
});
GetCurrentUserEmail('', function(error, result) {
  if (error) throw error;
  console.log(result);
});

This seems odd because I am on Windows and the C# code even confirms this is the case.

Probably because underlying edge-js project compiled for netcoreapp1.1, not 100% sure.

It could be related to #191 take a look at solution provided there.