无法从程序集“System.Runtime、版本=4.2.2.0、文化=中性加载类型 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder'

Could not load type 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder' from assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral

我有一个 azure 函数应用程序,它依赖于另一个 class 库来完成某些功能。

Azure 函数详细信息: 目标框架:net5.0 AzureFunctionsVersion : v3

类库详细信息: 目标框架:net5.0

我在 AzureFunction 中引用了 classlibrary 项目。

Azure 函数代码:

Startup.cs:

[assembly: FunctionsStartup(typeof(Startup))]
namespace MessageReceiver
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddTransient<IMailSender, MailSender>();
        }
    }
}

MessageReceiver.cs

namespace ProcessReportRequests
{
    public class MessageReceiverDataFunction
    {
        private const string QUEUE_NAME = "message-queue";
        private readonly IMailSender _mailSender;
        private readonly ILogger<MessageReceiverDataFunction> _log;
        public MessageReceiverDataFunction(IMailSender mailSender, ILogger<MessageReceiverDataFunction> log)
        {
            _mailSender = mailSender ?? throw new ArgumentNullException(nameof(mailSender));
            _log = log ?? throw new ArgumentNullException(nameof(log));
        }

        [FunctionName("MessageReceiver")]
        public void Run([QueueTrigger(QUEUE_NAME, Connection = "StorageConnectionString")] string queueMessage)
        {
            _mailSender.SendPlaintextMail("my.test@gmail.com", "mytest");
            _log.LogInformation($"C# Queue trigger function processed: {queueMessage}");
        }
    }
}

类库:ReportUtility

IMailSender.cs:

public interface IMailSender
{
    void SendPlaintextMail(string recipientEmail, string recipientName);
}

MailSender.cs

public class MailSender : IMailSender
{
    public MailSender()
    {
    }

    public async void SendPlaintextMail(string recipientEmail, string recipientName)
    {
        var sender = new SmtpSender(() => new SmtpClient("xxx.yyyy.zzzz")
        {UseDefaultCredentials = false, Port = 10, Credentials = new NetworkCredential(recipientEmail, "xxxxxxxxxxxxxx"), EnableSsl = true, });
        Email.DefaultSender = sender;
        var email = await Email.From(emailAddress: "my.test@sendermail.com").To(emailAddress: recipientEmail, name: recipientName).Subject(subject: "Testing Email Subject").Body(body: "This is a plain text message").SendAsync();
    }
}

在测试应用程序时,我收到如下所述的错误:

2022-05-11T19:53:58.654 [Error] Executed 'MessageReceiver' (Failed, Id=f13f0402-7c74-4f39-a9e1-a65fa8f61af9, Duration=3ms)Could not load type 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder' from assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

任何人都可以提供解决此问题的指导

为避免这种情况,请尝试将函数应用程序更新到版本 4。

将以下项目值更新为:

<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>

并使用此命令:

az functionapp config appsettings set --settings FUNCTIONS_EXTENSION_VERSION=~4 -n <APP_NAME> -g <RESOURCE_GROUP_NAME>

参考:
Migrating from 3.x to 4.x ,