如何在 Azure Functions 中包含对外部程序集的引用

How do you include references to external assemblies in Azure Functions

我尝试了以下方法:

using System;
using Newtonsoft.Json
using Newtonsoft.Linq

public static void Run(string myEventHubMessage, out string document, TraceWriter log)
{
    log.Verbose($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
    dynamic jsonData = JObject.Parse(myEventHubMessage);
    document = jsonData;
}

当我在 Azure 门户中点击 "Save" 按钮时,我得到以下信息:

2016-04-05T21:28:31 Welcome, you are now connected to log-streaming service. 2016-04-05T21:28:33.443 Script for function 'ProbeEventHubTrigger' changed. Reloading. 2016-04-05T21:28:33.443 Compiling function script. 2016-04-05T21:28:33.568 (2,22): error CS1002: ; expected 2016-04-05T21:28:33.568 (3,22): error CS1002: ; expected 2016-04-05T21:28:33.568 (2,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) 2016-04-05T21:28:33.568 (3,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) 2016-04-05T21:28:33.568 (8,24): error CS0103: The name 'JObject' does not exist in the current context 2016-04-05T21:28:33.568 Compilation failed.

我还尝试了以下方法:

#r "Newtonsoft.Json"
#r "Newtonsoft.Linq"
using System;

public static void Run(string myEventHubMessage, out string document, TraceWriter log)
{
    log.Verbose($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
    dynamic jsonData = JObject.Parse(myEventHubMessage);
    document = jsonData;
}

在这种情况下,当我点击 Azure 门户中的 "Save" 按钮时,我得到以下信息:

2016-04-05T21:35:36 Welcome, you are now connected to log-streaming service. 2016-04-05T21:35:38.428 Script for function 'ProbeEventHubTrigger' changed. Reloading. 2016-04-05T21:35:38.428 Compiling function script. 2016-04-05T21:35:38.571 (2,1): error CS0006: Metadata file 'Newtonsoft.Linq' could not be found 2016-04-05T21:35:38.571 (8,24): error CS0103: The name 'JObject' does not exist in the current context

从文档中看不出如何引用这些程序集。我在一个示例中看到语法是 "using Newtonsoft.Json",但这在门户中似乎不起作用。有什么建议吗?

史蒂夫,

.NET Framework 程序集和一些 "shared" 程序集 may be added with the following syntax:

#r "AssemblyName"

因此,对于 JSON.NET,您可以使用:

#r "Newtonsoft.Json"

添加引用后,然后您可以像在常规 C# 中一样添加 using 语句 project/file:

using Newtonsoft.Json;

因此,总而言之,您需要添加对要使用的程序集的引用, 导入该程序集公开的命名空间,以便您可以使用其类型。这类似于您在 Visual Studio 中所做的,您在其中添加程序集引用,然后在需要的地方添加 using 语句。

希望对您有所帮助!