ServicesResourcesConferencesOur TeamWeblogsAboutContact
   
Defining KnownTypes in a configuration file

WCF's serialization of data contracts supports the transmission of derived types which have not been available when the original application (or just contract) has been built. In the following example, ApplicationExtensionObject is just a placeholder instead of which a derived class should be transferred at runtime.

[DataContract]
public class Customer
{
  [DataMember]
  public string Firstname;
  [DataMember]
  public string Lastname;
  [DataMember]
  public Address DefaultDeliveryAddress;
  [DataMember]
  public Address DefaultBillingAddress;

  [DataMember]
  public ApplicationExtensionObject Extension;
}

[DataContract]
public class ApplicationExtensionObject
{
}

You would normally accomplish this using the [KnownType] attribute on the field Extension. But what if you can't do this as the type is not yet known at compile time? Well, for one thing you could use [KnownType(methodName)] to define a method which returns an array of Types at runtime. Another alternative is to use the section to define the mapping between declared types and known subtypes.

In the following snippet, the class ProjectSpecificExtension.CustomerExtension is defined as a valid known subtype of ApplicationExtensionObject for the serializer.

<configuration >
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Shared.ApplicationExtensionObject, Shared">
          <knownType type="ProjectSpecificExtension.CustomerExtension, ProjectSpecificExtension" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  ...
</configuration>
Update: This has actually been documented and is a supported practice. I guess I just made it too much of a habit to look into Reflector instead of searching long enough in the docs ;-)
posted on Wednesday, September 06, 2006 7:03 PM

# re: Title: Defining KnownTypes in a configuration file @ Friday, September 15, 2006 8:39 PM

Specifying KnownTypes using Config is a supported scenario. Please see my blog http://blogs.msdn.com/sowmy/archive/2006/06/06/618877.aspx for more information

sowmys

# re: Defining KnownTypes in a configuration file @ Tuesday, October 09, 2007 7:32 PM

can be added a DBNull type as a KnownType?

I can't call a WCF method like:

public function GetData(param1 as Object) as boolen

when param1 is DBNull.value

I think that adding DBNull as KnownType will work.

how can i do this?

Manuel Fernandez

# re: Defining KnownTypes in a configuration file @ Thursday, October 18, 2007 1:26 PM

Greats site Thanks for your work!
http://animeepisode1.cn
Tom


Powered by Community Server, by Telligent Systems