【发布时间】:2020-05-11 15:39:54
【问题描述】:
我正在构建一个发布到 Azure Functions 的 API。我目前有两个工作函数,我写了第三个,部署后我无法工作。
当使用func start 在本地运行并将function.json 中的scriptFile 指向与我部署到Azure 相同的转译文件时,它可以工作。
这是我从函数运行时下载内容时的样子:
├── host.json
├── node_modules/
├── FunctionA
│ ├── function.json
│ ├── index.js
│ └── index.js.map
├── proxies.json
├── FunctionB
│ ├── function.json
│ ├── index.js
│ └── index.js.map
└── FunctionC
├── function.json
├── index.js
└── index.js.map
FunctionA 和 FunctionB 工作。 FunctionC 的主要区别在于此函数依赖于 node_modules 依赖项 @azure/cosmos。
我以相同的方式构建所有功能,npx parcel build index.ts --target node。根据包裹文档:
目标节点和电子不会捆绑 package.json 的依赖项
正是我想要的,因为 developer guidelines 状态是您使用 Javascript 在 Azure 函数中处理依赖项的方式:
- 通过运行 npm install 在本地安装所有必需的包。
- 部署您的代码,并确保 node_modules 文件夹包含在部署中。
我提供的node_modules 文件夹是运行npm install --production 产生的文件夹,因为我不想要其他依赖项。
但是当我在Azure Portal中查看这个功能时,进入代码视图时看到如下错误:
函数 (FunctionC) 错误:无法确定要调用哪个 ctor。
函数本身在 TypeScript 中如下所示:
// Omitted imports
const functionC: AzureFunction = async function (context: Context, events: CustomEvent[]): Promise<void> {
try {
// Extract data about Cosmos database and container
const cosmosOutBinding = await FunctionUtil.getBindingByName(context.executionContext.functionDirectory, "documentOut")
const cosmosConnection: CosmosDB = new CosmosDB(process.env.COSMOSDB_CONNECTION_STRING, cosmosOutBinding.databaseName)
const updateContext: UpdateContext = new UpdateContext(context, cosmosConnection, cosmosOutBinding.collectionName)
await updateContext.handleEvents(events)
} catch (error) {
context.log.error('Failed', error)
}
}
export default functionC
UpdateContext 是一个导入类,包含函数的主要逻辑。我以这种方式构建它以使其可测试。
最后,这个函数的绑定:
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": "some-hub",
"connection": "EVENT_HUB_CONNECTION_STRING",
"cardinality": "many",
"consumerGroup": "events"
},
{
"name": "documentOut",
"type": "cosmosDB",
"databaseName": "dev",
"collectionName": "asdf",
"createIfNotExists": false,
"connectionStringSetting": "COSMOSDB_CONNECTION_STRING",
"direction": "out"
}
]
}
我无法理解这里出了什么问题。任何指向正确方向的指针将不胜感激。
【问题讨论】:
-
我以前见过这个
function.json不完整,尝试将"createLeaseCollectionIfNotExists": true添加到您的 Cosmos 输出绑定中。 -
@evilSnobu 谢谢,但该属性不在 json 模式中(我认为它是
createIfNotExists)。我不认为是这样。我在其他函数中使用 cosmos 作为 in 和 out 的同一个集合。 -
我发现了问题...我们使用 terraform 创建基础架构,而 terraform 默认为函数运行时的版本 1。我在门户中创建了一个新的函数应用程序,默认为 3。部署到手动创建的运行时工作。
-
您可能仍应在github.com/Azure/Azure-Functions 上报告此问题。似乎是一个诚实的错误,运行时应该能够至少给你一个更有意义的错误。
-
我会这样做的。似乎这个特定的错误消息相当多产,而且大多数是一些版本不匹配。
标签: typescript azure azure-functions