pedrohm / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

WCF serializer replace #195

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
armsoft@ukr.net, Ruslan Mogilevskiy.
==============

What steps will reproduce the problem?
1. Create WCF service host and WinForms/WPF client
2. Add service endpoints into the app.config with <protobuf/> extension as 
described in protobuf doc.
3. run server/client.

What is the expected output? What do you see instead?
All messages are serialized with the XmlProtoSerializer.
Instead the default WCF serializer (DataContract) is used while the custom 
behavior is added to every service operation. 
NET 4.0, VS2010 SP1, WinForms server (self hosting) and client.

What version of the product are you using? On what operating system?
last (2.0.0.420) on Win7 x64

Please provide any additional information below.
There`s many topics in internet about changing WCF serializer but many of them 
have a comment that this isn`t working (default serializer is used anyway).

Original issue reported on code.google.com by arms...@ukr.net on 25 Jun 2011 at 10:09

GoogleCodeExporter commented 9 years ago
Also, ProtoOperationBehavior trying to create new XmlProtoSerializer instance 
for every data type used in the service contract (return type or parameters) 
but it fails (calls the base implementation in 
ProtoOperationBehavior\CreateSerializer()) because ProtoOperationBehavior.model 
(= RuntimeTypeModel.Default by default) doesn`t contain models for even simple 
types like string, etc.

So how you suppose to work with TypeModel/RunTimeModel to serialize data types 
in WCF requests?
Should I add some predefined types into the model before using? Any working 
sample/doc is appreciated.

Thanks in advance.

Original comment by arms...@ukr.net on 25 Jun 2011 at 10:17

GoogleCodeExporter commented 9 years ago
The benefits of serializing simple types like string, int etc would be marginal 
in most cases. IMO it isn't worth swapping for this, but rather only for 
interesting class models. It should, however, work fine for types marked as 
either [DataContract] or [ProtoContract] (indeed I was demonstrating this in a 
geek meeting just the other weekend, using 2.*). Can you give an example 
service contract I can look at here? Ideally involving something more than 
int/string/etc (since these are skipped by design).

Original comment by marc.gravell on 25 Jun 2011 at 10:38

GoogleCodeExporter commented 9 years ago
Thanks for quick reply.
I`ll prepare sample server/client project with the first problem.
As to the second one: I see that the ProtoWriter class supports simple types 
out of the box (WriteFieldHeader(...)). I don`t know yet (becuase of first 
problem) when the CreateSerializer method will be involved and whether is would 
be called for simple types like string, but not I`m writing a custom serializer 
for Silverlight (I`ll send you when it will be ready to deploy in protobuf-net 
if you want) and there serializer will be created for every part of the WCF 
message (either return type or parameter which may be either a simple (string) 
or complex (DataContract) type) and in this case I should know which types to 
serialize and which not. So it would be very useful to just fire 'new 
XmlProtoSerializer(model, objectType).Serialize(object)' for any (BCL or 
DataContract) type and get exception only in most worse cases when serializer 
don`t know what to do with the supplied object.

Original comment by arms...@ukr.net on 25 Jun 2011 at 11:02

GoogleCodeExporter commented 9 years ago
it would be possible I'm sure - I'm just not sure of the benefit vs the 
standard serialiser in that case; btw you might also want to see 
http://stackoverflow.com/questions/6065081/using-a-custom-wcf-serializer-in-silv
erlight/6065200#6065200 which might be useful to you.

Original comment by marc.gravell on 25 Jun 2011 at 11:23

GoogleCodeExporter commented 9 years ago
Thanks for the link, I`m going the same way. I`ve made some steps further and 
now I have a simple sample server which uses custom serializer so I guess the 
first problem is solved (is in my code, will find it).

Marc, could you please point me to a tutorial or drop a quick description about 
differences between Serializer.Serialize(), ProtoWriter (XmlProtoSerializer use 
it) and TypeModel.Serialize() which seems do the same thing - serializing 
object graph. Which one should I use to just serialize an object? Why not just 
one Serializer.Serialize(obj)? IMO there`s pretty complex architecture to 
quickly understand what to do.
Also, you`ve mentioned the protobuf demonstration early. Is there any 
protobuf-net video or text tutorials other than on this site?

Thanks for great implementation and your effort!

Original comment by arms...@ukr.net on 26 Jun 2011 at 4:46

GoogleCodeExporter commented 9 years ago
Ohh, forgot. In the GettingStarted tutorial 'Amit.B...@gmail.com, Mar 27, 2011' 
added useful question:
"To support portability, would i need to manually create a .proto file defining 
the messages for communicating between this c# library and other (i.e jave, 
cpp) implementation? Is there a way to auto-generate the .proto file from 
parsed class?"

but there`s no answer. So, if I using entity class instead of .proto files, can 
I generate appropriate .proto files for those entities/services to use from 
other platforms? Or should I maintain those .protos manually (human errors 
possible)?

Thanks.

Original comment by arms...@ukr.net on 26 Jun 2011 at 4:51

GoogleCodeExporter commented 9 years ago
In v1, GetProto() does that; this is an outstanding piece of work for v2.

ProtoWriter is *largely* an implementation detail, but the pre-gen process 
requires this to be public. Essentially it is similar to XmlWriter vs 
XmlSerializer - you will most-likely never use ProtoWriter directy.

A better question, though, would be the difference between Serializer and 
TypeModel; Serializer is the v1 API - still fully working, and acts as a proxy 
to RuntimeTypeModel.Default.

TypeModel supports everything Serializer offers, but has much greater 
flexibility - for example, parallel (different) models on the same types, use 
without attributes, etc.

Original comment by marc.gravell on 26 Jun 2011 at 10:17

GoogleCodeExporter commented 9 years ago
The xml-related serializer **only** exists  to make WCF happy. I you don't need 
to look like XML, don't use it.

Original comment by marc.gravell on 26 Jun 2011 at 10:20

GoogleCodeExporter commented 9 years ago
Thanks for clarification.

Original comment by arms...@ukr.net on 26 Jun 2011 at 1:38

GoogleCodeExporter commented 9 years ago
Hello Marc,
I`ve incorporated protobuf-net serializer between SL (v5 in my case) client and 
WCF service. If you interested, I`ll prepare and send you the code to include 
into framework.
Thanks for protobuf-net!

Original comment by arms...@ukr.net on 24 Jul 2011 at 9:56

GoogleCodeExporter commented 9 years ago
To the last comment: i`m using protobuf v2

Original comment by arms...@ukr.net on 24 Jul 2011 at 10:03

GoogleCodeExporter commented 9 years ago
Certainly interested, if you are able to contribute the code under the existing 
license, etc.

Original comment by marc.gravell on 26 Jul 2011 at 7:46