简体   繁体   English

.Net Standard 4.7.1 在序列化期间无法加载 System.Private.CoreLib

[英].Net Standard 4.7.1 Could not load System.Private.CoreLib during serialization

I'm working on migrating most of my project to .Net Standard 2.0.我正在努力将我的大部分项目迁移到 .Net Standard 2.0。

Last part of the project in .net standard 4.7.1 is a WCF front end. .net 标准 4.7.1 项目的最后一部分是 WCF 前端。 This executable communicate with a .net core application throught a library Akka.net by ClientServiceReceptionist.此可执行文件通过 ClientServiceReceptionist 的库 Akka.net 与 .net 核心应用程序通信。

Network communication between application by akka.net work.应用程序之间的网络通信通过 akka.net 工作。 Except when an it try to serialize a ReadOnlyCollection.除非它尝试序列化 ReadOnlyCollection。

In this case the system try to load a "System.Private.CoreLib.dll" that is not available.在这种情况下,系统会尝试加载不可用的“System.Private.CoreLib.dll”。

在此处输入图片说明

I readed many issues with .net 4.6 using .net standard 2.0 libraries that must have been corrected in 4.7.1.我使用 .net 标准 2.0 库阅读了 .net 4.6 的许多问题,这些问题必须在 4.7.1 中得到纠正。 I pass from package.config to PackageReference.我从 package.config 传递到 PackageReference。

I tryed to used NewtonSoft as a serializer in place of Hyperion without progress.我尝试使用 NewtonSoft 作为序列化程序来代替 Hyperion,但没有任何进展。

Does anyone have an idea, a solution ?有没有人有想法,解决方案?

Edit : 07-05-2018编辑 : 07-05-2018

在此处输入图片说明

The issue is throw in the WCF Entry Points when i sent a ClusterClient.Send object throught the ClusterClientReceptionist.当我通过 ClusterClientReceptionist 发送 ClusterClient.Send 对象时,问题出在 WCF 入口点中。

The object sent contains only boolean, string, Guid and array of string properties.发送的对象仅包含布尔值、字符串、Guid 和字符串属性数组。

Edit 08-05-2018编辑 08-05-2018

The object sent look like this :发送的对象如下所示:

{
  (string) "Path": "/user/ProcessOrderWorkflow",
  "Message": {
    "Order": {
      "Data": {
        (string)"Sentence": "i love my name",
        "Options": {
            (boolean)"Simplify" : true,
            (IReadOnlyCollection<string>) LanguageAffinity : [ "FR", "EN" ]
        }
      },
      "OrderQuery": {
        "Verb": {
          (string)"Verb": "Extract"
        },
        "Arguments": [
          {
            (string)"Argument": "Sentence"
          },
          {
            (string)"Argument": "Meaning"
          }
        ]
      },
      (Guid)"ProcessId": "0bfef4a5-c8a4-4816-81d1-6f7bf1477f65"
    },
    (string)"ReturnTypeFullName": "Viki.Api.Server.Data.SentenceMeaningMsg,, Viki.Api.Server.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
  },
  (boolean)"LocalAffinity": false
}

Each of the object used in the hierachi is builded using the constructor. hierachi 中使用的每个对象都是使用构造函数构建的。 All the properties are in readonly.所有属性都处于只读状态。

I tryed to serialize and deserialize the result on the WCF part before it's sent and it works.我尝试在 WCF 部分的结果发送之前对其进行序列化和反序列化,并且可以正常工作。

var serializer = this._system.Serialization.FindSerializerFor(result);
var bytes = serializer.ToBinary(result);
var data = serializer.FromBinary(bytes, result.GetType());

The strange think is that it try to deserialize the object in the WCF part where it is sent and not in the LightHouse where the element should be received and transfert to a agent for processing.奇怪的想法是它尝试在发送它的 WCF 部分中反序列化对象,而不是在应接收元素并将其传输到代理进行处理的 LightHouse 中。

