为什么我的Azure V2时间函数在newtonsoft引用中崩溃?

为什么我的Azure V2时间函数在newtonsoft引用中崩溃?,azure,json.net,azure-functions,Azure,Json.net,Azure Functions,我有一个简单的基于计时器的azure函数,它会崩溃,并显示以下消息 我已经为newtonsoft.Json添加了nuget包,所以我不确定这是一个问题 [1/11/2018 07:00:26] Executed 'PimDataFeeder' (Failed, Id=291e9147-7f57-4fd3-887d-a8001afc8230) [1/11/2018 07:00:26] System.Private.CoreLib: Exception while executing functio

我有一个简单的基于计时器的azure函数,它会崩溃,并显示以下消息

我已经为newtonsoft.Json添加了nuget包,所以我不确定这是一个问题

[1/11/2018 07:00:26] Executed 'PimDataFeeder' (Failed, Id=291e9147-7f57-4fd3-887d-a8001afc8230)
[1/11/2018 07:00:26] System.Private.CoreLib: Exception while executing function: PimDataFeeder. System.Private.CoreLib: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
----编辑----

这个简单的函数看起来是这样的,基本上它从远程目标下载一个文件,并在将其写入CosmosDB实例之前在内存中对其进行操作,或者至少在它开始工作后就是这样。在循环中放置断点告诉我,第一个循环迭代是有效的,实际上,字符串的第一行被正确分割,然后跟随崩溃

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Net.Http.Headers;
using System.IO.Compression;
using System.IO;
using System.Text;
using System.Net;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace NWCloudPimDataFeeder
{
    public static class PimDataFeeder
    {
        [FunctionName("PimDataFeeder")]
        public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, TraceWriter log)
        {
            // The endpoint to your cosmosdb instance
            var endpointUrl = "https://example.com";
            // The key to you cosmosdb
            var key = "XXX";
            // The name of the database
            var databaseName = "XXX";
            // The name of the collection of json documents
            var databaseCollection = "XXX";

            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", "Bearer XXX");

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage file = await client.GetAsync("https://example.com");
            var content = await file.Content.ReadAsByteArrayAsync();

            MemoryStream originalFileStream = new MemoryStream(content);
            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
            {
                MemoryStream decompressedFileStream = new MemoryStream();
                decompressionStream.CopyTo(decompressedFileStream);
                byte[] fileResult = new byte[decompressedFileStream.Length];
                decompressedFileStream.Position = 0;
                decompressedFileStream.Read(fileResult, 0, fileResult.Length);
                string result = System.Text.Encoding.UTF8.GetString(fileResult);
                //log.Info(result);

                foreach (var singleItem in result.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    log.Info("singleItem looks like: " +singleItem);
                    log.Info("In the loop");
                    var itemWrapper = new ItemWrapper { NWID = Guid.NewGuid(), Item = singleItem, DocumentType = "Item"};

                    // Create a cosmosdb client
                    using (var docClient = new DocumentClient(new Uri(endpointUrl), key))
                    {
                        // Save the document to cosmosdb
                        docClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, databaseCollection), itemWrapper)
                            .GetAwaiter().GetResult();
                    }
                }
            }
        }
    }

    public class ItemWrapper
    {
        public Guid NWID { get; set; }
        [Required]
        [JsonProperty("item")]
        public string Item { get; set; }
        [Required]
        [JsonProperty("documentType")]
        public string DocumentType { get; set; }
    }
}

这些错误通常是由于未安装最新版本的SDK/Tools函数而导致的。对Newtonsoft 11.0.0的引用表明,情况可能就是这样(最新版本为11.0.2)。您可以在VS中导航到工具->扩展和更新->更新(窗口左下角),以检查Azure功能工具的更新。获得最新更新后,尝试重新创建函数以查看其是否有效

现在,在VS中调试函数时,Cli和运行时的输出如下,函数项目是使用Microsoft.NET.Sdk创建的。默认情况下,函数1.0.23(>=1.0.14)引用Newtonsoft.Json 11.0.2

Azure Functions Core Tools (2.1.748 Commit hash: 5db20665cf0c11bedaffc96d81c9baef7456acb3)
Function Runtime Version: 2.0.12134.0
我只能用一些旧的函数运行时来解决这个问题,它仍然需要v10 Newtonsoft.Json。因此,检查函数运行时版本并确保VS使用最新版本

手动下载并设置cli

  • 使用VS删除旧函数CLI。删除
    %localappdata%\AzureFunctionsTools\Releases
    下的子文件夹
  • 删除VS
    %userprofile%\.templateengine
    使用的模板引擎
  • 转到CLI提要以下载最新的CLI,现在它是
  • 转到
    %localappdata%\AzureFunctionsTools\Releases
    并创建文件夹
    2.10.1
  • 解压缩zip并将其重命名为
    cli
    ,将其拖动到
    2.10.1
  • cli
    下的
    templates
    文件夹复制到
    2.10.1
    ,并通过删除版本重命名其中的两个文件。e、 g
    itemTemplates.2.0.0-10300.nupkg
    itemTemplates.nupkg
  • 2.10.1
    下创建manifest.json,如下所示,并更改用户名

        {
          "CliEntrypointPath": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.10.1\\cli\\func.exe",
          "FunctionsExtensionVersion": "~2",
          "MinimumRuntimeVersion": "2.1",
          "ReleaseName": "2.10.1",
          "RequiredRuntime": ".NET Core",
          "SdkPackageVersion": "1.0.23",
          "TemplatesDirectory": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.10.1\\templates"
        }
    
  • 文件夹结构应该是这样的


    重新启动VS后,一切都应按预期进行。

    如果我没有弄错的话,Azure函数所需的包取决于特定的Newtonsoft.Json版本。版本可能有问题。你没有收到任何生成警告或消息吗?没有生成警告没有,只是运行时,我试图查找所需的版本,但到目前为止没有找到任何版本,仍在查找它需要11.0.2,这是我使用VS 2017时得到的?是VS 2017 15.8.8我如何确保?您好@MattDouhan,您确实看到了旧版本吗?你还记得这个吗?你曾经发现运行时是旧的2.0.11651.0。我在网上读了很多关于这方面的文章,似乎我的cli工具可能出了问题,无论我做什么,都只获得beta 25版本,没有更新的版本,其他帖子说这太旧了,但无论我做什么,我都无法获得newer@MattDouhan您曾经说过,通过VPN连接,您的VS将成功下载新的cli。这一次,即使使用VPN,你也不能再下载了。好吧?无论我使用或不使用VPN做什么,只有beta 25被下载,我得到了beta 26的一个文件夹,但它总是空的,没有更新的