我用protobuf-net序列化了一个对象列表.
从理论上讲,.bin文件可以包含数百万个对象.
假设对象属于包含以下内容的类:
public string EventName;
Run Code Online (Sandbox Code Playgroud)
我必须进行查询并创建一个包含与查询匹配的对象的列表.使用LINQ从序列化文件中提取匹配对象的正确方法是什么?
我还没有使用过wcf web api.Glenn Block在MIX11上的演示看起来很不错,所以我一定会深入研究.
与此同时,我想知道它如何与协议缓冲区一起使用(主要是出于性能原因).我们的想法是建立一个application/x-protobuf MIME类型(或等价物).
我想我们可以:
你觉得那会有用吗?
我们有很多类的庞大应用程序.我们目前正在使用Monotouch将此.net应用程序移植到IPad.我们在使用DataContractSerializer时遇到了一些问题,我们想使用Marc Gravell的protobuf-net序列化器.
客户端和服务器之间的通信由WCF服务管理.
WCF服务由暴露给客户端和服务器的一个接口以及服务器上此接口的一个实现组成.
界面看起来像这样:
[ServiceContract]
public interface IMyService
{
[OperationContract]
SomeObject MyFunction(SomeObject myObject);
}
Run Code Online (Sandbox Code Playgroud)
服务器端实现看起来像这样:
[ServiceBehavior(...)]
public class MyService
{
public SomeObject MyFunction(SomeObject myObject)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我们的课程看起来像这样:
[DataContract]
public class MyClass
{
[DataMember]
public int SomeProp {get; set;}
[OnSerialized]
public void OnSerialized(StreamingContext context)
{
}
}
Run Code Online (Sandbox Code Playgroud)
所以这是我的问题:
对我的类,wcf接口和wcf实现做什么改动.
如何将默认WCF DataContractSerializer替换为Protobuf Serializer.
请注意,在monotouch上,我只能访问Protobuf和Protobuf.Meta命名空间.
[编辑] 我找到了一种交换序列化器运行时的方法: 自定义WCF DataContractSerializer
上述解决方案使用DataContractSerializerOperationBehavior.Protobuf-net是否提供此类行为?
[DataContract] public class Foo
{
[DataMember(Order = 1)] public int FooId { get; set; }
}
[DataContract] public class Bar : Foo
{
[DataMember(Order = 2)] public string Name { get; set; }
}
[DataContract] public class Baz : Bar
{
[DataMember(Order = 3)] public string BazName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,在代码中我设置了一个新的TypeModel并添加了我的子类型
_ProtobufSerializer = TypeModel.Create();
_ProtobufSerializer.Add(typeof(Bar), true);
_ProtobufSerializer.Add(typeof(Baz), true);
var type = _ProtobufSerializer.Add(typeof(Foo), true);
type.AddSubType(100, typeof(Bar));
type.AddSubType(101, typeof(Baz));
Run Code Online (Sandbox Code Playgroud)
现在,我可以将Foo,Bar和Baz的实例序列化.如果我序列化
var listThatWorks = new List<Foo> { new Foo { FooId …Run Code Online (Sandbox Code Playgroud) 我使用protobuf-net进行二进制序列化.在序列化A类时我很喜欢OutOfMemory .BinaryFormatter很好地序列化了同一个对象.
下面是类示例:
[ProtoContract]
class A:
[ProtoMember(1, DataFormat = DataFormat.Group)]
B[] Array1 {get; set;}
....
class B:
[ProtoMember(1)]
string Field1 {get; set;}
[ProtoMember(2)]
string Field1 {get; set;}
[ProtoMember(3, DataFormat = DataFormat.Group)]
C[] Array2 {get; set;} // 20000 elements
....
class C:
[ProtoMember(1)]
string Field1 {get; set;}
[ProtoMember(2)]
string Field1 {get; set;}
Run Code Online (Sandbox Code Playgroud) 是否有任何规范的直接方式在.NET 4 WCF上启用protobuf-net序列化?我试图将代码简化到可以轻松构建的程度:
这是我的服务代码:
[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MobileServiceV2
{
[WebGet(UriTemplate = "/some-data")]
[Description("returns test data")]
public MyResponse GetSomeData()
{
return new MyResponse { SomeData = "Test string here" };
}
}
[DataContract]
public class MyResponse
{
[DataMember(Order = 1)]
public string SomeData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在Application_OnStart(Global.asax)中激活此服务路由,如下所示:
RouteTable.Routes.Add(new ServiceRoute("mobile", new MyServiceHostFactory(), typeof(MobileServiceV2)));
Run Code Online (Sandbox Code Playgroud)
我用MyServiceHostFactoryMEF来管理服务,但那是无关紧要的.
我的服务配置都是默认的,Web.Config中唯一的附加功能就在这里:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" maxReceivedMessageSize="5242880" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" …Run Code Online (Sandbox Code Playgroud) 我希望我的Swagger输出支持Protobuf和Websockets。(Protobuf是最重要的)
基于这个问题,我不认为招摇可以支持这一点。我的目标是允许最终用户看到Protobuf格式的价值,因为他们都要求我改用JSON。
我会贡献自己的力量,但是我不熟悉那个级别的大手笔项目。
题
有什么方法可以使Swagger支持Protobuf或WebSockets?
protobuf-net是否可以处理新的自动只读属性,即使用单个get和否定义的自动属性private set?
public class WithReadonlyProperty {
public int ReadonlyProperty { get; }
public WithReadonlyProperty(int val) {
ReadonlyProperty = val;
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时
RuntimeTypeModel
.Default
.Add(typeof (WithReadonlyProperty), false)
.Add(nameof(WithReadonlyProperty.ReadonlyProperty));
var test = new WithReadonlyProperty(12345);
using (var output = File.Create(@"c:\temp\_readonly.bin")) {
try {
Serializer.Serialize(output, test);
} catch (Exception e) {
Console.WriteLine(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
System.InvalidOperationException: Cannot apply changes to property WithReadonlyProperty.ReadonlyProperty
at ProtoBuf.Serializers.PropertyDecorator.SanityCheck(TypeModel model, PropertyInfo property, IProtoSerializer tail, Boolean& writeValue, Boolean nonPublic, Boolean allowInternal) in c:\Dev\protobuf-net\protobuf-net\Serializers\PropertyDecorator.cs:line 46
at …Run Code Online (Sandbox Code Playgroud) 我正在使用protobuf-net序列化对象,但出现异常:
无法将更改应用于属性TestProject.TestMessage.ClientId
使用stacktrace:
at ProtoBuf.Serializers.PropertyDecorator.SanityCheck(TypeModel model, PropertyInfo property, IProtoSerializer tail, Boolean& writeValue, Boolean nonPublic, Boolean allowInternal)
at ProtoBuf.Serializers.PropertyDecorator..ctor(TypeModel model, Type forType, PropertyInfo property, IProtoSerializer tail)
at ProtoBuf.Meta.ValueMember.BuildSerializer()
at ProtoBuf.Meta.ValueMember.get_Serializer()
at ProtoBuf.Meta.MetaType.BuildSerializer()
at ProtoBuf.Meta.MetaType.get_Serializer()
at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest)
at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value)
at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
Run Code Online (Sandbox Code Playgroud)
我的课程是:
[DataContract]
public class TestMessage
{
private int clientId;
[DataMember(Order = 1)]
public int ClientId
{
get { …Run Code Online (Sandbox Code Playgroud) 我正在寻找替换ASF中RPC的默认序列化器。这涉及实现一些接口,其中一个接口在通过RPC进行通信的服务之间传递
public interface IServiceRemotingResponseMessageBody
{
void Set(object response);
object Get(Type paramType);
}
Run Code Online (Sandbox Code Playgroud)
由于实现需要可序列化,因此显而易见的ProtoBuf实现类似于
[ProtoContract]
public class ProtoBufRemotingResponseBody : IServiceRemotingResponseMessageBody
{
[ProtoMember(1)]
public object Value { get; set; }
public void Set(object response)
{
Value = response;
}
public object Get(Type paramType)
{
return Value;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败了
没有为类型定义序列化器:System.Object
这里有解决方法吗?System.Object的没有合同,但开箱即用的DataContract串行器可以,如可以MessagePack 这里,但这些都不是系统化它可以创建时的版本头痛可靠的集合。我试过使用常见的基本类型,但Value可以是IEnumerable<T>或T等。
有人可以帮忙吗?谢谢,KH
protobuf-net protobuf-csharp-port azure-service-fabric service-fabric-stateful