This is a top result for request "Could not load System.Private.CoreLib" so I post the workaround for ASP.NET Core APIs and .NET clients.这是请求“无法加载 System.Private.CoreLib”的最佳结果,因此我发布了 ASP.NET Core API 和 .NET 客户端的解决方法。

If json serializer type handling set to auto如果 json 序列化程序类型处理设置为自动

settings.TypeNameHandling = TypeNameHandling.Auto;

serializer will include type information for polymorphic types.序列化程序将包含多态类型的类型信息。 After migration to .NET Core some clients reported exception and the response body contained following type descriptor:迁移到 .NET Core 后,一些客户端报告了异常,并且响应正文包含以下类型描述符:

"$type":"System.String[], System.Private.CoreLib"

In the API model property type was defined as IEnumerable<string> which forced serializer to include actual type for an Array.在 API 模型中,属性类型被定义为IEnumerable<string> ,它强制序列化程序包含数组的实际类型。 The solution was to replace IEnumerable<string> with string[] or any other concrete type which allowed serializer to omit the type descriptor.解决方案是将IEnumerable<string>替换为string[]或任何其他允许序列化程序省略类型描述符的具体类型。

If the above does not work, for instance when you use Dictionary<string, object> you can implement custom SerializationBinder :如果上述方法不起作用,例如当您使用Dictionary<string, object>您可以实现自定义SerializationBinder

public class NetCoreSerializationBinder : DefaultSerializationBinder
{
    private static readonly Regex regex = new Regex(
        @"System\.Private\.CoreLib(, Version=[\d\.]+)?(, Culture=[\w-]+)(, PublicKeyToken=[\w\d]+)?");

    private static readonly ConcurrentDictionary<Type, (string assembly, string type)> cache =
        new ConcurrentDictionary<Type, (string, string)>();

    public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
    {
        base.BindToName(serializedType, out assemblyName, out typeName);

        if (cache.TryGetValue(serializedType, out var name))
        {
            assemblyName = name.assembly;
            typeName = name.type;
        }
        else
        {
            if (assemblyName.Contains("System.Private.CoreLib", StringComparison.OrdinalIgnoreCase))
                assemblyName = regex.Replace(assemblyName, "mscorlib");

            if (typeName.Contains("System.Private.CoreLib", StringComparison.OrdinalIgnoreCase))
                typeName = regex.Replace(typeName, "mscorlib");

            cache.TryAdd(serializedType, (assemblyName, typeName));
        }
    }
}

And register it in JsonSerializerSettings :并在JsonSerializerSettings注册它:

settings.SerializationBinder = new NetCoreSerializationBinder();

So this is what I thought the issue might be... The problem is that transmitting serialized content between a .NET Core application and a .NET Framework application using a polymorphic serializer like JSON.NET or Hyperion is an unsupported operation in Akka.NET at least, but also in any other runtime where the users uses CLR types as the wire format of the messages.所以这就是我认为的问题可能是......问题是在 .NET Core 应用程序和 .NET Framework 应用程序之间使用多态序列化程序(如 JSON.NET 或 Hyperion)传输序列化内容是 Akka.NET 中不受支持的操作至少,而且在用户使用 CLR 类型作为消息的有线格式的任何其他运行时中也是如此。

A brief explanation as to why we don't support this in Akka.NET, from the v1.3.0 release notes :关于为什么我们在 Akka.NET 中不支持这一点的简要说明,来自 v1.3.0 发行说明

As a side note: Akka.NET on .NET 4.5 is not wire compatible with Akka.NET on .NET Core;附带说明:.NET 4.5 上的 Akka.NET 与 .NET Core 上的 Akka.NET 不兼容; this is due to fundamental changes made to the base types in the CLR on .NET Core.这是由于对 .NET Core 上的 CLR 中的基本类型进行了根本性更改。 It's a common problem facing many different serializers and networking libraries in .NET at the moment.这是目前 .NET 中许多不同序列化程序和网络库面临的常见问题。 You can use a X-plat serializer we've developed here: #2947 - please comment on that thread if you're considering building hybrid .NET and .NET Core clusters.您可以使用我们在此处开发的 X-plat 序列化程序:#2947 - 如果您正在考虑构建混合 .NET 和 .NET Core 集群,请对该线程发表评论。

