In my client / server application, WCF is used for communication, which was great. However, one drawback of the current architecture is that I have to use a well-known type configuration for certain passed types. I use my own Pub / Sub engine, and this requirement is inevitable.
The problem is that it's easy to forget to add a known type, and if you do, WCF will work without the slightest clue as to what is going wrong.
In my application, I know the set of types that will be sent. I would like to perform the configuration programmatically and not declaratively through the App.config , which currently contains something like this:
<system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type="MyProject.MyParent, MyProjectAssembly"> <knownType type="MyProject.MyChild1, MyProjectAssembly"/> <knownType type="MyProject.MyChild2, MyProjectAssembly"/> <knownType type="MyProject.MyChild3, MyProjectAssembly"/> <knownType type="MyProject.MyChild4, MyProjectAssembly"/> <knownType type="MyProject.MyChild5, MyProjectAssembly"/> </add> </declaredTypes> </dataContractSerializer> </system.runtime.serialization>
Instead, I would like to do something like this:
foreach (Type type in _transmittedTypes) { // How would I write this method? AddKnownType(typeof(MyParent), type); }
Can someone explain how I can do this?
EDIT Please understand that I am trying to dynamically set known types at runtime, and not declaratively in the configuration or using attributes in the source code.
This is mainly a question about the WCF API, not the style.
EDIT 2 The page on this MSDN page says:
You can also add types to ReadOnlyCollection, accessed through the KnownTypes DataContractSerializer property.
Unfortunately, all this says, and it does not make much sense, given that KnownTypes is a readonly property, and the value of the property is ReadOnlyCollection .