Nodejs 调用WCF实例
WCF的概念
1.Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分。由 .NET Framework 3.0 开始引入。
2.WCF的最终目标是通过进程或不同的系统、通过本地网络或是通过Internet收发客户和服务之间的消息。
3.WCF合并了Web服务、.net Remoting、消息队列和Enterprise Services的功能并集成在Visual Studio中。
4.WCF专门用于面向服务开发。
开启WCF
五种常用绑定常用绑定的传输协议以及编码格式
名称 | 传输协议 | 编码格式 | 互操作性 |
---|---|---|---|
BasicHttpBinding | HTTP/HTTPS | Text,MTOM | Yes |
NetTcpBinding | TCP | Binary | No |
NetNamedPipeBinding | IPC | Binary | No |
WSHttpBinding | HTTP/HTTPS | Text,MTOM | Yes |
NetMsmqBinding | MSMQ | Binary | No |
默认使用的是最简单的一种WCF绑定方式,采用自宿主方式,使用BasicHttpBinding方式进行WCF程序的发布
发布的WCF接口随便取了个名字,这个接口的目的是发送邮件,传入一些邮件服务器的参数和邮件内容,就可以根据配置发送邮件。
正式开始
在vs中,只需要添加服务应用,编辑器会自动生成接口供我们调用。那么我们现在没有编辑器,如何去调用?
其实调用WCF接口,和调用webservice一样,都是向接口发送一段XML,如何拼凑这一段XML使我们需要考虑的一件事情。
在vs中有一个小工具,就可以实现我们想要的东西,它叫wcftestclient。相信很多人都用过,但是不知道有没有注意到,在下方格式化边上还有XML的显示。
如何开启wcftestclient.exe
1.找到vs的安装路径
我自己的安装路径在:D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
2.在文件夹中找到wcftestclient.exe的文件
3.在我的服务上右击出现一个textbox上面输入自己的wcf地址
4.点击确定后出现自己的WCF服务
5.这个sendmail就是发送邮件的一个接口
6.点击下方的XML之后会显示这个工具帮我们生成的XML信息
7.代码
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IOtherService/SendMail</Action>
</s:Header>
<s:Body>
<SendMail xmlns="http://tempuri.org/">
<smtpInfo>qq.com</smtpInfo>
<Sender>sender@smtp.com</Sender>
<SenderPwd>password</SenderPwd>
<emailFrom>sender@smtp.com</emailFrom>
<emailSubject>主题</emailSubject>
<emailContent>内容</emailContent>
<annexFullPathList xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
<toUserList xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:string>sender@smtp.com</d4p1:string>
</toUserList>
<ccUserList xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
<result>false</result>
<resultMessage i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</SendMail>
</s:Body>
</s:Envelope>
8.其中的Header部分需要去除,如果在nodejs中拼接上Header时会报错。
9.在nodejs中安装wcf.js
npm install --save wcf.js
或者
cnpm install --save wcf.js
https://github.com/yaronn/wcf.js 这个是wcf.js的github地址,上面有样例。
10.js文件
var Proxy = require('wcf.js').Proxy;
var BasicHttpBinding = require('wcf.js').BasicHttpBinding;
var binding = new BasicHttpBinding();
/Ensure the proxy variable created below has a working wsdl link that actually loads wsdl
var proxy = new Proxy(binding, "http://192.168.216.221:8081/OtherService?wsdl");
/*Ensure your message below looks like a valid working SOAP UI request*/
var message = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
// "<s:Header>" +
// "<Action s:mustUnderstand='1' xmlns='http://schemas.microsoft.com/ws/2005/05/addressing/none'>http://tempuri.org/IOtherService/SendMail</Action>" +
// "</s:Header>" +
"<s:Body>" +
"<SendMail xmlns='http://tempuri.org/'>" +
"<smtpInfo>smtp服务器</smtpInfo>" +
"<Sender>发件人邮箱</Sender>" +
"<SenderPwd>发件人密码</SenderPwd>" +
"<emailFrom>发件人</emailFrom>" +
"<emailSubject>主题</emailSubject>" +
"<emailContent>Hello,我这个是用nodejs 调用 wcf 给你发送的邮件(V1.0)。 0v0</emailContent>" +
"<annexFullPathList xmlns:d4p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' />" +
"<toUserList xmlns:d4p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
"<d4p1:string>收件人邮箱</d4p1:string>" +
"</toUserList>" +
"<ccUserList xmlns:d4p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' />" +
"<result>false</result>" +
"<resultMessage i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' />" +
"</SendMail>" +
"</s:Body>" +
"</s:Envelope>";
/*The message that you created above, ensure it works properly in SOAP UI rather copy a working request from SOAP UI*/
/*proxy.send's second argument is the soap action; you can find the soap action in your wsdl*/
proxy.send(message, "http://tempuri.org/IOtherService/SendMail", function (response, ctx) {
console.log(response);
console.log("邮件发送完成");
/*Your response is in xml and which can either be used as it is of you can parse it to JSON etc.....*/
});
11.发送完成后或有提示
12.执行js文件/node js文件
发送成功,并且返回了一个response,包含了我wcf返回的一个**“true”**。
同时wcf中也输出了成功。也收到邮件了
好了以上就是我实践得出的一些结论,如果有什么问题,请联系13482198105@163.com,如果侵权,请删除。