1

我正在将 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>我得到一个反射。发射错误:

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 

注意。

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

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

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

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


具体来说,我的问题如下;

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

4

1 回答 1

1

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

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

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


至于预编译位:

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

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

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

于 2020-06-25T17:13:36.330 回答