c# - 如何修复 "Could not load type ' System.Runtime.Remoting.Messaging.CallContext'"

标签 c# azure azure-functions .net-6.0

错误: 无法从程序集“mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”加载类型“System.Runtime.Remoting.Messaging.CallContext”。

我正在将 Azure Function V1 迁移到运行引用 .net Framework 4.8 代码的 .net 6 的 Azure Function 版本 4。 azure 函数代码相当基础,因为它将大部分功能传递给 4.8 代码。 4.8 代码完成了所有实际工作。这包括 EF 6 的数据库功能、对 Web API 的调用等。

写入数据库的示例(读取案例相同问题)

    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();
            
        }
    }

运行 .NET 6 的 Azure Function Code V4

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));
    }
}

附加说明:4.8 代码的功能非常广泛,特别是数据库优先的 EF 框架是最大的障碍。除非有人能建议一种使用正确的 EF 版本的 .net Standard 的方法,否则我会陷入困境,直到我们能够花几个月的时间先用代码重建它。 目前,这适用于 V1 azure 函数,但由于 V1 存在奇怪的问题,对 NewtonSoft JSON (9.01) 的支持有限,我们一直在使用程序集替换来使用奇怪的解决方法,并希望迁移到似乎支持它的 V4。

创建 4.8 框架 V4 函数的说明链接也很棒。我很感激你的帮助。 Azure Functions v4 的 Miscrofts 文档 Azure Function V4

最佳答案

将 Azure Functions 代码从 .NET Framework 4.8 迁移到 .NET Core 时,您需要检查一些应与您选择的语言运行时兼容的内容,例如

  • 兼容的代码和库
  • .NET Framework 和 Core 都支持第 3 方库,但 .NET Core 仍不与 .NET Framework 竞争。

感谢@JeroenMostert ,因为您在评论中建议的解决方案是正确且有帮助的。

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.

我建议您在新版本(.NET 6 Core/.NET 4.8 Framework Azure Functions Core Tools V4)中创建新的函数应用,并重写相同的功能代码以获得最佳迁移结果。

正如 Jeroen 所说,您必须注意 .NET 4.8 Framework Azure Functions v4 功能处于预览模式,您在开发项目时可能会遇到一些问题。

请参阅.NET Framework 4.8 Azure Functions v4 Out-of-process托管函数应用程序和 sample 的指南在 GitHub 中。

关于c# - 如何修复 "Could not load type ' System.Runtime.Remoting.Messaging.CallContext'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72851446/

相关文章:

c# - 使用 EF Core 在 Azure Functions 上的 Application Insights 中启用 Sql 依赖关系

c# - 无法在 Azure 上将 MSI 与 AKS 结合使用

node.js - 如何使用node js从azure存储中删除文件夹

powershell - Azure 函数和 Powershell - 无法使用证书登录 AzureRmAccount

VS2005 中的 C# : can you check whether an integer is declared in a given Enum type?

c# - 在表单之间切换(关闭一个表单,然后打开另一个表单)

azure - 我可以同时从同一台计算机使用 azure-cli 访问多个 azure 帐户吗?

node.js - 正确的 Jest 测试 Azure Functions

c# - 如何检查元组是否有任何项目?

c# - linq Group By 并获取第一个和最后一个记录