[英]Azure Mobile Services An unhandled exception occurred. Error: One of your scripts caused the service to become unresponsive
为我的英语道歉。
我有一个节点js脚本,必须使用IoT中心将AMQP消息发送到设备。 我已经从azure iot的github上获取了此脚本。 这是这个样本。
这是基于此脚本的脚本:
console.log("creating the client");
var Client = require('azure-iothub').Client;
console.log("client has been created");
var Message = require('azure-iot-common').Message;
console.log("message has been created");
var connectionString = "HostName=id**.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=***;
console.log(connectionString);
var targetDevice = 'devicesergey';
var client = Client.fromConnectionString(connectionString);
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
}
else {
console.log('Client connected');
var data = JSON.stringify({ text : 'foo' });
var message = new Message(data);
console.log("json message is created")
console.log('Sending message: ' + message.getData());
client.send(targetDevice, message, printResultFor('send'));
console.log("message has been sent");
}
});
function printResultFor(op) {
return function printResult(err, res) {
if (err) {
console.log(op + ' error: ' + err.toString());
} else {
console.log(op + ' status: ' + res.constructor.name);
}
};
}
在本地运行正常,我在设备模拟器上看到消息。 但是,当我尝试将其放入Azure移动服务API并尝试运行它时,我在日志中看到以下消息:
发生未处理的异常。 错误:您的脚本之一导致服务无响应,并重新启动了服务。 这通常是由脚本执行无限循环或长时间的阻塞操作引起的。 脚本连续执行超过5000毫秒后,服务重新启动。 在process.EventEmitter.emit(events.js:126:20)上的Server._registerUncaughtExceptionListenerAndCreateHttpServer._onUncaughtException(D:\\ home \\ site \\ wwwroot \\ node_modules \\ azure-mobile-services \\ runtime \\ server.js:218:17) )
有时我看到此IIS错误,我确切地知道此行发生此功能:client.open(function ....我已经尝试仅离开client.open()并从该函数发送消息。在这种情况下,我看到“客户端未连接”。
我在github上问了这些东西。 他们建议我在这里问。 也许有人知道如何解决此问题(使用脚本或Azure)。 我会非常非常高兴!
谢谢!
移动服务自定义API是用于展示express.js库功能的脚本,请参阅官方文档“使用JavaScript后端移动服务” Overview of custom APIs
部分
我已成功复制了该问题。 我猜您的脚本没有作为主体块包装在下面的代码中,并且没有将响应发送给客户端(如浏览器)。
exports.get = function(request, response) {
// The body block
....
response.send(200, "<response-body>");
}
有关移动服务自定义API的更多详细信息,请参阅https://msdn.microsoft.com/library/azure/dn280974.aspx 。
更新 :
为了方便测试,我如下更改了api的权限,然后可以使用浏览器访问api链接https://<mobile-service-name>.azure-mobile.net/api/test
。
我刚刚尝试在新的Azure MS上执行脚本,但未成功。 我将编写分步操作,也许您会发现任何错误,因为我对NodeJS的了解不佳。
添加一个新的API“ dev”。 访问-每个人都可以。 这是源代码:
exports.get = function(request, response) { console.log("creating the client"); var Client = require('azure-iothub').Client; console.log("client has been created"); var Message = require('azure-iot-common').Message; console.log("message has been created"); var connectionString = "HostName=i***.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey***"; console.log(connectionString); var targetDevice = 'devicesergey'; var client = Client.fromConnectionString(connectionString); client.open(function (err) { if (err) { console.error('Could not connect: ' + err.message); } else { console.log('Client connected'); var data = JSON.stringify({ text : 'foo' }); var message = new Message(data); console.log("json message is created") console.log('Sending message: ' + message.getData()); client.send(targetDevice, message, printResultFor('send')); console.log("message has been sent"); } }); response(200, "Hello, world!"); }; function printResultFor(op) { return function printResult(err, res) { if (err) { console.log(op + ' error: ' + err.toString()); } else { console.log(op + ' status: ' + res.constructor.name); } }; }
如果我尝试执行这些内容,则会出现“ no azure-iothub”和“ no azure-iot-common”,因此我需要使用git来添加这些npm。
输入“ API”文件夹并添加NPM:
然后我执行“重新扫描”,“保存更改”,“提交”,“推送”
完成这些操作后,我通过路径“ http:// id **。mobile-services.net/api/dev”执行脚本,看不到任何东西,看到错误“ 500.1013”,并且这些消息在日志中显示(id取决于) :
发生未处理的异常。 错误:您的脚本之一导致服务无响应,并重新启动了服务。 这通常是由脚本执行无限循环或长时间的阻塞操作引起的。 脚本连续执行超过5000毫秒后,服务重新启动。 在process.EventEmitter.emit(events.js:126:20)上的Server._registerUncaughtExceptionListenerAndCreateHttpServer._onUncaughtException(D:\\ home \\ site \\ wwwroot \\ node_modules \\ azure-mobile-services \\ runtime \\ server.js:218:17) )
我不知道我在做什么错
更新:我试图使用Kudu控制台安装npms,它返回许多错误。 如果我正确找出,我需要更新我的节点js和npm。 但是我不知道该怎么做,也没有找到解决方案。 这是日志: 我缺乏声誉,因此不允许我过去使用日志脚本。
我已尝试执行以下操作,但无济于事:
在仓库的根目录下,您将找到一个.deployment文件,该文件具有:
命令= .. \\ ZumoDeploy.cmd更改为
命令= deploy.cmd并在其旁边创建一个deploy.cmd,其中包含:
set NPM_JS_PATH =%ProgramFiles(x86)%\\ npm \\ 1.4.9 \\ node_modules \\ npm \\ bin \\ npm-cli.js .. \\ ZumoDeploy.cmd提交文件并推送。
我糊涂了。 这怎么可能? Azure移动服务不允许安装azure-iot-hub npm)。 该问题我该怎么办?
UPDATE2: Peter Pan-MSFT,您建议我使用Kudu DebucConsole安装必要的npm。 但是,当我尝试这样做时-我看到了错误。
我已经将此消息告知github上的“ npm”命令,他们说Azure过去使用的npm版本完全不受支持。 htt ps://github.com/npm/npm/issues/12210#event-615573997
UPDATE3(04/12/2016)我已经通过不同的方式解决了这个问题。 创建了我自己的侦听端口的节点JS脚本,读取GET参数(deviceId和message)并发送D2C消息。 不幸的是,我仍然无法解决Azure问题。
UPDATE4 Peter Pan给了我一个建议,如何使用另一个版本的nodejs和npm。 现在,我已经成功安装了必要的NPM模块。 但是现在Azure移动脚本API无法正常工作,它显示了我尝试在浏览器中获取的任何脚本上的{“ code”:404,“ error”:“ Error:Not Found”}。
尝试执行这些操作时,也许删除了某些内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.