如何在Azure Functions中包含对外部程序集的引用 [英] How do you include references to external assemblies in Azure Functions

查看:99
本文介绍了如何在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门户中单击保存"按钮时,我得到以下信息:

I get the following when I hit the "Save" button in Azure portal:

2016-04-05T21:28:31欢迎,您现在已连接到日志流 服务. 2016-04-05T21:28:33.443函数脚本 "ProbeEventHubTrigger"已更改.正在重新加载. 2016-04-05T21:28:33.443 编译功能脚本. 2016-04-05T21:28:33.568(2,22):错误 CS1002 :;预期2016-04-05T21:28:33.568(3,22):错误CS1002:; 预期的2016-04-05T21:28:33.568(2,7):错误CS0246:类型或 找不到名称空间名称"Newtonsoft"(您是否缺少 使用指令还是程序集引用?)2016-04-05T21:28:33.568 (3,7):错误CS0246:类型或名称空间名称"Newtonsoft"无法 被找到(您是否缺少using指令或程序集引用?) 2016-04-05T21:28:33.568(8,24):错误CS0103:名称'JObject'确实 当前上下文中不存在2016-04-05T21:28:33.568编译 失败.

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门户中单击保存"按钮时,得到以下信息:

In this case I get the following when I hit the "Save" button in the Azure portal:

2016-04-05T21:35:36欢迎,您现在已连接到日志流 服务. 2016-04-05T21:35:38.428功能脚本 "ProbeEventHubTrigger"已更改.正在重新加载. 2016-04-05T21:35:38.428 编译功能脚本. 2016-04-05T21:35:38.571(2,1):错误 CS0006:找不到元数据文件"Newtonsoft.Linq" 2016-04-05T21:35:38.571(8,24):错误CS0103:名称'JObject'确实 在当前上下文中不存在

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

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

It's not obvious from the documentation how to reference these assemblies. I see in one example the syntax is "using Newtonsoft.Json", but this doesn't appear to work in the portal. Any suggestions?

推荐答案

史蒂夫

.NET Framework程序集和一些共享" 程序集

.NET Framework assemblies and a few "shared" assemblies may be added with the following syntax:

#r "AssemblyName"

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

So, for JSON.NET, you can use:

#r "Newtonsoft.Json"

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

Once the reference is added, then you can add your using statements as you would in a regular C# project/file:

using Newtonsoft.Json;

因此,总而言之,您需要添加对要使用的程序集的引用,导入该程序集公开的名称空间,以便可以使用其类型.这与您在Visual Studio中执行的操作类似,在Visual Studio中添加程序集引用,然后在需要的地方添加using语句.

So, in summary, you need to add a reference to the assemblies you want to use, and import the namespaces exposed by that assembly so you can use its types. This is similar to what you'd do in Visual Studio, where you add the assembly reference and then add your using statements where you need them.

我希望这会有所帮助!

这篇关于如何在Azure Functions中包含对外部程序集的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