I'm writing a very simple C# HttpHandler (ashx) to use as a web service, and intend to use using Json.Net to serialize a small (4-5 fields) struct across a server boundary.
What are my options for being able to serialize the struct on the web service side and deserialize it into the correct type on the other end (which happens to be a separate c# web application) without having to more or less copy and paste the definition of the struct on each end? Right now the struct is a nested type inside the webservice and in the consuming web page. I could extract it into its own class assembly, and add a reference on both ends, but that doesn't seem to be much simpler than maintaining the definition on either end. Do I have any other sensible options?
If you only have a few types to transfer and they aren't likely to change much - just define it at both ends.
If you have lots, then share a dll
No magic really... json is by design quite contract free. In fact, even anonymous types serialize very nicely in most cases.
Personally I'd use a class
not a struct
, though... as always, prefer class
unless you have a really good idea why you are using struct
: most times, they are used incorrectly.