How to serialize generics in WebApi and WCF?

Hi.

Is there a way to have custom serialization of types with generics, both into JSON for WebApi, and into XML for WCF?

There are 2 possibilities: Either allow full polymorphism in the service contract, or allow only the type with its type parameter instanced with a specific type. One contract would be something like "MyClass<T> MyService1(T value)", while the other would be something like "MyClass<int> MyService2()".

Is it possible to serialize and deserialize both of those return types?

I'd also like to know if it's possible for the service clients to use the same exact type for consuming it. So a WCF client (for instance) could have something like this: "MyClass<int> myInt = service.MyService2();"

For reference, I have an "Option<T>" type in my code representing optional values, and I'd like to expose and consume it from WebApi and WCF services. I have services and clients of those services in the same project, both using DLLs that have this type. I'd like to create services and consume them using this exact type, and no other one (e.g classes autogenerated from the WSDL). If it's possible to do this, I'd like to try it with other polymorphic types as well.

by gonzaw308 via /r/csharp

Leave a Reply