ankita
ankita

Reputation: 31

use of declared types in WCF

I am using [ServiceKnownType(typeof(Document))] for every operation contract in my WCF interface class.I want to avoid using this KnownType.Instead use the DeclaredTypes in web.config. can i get any help on how to configure the DeclaredTypes in Web.config file.

Upvotes: 3

Views: 506

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87308

The option to add known types in configuration only applies to data contracts, not to service contracts. If the type of which Document derives from is always the same (and not System.Object), you can do that by using the config option (more info at MSDN), which would look something like this:

<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="MyNamespace.DocumentBase, MyAssembly, Version=...">
          <knownType type="MyNamespace.Document, MyAssembly, Version=..."/>
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
</configuration>

If this doesn't apply, then another option would be to pass this option to the DataContractSerializer constructor, which you can do by using some behavior which can be applied to the whole service. The post to replace the DataContractSerializer with the NetDataContractSerializer is a good starting point for you (instead of replacing the serializer, just return another DataContractSerializer instance, but always adding typeof(Document) to the known types passed to it.

Upvotes: 3

Related Questions