Reputation: 278
I have a message in protoBuf which called "package", I generated my .proto file to classes in purpose to fill all the required fields in that "package" and it works fine. Now, I have that protoBuf message instance called "package" and I want to serialize it to a stream and then output it to a file.
byte[] data;
using (var ms = new MemoryStream())
{
Serializer.Serialize<repo_package.Package.Builder>(ms, package);
data = ms.ToArray();
}
string packageFilePath = Path.Combine("C:\\1", package.Name);
File.WriteAllBytes(packageFilePath, data);
The problem is the I'm getting an Error when I calling to "Serializer" function, the error is
"Only data-contract classes (and lists/arrays of such) can be processed"
Why is that? How I can output my Package (protobuf message) to a file?
Thank you,
Orion.
Upvotes: 2
Views: 2161
Reputation: 1062780
This looks like a library cross-over; if your DTO is Package
, then the existence of Package.Builder
strongly suggests that you are using the code-generation tools from Jon Skeet's protobuf-csharp-port
implementation. That is fine, but code generated via protobuf-csharp-port
expect to be used with the protobuf-csharp-port
library, which probably means "use the WriteTo
method on the DTO instance".
protobuf-net
is a completely separate implementation of the same serialization protocol; it has separate code-generation tools (admittedly it may be slightly confusing that both involve tools called protogen
and protoc
). If you use the protobuf-net
version of protogen
, it will output code that works with the protobuf-net
library (for completeness, with protobuf-net
it is also common to use code-first, without ever involving a .proto
file, but contract-first .proto
usage is fully supported).
So; either:
protobuf-csharp-port
code-generation, and switch to the protobuf-csharp-port
runtime, or:protobuf-net
runtime, and switch to the protobuf-net
code-generationUpvotes: 1