Nodejs调用.Net Demo,使用Edge

1,697 阅读1分钟

Nodejs 调用.Net代码,使用Edge-js进行调用

Edge-js 发送MSMQ

npm install edge-js --save
var edge = require('edge-js')

Nodejs 调用.Net中MsMqDemo.dll进行发送MQ

function pushMSMQ(){
    console.log('-------------start------------------')
    let msmqPath='.\\private$\\killf';
    //let msmqPath="FormatName:DIRECT=TCP:*\\private$\\mq";
	
	//调用.net中MsMqDemo.dll,进行发送消息
    var msmq = edge.func({
      assemblyFile:"./dll/MsMqDemo.dll",
      typeName:"MsMqDemo.msmq",
      methodName:'pubMsg'});        
	  
    let body={
      msmqPath:msmqPath,
      msg:'nodejs connecet .net'
    };

    msmq(body,function (error, result) {
      if (error) throw error;
      console.log(result);
    });            

    console.log('-------------end------------------') 
}

.Net代码,发送MSMQ

using System;
using System.Threading.Tasks;
using System.Messaging;
using System.IO;

namespace MsMqDemo
{
    public class msmq
    {
        public  object pubMsg(dynamic body)
        {
            System.Object obj = null;
            try
            {
                MessageQueue mq;
                string path = (string)body.msmqPath;
                string input = (string)body.msg;
                //string path = ".\\private$\\killf";
                //访问远程的MSMQ,不能使用Exists和Create,注释以下代码 
                if (MessageQueue.Exists(path))
                {
                    mq = new MessageQueue(path);
                }
                else
                {
                    mq = MessageQueue.Create(path);
                }
                mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                mq.Send(input);
                obj = input;
                return obj;
            }
            catch (Exception ex) {
                obj = ex.Message;
                return obj;
            }
        }
    }
}