【问题标题】:How to call dll methods in a node.js application? (Parameter count mismatch)如何在 node.js 应用程序中调用 dll 方法? (参数计数不匹配)
【发布时间】:2021-01-01 13:28:37
【问题描述】:

我正在尝试使用我的 node.js 应用程序中的 c# .dll。我正在使用edge-js library 来完成此操作。

我能够加载 dll,但无法调用它的方法。 我得到的错误是

错误:参数计数不匹配。 匿名时间:1:55

如果有人能解释边缘绑定/参数传递的工作原理,将不胜感激。

dll代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace PortableClassLibrary1
{
    public class Class1
    {
        public String helloworld(){
            Debug.WriteLine("Hello dll world");  
            return("Hello dll World!");
        }

    }
}

这是我的(简化的)node.js 代码:

"use strict";
const express = require("express");
const router = express.Router();
const { Console } = require("console");

router.get("/", (req, res) => {
 var edge = require("edge-js");
  var helloDll = edge.func({
    assemblyFile: "bin/PortableClassLibrary1.dll",
    typeName: "PortableClassLibrary1.Class1",
    methodName: "helloworld",
  });
  helloDll(null, function (error, result) {
    if (error) throw error;
    console.log(result);
  });

});


module.exports = router;

我也尝试过同步调用:

  var returnResult = helloDll(true);
  var returnResult = helloDll(null, true);

结果相同。

我查看了这些链接,但没有帮助。

那么怎么样呢?有人知道如何使用 edge-js 调用 .dll 方法吗?

【问题讨论】:

  • 您正在调用实例方法。您需要一个实例来调用它(这可能是缺少的参数) 不知道它是如何与这个库一起工作的。尝试将其设为静态方法,看看会发生什么
  • 我将其设为静态并得到不同的错误:System.InvalidOperationException 无法访问 CLR 方法以通过反射进行包装。确保它是一个公共实例方法

标签: c# node.js dll edgejs


【解决方案1】:

dll中的“helloworld”方法应该返回一个Task并接受一个输入参数。

我已经修改了如下代码,它对我有用。

    public async Task<object> helloworld(dynamic input)
    {
        Debug.WriteLine("Hello dll world");
        // Ignore the compiler warning about await keyword as this just a demo code..
        return "Hello from.NET world !!";
    }

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多