打包的.net-core电子应用程序无法运行

时间:2019-08-25 19:41:37

标签: asp.net-core asp.net-core-mvc electron electron-builder electron-packager

我正在尝试使用.net-core创建一个电子应用程序。它在所有步骤中都运作良好,并且在我测试和调试应用程序时完全没有错误。 但是不幸的是,当我使用电子打包程序打包应用程序时,执行.exe文件时出现此错误。

  

在主进程未捕获的异常中发生了javaScript错误:   TypeError [ERR_INVALID_ARG_VALUE]:参数'args'无效。   已收到{cwd:   'D:\ win \ release-builds \ gen \ resources \ app.asar \ bin'}       在Object.execFile(child_process.js:218:11)       在Object.module。(匿名函数)作为execFile       在exec(child_process.js:160:18)       在ELECTRON_ASAR.js:746:23       在portfinder上(D:\ win \ release-builds \ gen \ resources \ app.asar \ main.js:122:22)       在听D:....       在Server.server.listen D:....       在Object.onceWrapper(events.js:273:13)       在Server.emit(events.js:182:13)       在emitListeningNT(net.js:1364:10)

这是我的main.js文件的内容:

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        console.log('spawn called');
        console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();
const { app } = require('electron');
const { BrowserWindow, dialog, shell } = require('electron');
const fs = require('fs');
const path = require('path');
const process1 = require('child_process').exec;
const portfinder = require('detect-port');
let io, browserWindows, ipc, apiProcess, loadURL;
let appApi, menu, dialogApi, notification, tray, webContents;
let globalShortcut, shellApi, screen, clipboard;
let splashScreen, mainWindowId;

process.env.NODE_ENV = 'development';

const manifestJsonFilePath = path.join(__dirname, 'bin', 'electron.manifest.json');
const manifestJsonFile = require(manifestJsonFilePath);
if (manifestJsonFile.singleInstance) {
    const shouldQuit = app.requestSingleInstanceLock();
    app.on('second-instance', (commandLine, workingDirectory) => {
        mainWindowId && BrowserWindow.fromId(mainWindowId) && BrowserWindow.fromId(mainWindowId).show();
    });

    if (shouldQuit) {
        app.quit();
    }
}

app.on('ready', () => {
    if (isSplashScreenEnabled()) {
        startSplashScreen();
    }

    portfinder(8000, (error, port) => {
        startSocketApiBridge(port);
    });
});

function isSplashScreenEnabled() {
    return Boolean(manifestJsonFile.loadingUrl);
}

function startSplashScreen() {
    let loadingUrl = manifestJsonFile.loadingUrl;
    let icon = manifestJsonFile.icon;

    if (loadingUrl) {
        splashScreen = new BrowserWindow({
            width: manifestJsonFile.width,
            height: manifestJsonFile.height,
            transparent: true,
            frame: false,
            show: false,
            icon: path.join(__dirname, icon)
        });

        if (manifestJsonFile.devTools) {
            splashScreen.webContents.openDevTools();
        }

        splashScreen.loadURL(loadingUrl);
        splashScreen.once('ready-to-show', () => {
            splashScreen.show();
        });

        splashScreen.on('closed', () => {
            splashScreen = null;
        });
    }
}

function startSocketApiBridge(port) {
    io = require('socket.io')(port);
    startAspCoreBackend(port);

    io.on('connection', (socket) => {
        global['electronsocket'] = socket;
        global['electronsocket'].setMaxListeners(0);
        console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());

        appApi = require('./api/app')(socket, app);
        browserWindows = require('./api/browserWindows')(socket, app);
        ipc = require('./api/ipc')(socket);
        menu = require('./api/menu')(socket);
        dialogApi = require('./api/dialog')(socket);
        notification = require('./api/notification')(socket);
        tray = require('./api/tray')(socket);
        webContents = require('./api/webContents')(socket);
        globalShortcut = require('./api/globalShortcut')(socket);
        shellApi = require('./api/shell')(socket);
        screen = require('./api/screen')(socket);
        clipboard = require('./api/clipboard')(socket);

        if (splashScreen && !splashScreen.isDestroyed()) {
            splashScreen.close();
        }
    });
}

function startAspCoreBackend(electronPort) {
    portfinder(8000, (error, electronWebPort) => {
        loadURL = `http://localhost:${electronWebPort}`
        const parameters = [`/electronPort=${electronPort}`, `/electronWebPort=${electronWebPort}`];
        let binaryFile = manifestJsonFile.executable;

        const os = require('os');
        if (os.platform() === 'win32') {
            binaryFile = binaryFile + '.exe';
        }

        const binFilePath = path.join(__dirname, 'bin', binaryFile);
        var options = { cwd: path.join(__dirname, 'bin') };
        apiProcess = process1(binFilePath, parameters, options);

        apiProcess.stdout.on('data', (data) => {
            console.log(`stdout: ${data.toString()}`);
        });
    });
}

package.json文件的内容:

{
  "name": "XXXXXXXX",
  "version": "1.0.0",
  "description": "XXXXXXXXXXXXXXXXXXX",
  "main": "main.js",
  "author": "Gregor Biswanger",
  "license": "MIT",
  "scripts": {
    "start": "electron .",
    "package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds",
    "package-win": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\"",
    "package-linux": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=linux --arch=x64 --icon=assets/icons/png/1024x1024.png --prune=true --out=release-builds"
  },
  "dependencies": {
    "detect-port": "^1.2.3",
    "socket.io": "^2.1.1"
  },
  "devDependencies": {
    "@types/node": "^10.11.0",
    "@types/socket.io": "^1.4.38",
    "electron": "^3.1.13",
    "electron-packager": "^14.0.4",
    "tslint": "^5.11.0",
    "typescript": "^3.0.3"
  },
  "keywords": [
    "genetic"
  ]
}

和program.cs文件:

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseElectron(args)
        .Build();
}

0 个答案:

没有答案