DanScan
DanScan

Reputation: 901

How do I fix "Could not load type 'System.Runtime.Remoting.Messaging.CallContext'"

Error: Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I am migrating an Azure Function V1 to an Azure Function version 4 running .net 6 that references .net framework 4.8 code. The azure function code is rather basic because it passes off most the functionality to the 4.8 code. The 4.8 code does all the real work. This includes database functions with EF 6, calls to web APIs etc.

Example of writing to database (Reading cases same problem)

    public void WriteInfo(string message, int merchantid, int merchantchannelid, string logicalThread, string level)
    {
        using (var db = new Repository(_dbConnection))
        {
            var log = new Model.Log
            {
                Date = DateTime.UtcNow,
                Message = message.WriteToMaxLength(3999),
                Logger = "Base",
                Thread = logicalThread,
                Level = level,
                LogicalThreadId = logicalThread
            };
            db.Logs.Add(log);
            db.SaveAllChanges();
            
        }
    }

Azure Function Code V4 running .NET 6

public class JobRunner
{
    public static string jobquename = String.Empty;
    private readonly IConfiguration _configuration;

    public JobRunner(IConfiguration configuration)
    {
        _configuration = configuration;
        DbProviderFactories.RegisterFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance);
    }
    
    [FunctionName("JobRunner")]
    public async Task RunAsync([QueueTrigger("%Queuename%", Connection = "jobqueueconnection")]string myQueueItem, Microsoft.Extensions.Logging.ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        var logger = new Logger();
        var s = _configuration.GetConnectionString("dbConnection").ToString();
        var repo = new Repository(s);
        var f = new JobManager.CoreJobManager(s);
        var result = await f.RunJob(Convert.ToInt32(12));
    }
}

Additional notes: The 4.8 code is very extensive in what it does and in particular the EF framework being database first is the biggest stumbling block. Unless someone can suggest a way around using .net Standard with correct EF version I am pretty much stuck until we can dedicate the months it would take rebuild it with code first. This currently works in the V1 azure functions but because of V1's weird issue with limited support for NewtonSoft JSON (9.01) we have been using a weird workaround using assembly replacement and would like to move to V4 which seems to support it.

A link to instruction for creating a 4.8 framework V4 function would also be great. I appreciate the help. Miscrofts documentation for azure functions v4 Azure Function V4

Upvotes: 1

Views: 9303

Answers (1)

anon
anon

Reputation:

When migrating the Azure Functions Code from .NET framework 4.8 to .NET Core, you need to check some things that should be compatible to the language runtime you choose like

  • Compatible Code and Libraries
  • Both the .NET Framework and Core supports 3rd Party Libraries but still, the .NET Core is doesn't compete with the .NET Framework.

Thanks to @JeroenMostert, as your suggested solution in the comments are correct and helpful.

under no circumstances will you be able to mix and match .NET 6 and 4.8 assemblies this way -- you still have to settle on one particular runtime version.

I would suggest that you create the new function app in a new version (.NET 6 Core / .NET 4.8 Framework Azure Functions Core Tools V4) and rewrite the same functionality code for the best migration result.

As Jeroen also said that you have to make note of the .NET 4.8 Framework Azure Functions v4 feature is in preview mode, you may encounter some issues while developing the project.

Refer to the .NET Framework 4.8 Azure Functions v4 Out-of-process guide for hosting the function app and the sample in the GitHub.

Upvotes: 1

Related Questions