我正在使用protobuf和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>,我就会得到一个reflection.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 注:
我在不同的地方读过,关掉"AutoCompile“可能是一项工作。这确实解决了我无法开始直接调用序列化的问题--因此我似乎不需要预编译的序列化程序。
在深入了解了protobuf的内部工作之后,我尝试这样做:
typeof(TypeModel).GetMethod("SetDefaultModel", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { new PDASerializers() })但遗憾的是,这也没有解决问题。
具体而言,我的问题如下:
是否可以替换默认的序列化程序来使用protobuf-net.grpc中预编译的序列化程序,或者是否有其他方法在全局上关闭"AutoCompile“(对protobuf-net.grpc也是如此)?
发布于 2020-06-25 17:13:36
再读一读就会发现,您实际上是在寻找gRPC支持。现在,这实际上是可能的--您只需要提供一个自定义绑定器配置:
var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> {
ProtoBufMarshallerFactory.Create(yourModelHere)
});现在,您可以在创建客户端(或者注册服务器,如果您愿意)时使用该binderConfig,并且:它应该都能工作。
至于预编译位:
这里的答案很复杂。V3是为了方便这种精确的场景而设计的--但是构建时间位(也称为“生成器”)还没有完成(这是我们一直在专门等待的C# vNext/预览功能)。
此外,V3将核心部分和反射部分分割成两部分,特别是在不使用这些特性时减少引用表面积。
因为今天可以这样做:是的,但现在是有点尴尬,因为尘埃落定。如果你有一个规模适中的例子,我很乐意和你一起工作,让它发挥作用。为了证明它是可以做到的:https://protogen.marcgravell.com/decode今天就是这样做的,并且可以通过Blazor成功地在浏览器中运行,没有运行时发出--它有预编译的FileDescriptorSet等序列化器,在需要时使用。
https://stackoverflow.com/questions/62553644
复制相似问题