如何在 node.js 中使用 wcf? [英] how to consume wcf in node.js?
问题描述
我想在 node.js 中使用 wcf.我试过了:
I want to consume wcf in node.js. I tried it:
soap.createClient(url, function (err, client) {如果(错误){控制台日志(错误);返回假;}client.myFunc(args, function(err1, result) {如果(结果.成功)返回真;});});
但是在createClient(错误块)中发生了错误.它说: WSDL 或 include 的意外根元素.然后我通过 wcf.js 尝试:
But an error occurred in createClient (error block). it says: Unexpected root element of WSDL or include . Then I tried by wcf.js:
var BasicHttpBinding = require('wcf.js').BasicHttpBinding
, Proxy = require('wcf.js').Proxy
, binding = new BasicHttpBinding()
, proxy = new Proxy(binding, " http://localhost/myService.svc");
var message = '<Envelope xmlns=' +
'"http://schemas.xmlsoap.org/soap/envelope/">' +
'<Header />' +
'<Body>' +
'<myFunc xmlns="http://localhost/myService.svc/">' +
'<value>'+ args +'</value>' +
'</AddNewUser>' +
'</Body>' +
'</Envelope>';
proxy.send(message, "http://localhost/myService.svc", function (result, ctx){
if(result.success)
return true;
});
但是我的程序没有调用发送函数.最后,我尝试将 WCF 配置为 WSDL 发布,如下所示:WCF 无法配置 WSDL 发布
But my program didn't call send function. Finally I tried to configure WCF to WSDL publishing like this: WCF cannot configure WSDL publishing
但是没用!我该如何解决我的问题?
But it didn't work to! How can I solve my problem?
推荐答案
我遇到了这个问题,在我的例子中是因为响应被压缩了.npm 包soap 确实指定了'Accept-Encoding': 'none'
但SOAP 服务器(由我公司开发)表现不佳并发回一个gzipped 正文.肥皂包不处理这个.
I ran into this and in my case it's because the response is gzipped. The npm package soap does specify 'Accept-Encoding': 'none'
but the SOAP server (developed by my company) is not well behaved and sends back a gzipped body. The soap package doesn't handle this.
我正在考虑的另一种方法是在 createClient
的 options
参数中传递我自己的 httpclient
并解压缩它.有一个在 GitHub 上的 node-soap 代码测试中使用自定义 httpclient 的示例:https://github.com/vpulim/node-soap/blob/master/test/client-customHttp-test.js.
One alternative I'm looking at is to pass my own httpclient
in the options
parameter of createClient
and unzip it. There is an example of using a custom httpclient in the tests for the node-soap code on GitHub: https://github.com/vpulim/node-soap/blob/master/test/client-customHttp-test.js.
我还没有弄清楚如何解压回复,但我会在搞定后更新这个答案.
I haven't worked out how to unzip the response yet, but I'll update this answer once I work it out.
更新
对于 gzip 压缩的响应,它更简单.您可以将您想要的任何选项传递给 createClient
options
对象的 wsdl_options 属性中的节点 request
调用.这包括 gzip: true
,它将使 request
为您处理 gzip 压缩的请求.例如
For gzipped responses, it's simpler. You can pass any options you want to the node request
call in the wsdl_options property of the createClient
options
object. This includes gzip: true
, which will make request
handle gzipped requests for you. e.g.
soap.createClient('http://someservice.com/?wsdl',
{ wsdl_options: { gzip: true } },
function (err, client) {});
然后在进行 SOAP 方法调用时,将 gzip 参数添加到回调后的 options 参数中,例如
Then when making SOAP method calls, add the gzip parameter to the options argument after your callback e.g.
client.someSoapCall({arg1:"12345"},
function (err, result) {},
{ gzip: true });
我通过深入研究 node soap 代码发现了这一点.文档没有明确提到这些事情.
I figured this out by digging into the node soap code. The docs do not explicitly mention these things.
这篇关于如何在 node.js 中使用 wcf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!