4

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the confirmation - that was my gut feeling, too. FWIW, the object in question is literally just a collection of string properties, bundled together only to make it easy to serialize across the boundary I'm jumping. Seemed like a perfect case for the oft-misunderstood struct. Am I wrong?
@cori - yes; class would be more appropriate there. The choice has nothing to do with it being a simple container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.