如何通过Node.js Web API调用C# DLL的带返回值函数?
实现Node.js Web API调用C#带返回值DLL的方案
方案一:用Edge.js直接在Node.js中调用C# DLL
Edge.js支持Node.js直接运行.NET代码、调用.NET DLL,适合轻量场景。
步骤:
- 安装Edge.js依赖:
npm install edge-js
- 假设你的C# DLL(比如
MyFunctions.dll)里有如下带返回值的函数:
namespace MyDLL { public class InfoGetter { public static string GetInfo(string input) { // 替换为你的业务逻辑,返回处理后的结果 return $"Processed info: {input}"; } } }
- 在Node.js Web API(以Express框架为例)中实现
/getinfo接口:
const express = require('express'); const edge = require('edge-js'); const app = express(); app.use(express.json()); // 加载C# DLL并绑定目标函数 const getInfoFromDLL = edge.func({ assemblyFile: './MyFunctions.dll', typeName: 'MyDLL.InfoGetter', methodName: 'GetInfo' // 指定要调用的DLL函数名 }); app.get('/getinfo', (req, res) => { const inputParam = req.query.input || 'default'; // 异步调用DLL函数 getInfoFromDLL(inputParam, (error, result) => { if (error) { return res.status(500).json({ error: error.message }); } res.json({ data: result }); }); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
注意:若DLL基于.NET Framework编写,需保证Node.js运行环境与.NET版本兼容;若为.NET Core/.NET 5+,选择对应版本的
edge-js即可。
方案二:将C# DLL封装为.NET Web API,Node.js通过HTTP调用
如果直接调用存在兼容性问题,或需要更稳定的服务化架构,可先把C# DLL封装成独立的.NET Web API,再让Node.js通过HTTP请求转发调用。
步骤:
- 创建.NET Web API项目:
- 新建ASP.NET Core Web API项目,引用你的C# DLL。
- 编写接口暴露DLL函数:
[ApiController] [Route("api/dll")] public class DllController : ControllerBase { [HttpGet("getinfo")] public IActionResult GetInfo([FromQuery] string input) { var result = MyDLL.InfoGetter.GetInfo(input); return Ok(new { data = result }); } }
- 发布并运行该.NET Web API,假设服务地址为
http://localhost:5000/api/dll/getinfo。 - 在Node.js的Web API中转发请求:
const express = require('express'); const axios = require('axios'); const app = express(); app.use(express.json()); app.get('/getinfo', async (req, res) => { try { const inputParam = req.query.input || 'default'; const response = await axios.get('http://localhost:5000/api/dll/getinfo', { params: { input: inputParam } }); res.json(response.data); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(3000, () => { console.log('Node.js API running on port 3000'); });
该方案兼容性更强,适合生产环境,实现了.NET代码与Node.js代码的解耦,便于各自维护。
方案三:将C# DLL转为非托管导出(进阶)
若对性能要求较高,可使用DllExport工具将C#函数导出为非托管函数,再通过node-ffi-napi在Node.js中调用。
步骤:
- 在C#项目中安装
DllExportNuGet包,配置导出函数:
using RGiesecke.DllExport; using System.Runtime.InteropServices; namespace MyDLL { public class InfoGetter { [DllExport("GetInfo", CallingConvention = CallingConvention.StdCall)] public static string GetInfo(string input) { return $"Processed info: {input}"; } } }
- 编译生成非托管可调用的DLL。
- 安装Node.js依赖:
npm install ffi-napi ref-napi
- 在Node.js中调用DLL:
const express = require('express'); const ffi = require('ffi-napi'); const ref = require('ref-napi'); const app = express(); app.use(express.json()); // 定义DLL函数签名 const myDll = ffi.Library('./MyFunctions.dll', { 'GetInfo': [ref.types.CString, [ref.types.CString]] }); app.get('/getinfo', (req, res) => { const inputParam = req.query.input || 'default'; try { const result = myDll.GetInfo(inputParam); res.json({ data: result }); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(3000, () => { console.log('Server running on port 3000'); });
此方案性能优势明显,但配置复杂度较高,需注意类型转换问题。
内容的提问来源于stack exchange,提问作者amit joshi
相关产品推荐
相关产品推荐

