.net System.json.text in not found in JsonSerializer.Deserialize&lt;List< CarCounter>&gt;(json)

pod7payv  于 2023-05-30  发布在  .NET
关注(0)|答案(1)|浏览(143)

我有一个azure函数,代码看起来像这样。我用的是.Net 6

[FunctionName("CarCounterFunc")]
    public void Run(
        [BlobTrigger("input/{name}.xml")] Stream inputBlob,IBinder binder,string
        name,ILogger log)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(inputBlob);

        string json = jsonHandling.Convert(doc);
        json = jsonHandling.Clean(json);

        List<CarCounter> myList = JsonSerializer.Deserialize<List<CarCounter>>(json);
    }

只要我加上这一行

List<CarCounter> myList = JsonSerializer.Deserialize<List<CarCounter>>(json);

我得到这些错误

[2023-05-15T16:07:48.680Z] Executing 'CarCounterFunc' (Reason='New blob detected(LogsAndContainerScan): input/a9909523-a23b-47ed-9732-40ea4535c82f.xml', Id=68bf0483-5f36-4325-8d82-01e02c0b3361)
[2023-05-15T16:07:48.689Z] Trigger Details: MessageId: b427a04e-620b-44db-8948-d5d9cc25145a, DequeueCount: 1, InsertedOn: 2023-05-15T16:07:47.000+00:00, BlobCreated: 2023-05-15T16:07:46.000+00:00, BlobLastModified: 2023-05-15T16:07:46.000+00:00
[2023-05-15T16:07:48.955Z] Executed 'CarCounterFunc' (Failed, Id=68bf0483-5f36-4325-8d82-01e02c0b3361, Duration=627ms)
[2023-05-15T16:07:48.957Z] System.Private.CoreLib: Exception while executing function: CarCounterFunc. CarCounterprj: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Det går inte att hitta filen.

感谢Tony Johansson

wqsoz72f

wqsoz72f1#

从我的端复制后,我收到了相同的错误,你得到当我安装System.Text.Json包.

但在我卸载了软件包后,我可以使这个工作。这是因为.NetCore 3.0及以上版本安装了内置包,其中包括System.Text.Json。有关更多信息,请参阅Rich Lander发布的.NET Core 3.0。出于演示目的,我直接使用了一个示例JSON。下面是为我工作的完整代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp16
{

    public class Function1
    {
        [FunctionName("Function1")]
        public void Run([BlobTrigger("container/{name}", Connection = "constr")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

            string json1 = @"[{ ""Name"": ""John"", ""Age"": ""30"" }]";

            List<CarCounter> carCounterList = JsonSerializer.Deserialize<List<CarCounter>>(json1);

            foreach (CarCounter carCounter in carCounterList)
            {
                Console.WriteLine($"Name: {carCounter.Name}");
                Console.WriteLine($"Age: {carCounter.Age}");
                Console.WriteLine();
            }
        }
    }
}

相关问题