Search code examples
c#azureazure-functionsclass-libraryasp.net-core-5.0

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


I have a azure function app which has a dependency on another class library to accomplish certain functionalities.

Azure Function Details: TargetFramework: net5.0 AzureFunctionsVersion : v3

ClassLibrary Details: TargetFramework: net5.0

I referenced the classlibrary project in the AzureFunction.

Azure Function Code:

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("[email protected]", "mytest");
            _log.LogInformation($"C# Queue trigger function processed: {queueMessage}");
        }
    }
}

ClassLibrary :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: "[email protected]").To(emailAddress: recipientEmail, name: recipientName).Subject(subject: "Testing Email Subject").Body(body: "This is a plain text message").SendAsync();
    }
}

On testing the application I am getting an error as mentioned below :

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'.

Can anyone provide their guidance to fix this issue


Solution

  • To avoid this try updating the function app to version 4.

    Updating the following project values to:

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

    and use this command:

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

    Reference:
    Migrating from 3.x to 4.x ,
    .NET Core 3.1 - Could not load file or assembly System.Runtime, Version=4.2.2.0