预编译的 Azure 函数行为异常
Precompiled Azure Function Behaving Unexpectedly
MyLibrary.dll
public class Person
{
[JsonProperty("name")]
public string Name { get; set; }
public static async Task Save(Person person)
{
DocumentClient client = CreateDocumentClient();
await client.OpenAsync();
await client.CreateDocumentAsync(CreateDocumentCollectionUri(), person);
}
}
MyFunctionApp.dll
public class SimpleHttpTrigger
{
public static async Task Run(HttpRequestMessage req)
{
Person bob = new Person() { Name = "Bob" };
await Person.Save(bob);
}
}
MyLibrary
取决于:
Newtonsoft.Json
10.0.2
Microsoft.Azure.Documents.Client
1.13.1
而MyFunctionApp
取决于MyLibrary
。
在此示例中观察到的问题是,当 Azure Function CLI 调用 SimpleHttpTrigger.Run
时,JsonProperty
属性被忽略。 SimpleHttpTrigger
从控制台应用直接调用时表现符合预期。
可以通过更改 MyLibrary
的依赖项以匹配 Azure Functions CLI 当前使用的版本来解决此问题:
- Newtonsoft.Json 9.0.1
- Microsoft.Azure.Documents.Client 1.11.4
当 Azure Function CLI 拥有自己的库版本(在 node_modules/azure-functions-cli/bin 中找到)时,它似乎忽略了 MyFunctionApp/bin 中的库。在这个小例子中,匹配依赖关系很好,但当 MyFunctionApp
具有更大的依赖关系时,这是不可行的。
我对这种行为的理解是否正确?
有什么方法可以指定在预编译函数中使用这些库的哪个版本?我相信可以通过将依赖项放入函数的 bin 文件夹中来在脚本函数中获得这种行为。
更新
我对这种行为原因的假设似乎不正确。
Newtonsoft.Json 和 Microsoft.Azure.Documents.Client 程序集的较新版本实际上是与 Azure Function CLI 自动加载的程序集一起加载的。
这让我更加困惑,因为 SimpleHttpTrigger.Run
直接从控制台应用程序调用时的行为与 Azure 函数主机调用时的行为仍然不同。
知道发生了什么事吗?可能是我做的蠢事。
更新 2
似乎使用了两个不同版本的 Newtonsoft.Json
,无论加载程序集的方式如何:
MyLibrary
按预期使用 Newtonsoft.Json
10.0.2,但其依赖项 Microsoft.Azure.Documents.Client
1.13.1 使用 Newtonsoft.Json
9.0.1
这或许可以解释 JsonProperty
属性的不兼容性。
可能吧?请帮忙,我很困惑!
基本上,您对问题的诊断是正确的。
这是 Azure Functions 的未决问题。
我自己偶然发现了这一点,并对你的沮丧表示同情。
在问题解决之前,您必须使用碰巧与 Azure Functions 实现共享的所有依赖项的相同主要版本。
这是 Azure Functions 依赖项的列表:https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/src/WebJobs.Script/packages.config
待跟踪的未解决问题:
https://github.com/Azure/azure-webjobs-sdk-script/issues/573
https://github.com/Azure/azure-webjobs-sdk-script/issues/1311
MyLibrary.dll
public class Person
{
[JsonProperty("name")]
public string Name { get; set; }
public static async Task Save(Person person)
{
DocumentClient client = CreateDocumentClient();
await client.OpenAsync();
await client.CreateDocumentAsync(CreateDocumentCollectionUri(), person);
}
}
MyFunctionApp.dll
public class SimpleHttpTrigger
{
public static async Task Run(HttpRequestMessage req)
{
Person bob = new Person() { Name = "Bob" };
await Person.Save(bob);
}
}
MyLibrary
取决于:
Newtonsoft.Json
10.0.2Microsoft.Azure.Documents.Client
1.13.1
而MyFunctionApp
取决于MyLibrary
。
在此示例中观察到的问题是,当 Azure Function CLI 调用 SimpleHttpTrigger.Run
时,JsonProperty
属性被忽略。 SimpleHttpTrigger
从控制台应用直接调用时表现符合预期。
可以通过更改 MyLibrary
的依赖项以匹配 Azure Functions CLI 当前使用的版本来解决此问题:
- Newtonsoft.Json 9.0.1
- Microsoft.Azure.Documents.Client 1.11.4
当 Azure Function CLI 拥有自己的库版本(在 node_modules/azure-functions-cli/bin 中找到)时,它似乎忽略了 MyFunctionApp/bin 中的库。在这个小例子中,匹配依赖关系很好,但当 MyFunctionApp
具有更大的依赖关系时,这是不可行的。
我对这种行为的理解是否正确?
有什么方法可以指定在预编译函数中使用这些库的哪个版本?我相信可以通过将依赖项放入函数的 bin 文件夹中来在脚本函数中获得这种行为。
更新
我对这种行为原因的假设似乎不正确。
Newtonsoft.Json 和 Microsoft.Azure.Documents.Client 程序集的较新版本实际上是与 Azure Function CLI 自动加载的程序集一起加载的。
这让我更加困惑,因为 SimpleHttpTrigger.Run
直接从控制台应用程序调用时的行为与 Azure 函数主机调用时的行为仍然不同。
知道发生了什么事吗?可能是我做的蠢事。
更新 2
似乎使用了两个不同版本的 Newtonsoft.Json
,无论加载程序集的方式如何:
MyLibrary
按预期使用 Newtonsoft.Json
10.0.2,但其依赖项 Microsoft.Azure.Documents.Client
1.13.1 使用 Newtonsoft.Json
9.0.1
这或许可以解释 JsonProperty
属性的不兼容性。
可能吧?请帮忙,我很困惑!
基本上,您对问题的诊断是正确的。 这是 Azure Functions 的未决问题。 我自己偶然发现了这一点,并对你的沮丧表示同情。
在问题解决之前,您必须使用碰巧与 Azure Functions 实现共享的所有依赖项的相同主要版本。
这是 Azure Functions 依赖项的列表:https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/src/WebJobs.Script/packages.config
待跟踪的未解决问题: https://github.com/Azure/azure-webjobs-sdk-script/issues/573 https://github.com/Azure/azure-webjobs-sdk-script/issues/1311