The "fundamental changes" in this case being that the namespaces for types like string are different on .NET 4.* and .NET Core, thus a polymorphic serializer that doesn't account for those differences will throw this type of exception.在这种情况下,“根本性变化”string类型命名空间在 .NET 4.* 和 .NET Core 上是不同的,因此不考虑这些差异的多态序列化程序将抛出这种类型的异常。

We do have a workaround available here if you'd like to use it:如果您想使用它,我们在此处提供了一种解决方法:

https://github.com/akkadotnet/akka.net/pull/2947 https://github.com/akkadotnet/akka.net/pull/2947

You'll need to register that serializer compat layer with JSON.NET inside Lighthouse and your .NET 4.7.1 apps.您需要在 Lighthouse 和您的 .NET 4.7.1 应用程序中使用 JSON.NET 注册该序列化程序兼容层。

If this issue happens during deserialization in .NET Framework application and you can't do anything about the JSON you receive, then you can extend DefaultSerializationBinder and use it in JsonSerializerSettings to deserialize JSON generated by .NET Core application:如果在 .NET Framework 应用程序中反序列化期间发生此问题,并且您无法对收到的 JSON 执行任何操作,那么您可以扩展DefaultSerializationBinder并在JsonSerializerSettings使用它来反序列化 .NET Core 应用程序生成的 JSON:

internal sealed class DotNetCompatibleSerializationBinder : DefaultSerializationBinder
{
    private const string CoreLibAssembly = "System.Private.CoreLib";
    private const string MscorlibAssembly = "mscorlib";

    public override Type BindToType(string assemblyName, string typeName)
    {
        if (assemblyName == CoreLibAssembly)
        {
            assemblyName = MscorlibAssembly;
            typeName = typeName.Replace(CoreLibAssembly, MscorlibAssembly);
        }

        return base.BindToType(assemblyName, typeName);
    }
}

and then:接着:

var settings = new JsonSerializerSettings()
{
    SerializationBinder = new DotNetCompatibleSerializationBinder()
};

for the base Type try to use对于基本类型尝试使用

serializer.TypeNameHandling = TypeNameHandling.Auto

can ignore the System.Private.CoreLib namespace in Json可以忽略 Json 中的 System.Private.CoreLib 命名空间

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集“ System.Private.CoreLib…” - Could not load file or assembly 'System.Private.CoreLib…' Azure Function:System.Private.CoreLib:无法加载文件或程序集“System.ComponentModel.Annotations...” - Azure Function: System.Private.CoreLib: Could not load file or assembly 'System.ComponentModel.Annotations…' System.TypeLoadException:无法从程序集“System.Private.CoreLib”加载类型“System.Object”,因为父级不存在 - System.TypeLoadException: Could not load type 'System.Object' from assembly 'System.Private.CoreLib' because the parent does not exist 无法加载文件或程序集“System.Private.CoreLib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e” - Could not load file or assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Azure函数:system.private.corelib:执行函数时发生异常 - Azure Function : system.private.corelib : exception while executing function UWP App RequestRestartAsync在System.Private.Corelib中引发异常 - UWP App RequestRestartAsync Throws Exception in System.Private.Corelib Asp Core新项目引发与“ System.Private.CoreLib”有关的异常 - Asp Core new project throws exceptions related to “System.Private.CoreLib” Windows 平台上的 .NET MAUI 应用程序在 System.Private.CoreLib.dll 中获取 System.IO.FileNotFoundException' - .NET MAUI app on Windows platform getting System.IO.FileNotFoundException' in System.Private.CoreLib.dll C#.NET Core如何在System.Private.CoreLib.dll中调试System.IO.FileNotFoundException? - C# .NET Core How to debug System.IO.FileNotFoundException in System.Private.CoreLib.dll? .NET 核心 - System.Private.CoreLib.dll 与 System.Runtime - .NET Core - System.Private.CoreLib.dll vs System.Runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM