【问题标题】:Is it possible to set the default Serializer to a pre-compiled one in protobuf-net?是否可以将默认序列化程序设置为 protobuf-net 中的预编译序列化程序?
【发布时间】:2020-06-24 11:07:32
【问题描述】:

我正在将 protobuf-net 与 protobuf-net.grpc 一起使用,并试图让它在 Xmarin/Ios 上工作。

目前我正在尝试创建一个预编译的序列化程序:

            RuntimeTypeModel runtimeTypeModel = RuntimeTypeModel.Create();
            runtimeTypeModel.AllowParseableTypes = true;
            runtimeTypeModel.AutoAddMissingTypes = true;
            runtimeTypeModel.AutoCompile = false;
            runtimeTypeModel.Add(typeof(UserInfo), true);
            Directory.SetCurrentDirectory(@"..\..\");
            runtimeTypeModel.Compile("PDASerializers", @"PDASerializers.dll");

当我引用这个 .dll 并执行 new PDASerializers().Serialize(new UserInfo()) 时,它可以正常工作。

然而,当我试图全力以赴并使用 protobuf-net.grpc 时,我正在走进一堵砖墙。

问题是,只要我打电话:channel.CreateGrpcService<ISomeInterface>,我就会收到一个反射.emit 错误:

Operation is not supported on this platform.

  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.pns.cs:129 
  at ProtoBuf.Grpc.Internal.ProxyEmitter..cctor () [0x0001e] in /_/src/protobuf-net.Grpc/Internal/ProxyEmitter.cs:18 

注意

我在不同的地方读到关闭“自动编译”可能是一种解决方法。这确实解决了我无法直接调用 Serialize 的问题 - 所以看起来我不需要预编译的序列化程序。

在深入了解 protobuf 的内部工作原理后,我尝试这样做:

typeof(TypeModel).GetMethod("SetDefaultModel", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { new PDASerializers() })

但是很遗憾这也没有解决问题。


具体我的问题如下;

是否可以替换默认序列化程序以使用 protobuf-net.grpc 中的预编译序列化程序,或者是否有其他方法可以关闭“自动编译”全局(也适用于 protobuf -net.grpc)?

【问题讨论】:

    标签: protobuf-net protobuf-net.grpc


    【解决方案1】:

    对此的第二次阅读表明您实际上正在寻找 gRPC 支持。现在这实际上是可能的 - 你只需要提供一个自定义的 binder 配置:

    var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> {
        ProtoBufMarshallerFactory.Create(yourModelHere)
    });
    

    现在您可以在创建客户端(或注册服务器,如果您愿意)时使用该 binderConfig,并且:它应该可以正常工作。


    至于预编译位:

    这里的答案很复杂。 V3 旨在促进这种确切的场景 - 但构建时间位(又名“生成器”)尚未完成(这是我们一直在等待的 C# vNext/预览功能) )。

    此外,V3 将核心和反射部分分成两半,特别是在您不使用这些功能时减少参考表面积。

    可以今天完成:是的,但是现在有点尴尬,尘埃落定。如果您有一个中等大小的示例,我很乐意与您合作以使某些东西起作用。为了证明它可以完成:https://protogen.marcgravell.com/decode 今天正是这样做的,并且可以通过 Blazor 在浏览器中以零运行时发射成功运行 - 它具有用于 FileDescriptorSet 等的预编译序列化程序是在需要时使用。

    【讨论】:

    • 谢谢!是的,很遗憾我无法附加标签 protobuf-net.grpc - 它还不存在。
    • @RichardVNL 修复了这个问题,ta;活页夹有任何问题:让我知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2012-04-26
    • 2020-05-31
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多