How to add service known types from external config file
Asked Answered
S

1

3

I am having difficulty understanding how to exactly go about adding known types; for WCF, from a configuration file that is external to my wcf. I found an example of how to set the configuration file up, however, I am a bit confused as to the way the file is set up and I am not sure as to how I am actually supposed to call this configuration file to load the service known types to my wcf. Here is the example of the configuration file containing the known types.

http://codeidol.com/csharp/wcf/Data-Contracts/Data-Contract-Hierarchy/

I am confused about why you have to add the a type and then specify another type as a child of that type just added. It seems to me you would just add the type "Contact", specify its assembly; "Host" and that would be it. Why is it that a knownType element tag follows the add type element tag specifying another type? Also, once I have configuration file set up properly, when and how do I call it from my wcf? Any assistance would be appreciated. Thanks!

Update 1: **Ok this gives me a better understanding, Thanks. I did try what you said though, and the ServiceKnownTypes were not found. The only thing I did different in my App.config file is in my service and host is that I didn't have any knownType type = "..." to specify. Here is mine at a glance. Do you have an idea what I'm doing wrong?

<system.runtime.serialization>

 <dataContractSerializer>

  <declaredTypes>

   <add type = "Data,TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57f2af9570299a17"/>

  </declaredTypes>

 </dataContractSerializer>

</system.runtime.serialization>

Sorry about posting this to comment section earlier, I hope this is clearer.**

Update 2: Here is something closer to what I am trying to accomplish. What are your thoughts?

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof=required
Schug answered 9/3, 2010 at 14:0 Comment(4)
@dawsoad: yes, much clearer ! Thanks.Ariose
Why do you want to add an entry to the config, if you have no known type to associate with your base "Data" type?? That's totally unnecessary. Only put stuff in there where you do have an object hierarchy.Ariose
I was hoping that I could add "ServiceKnownTypes" by way of a configuration file. So instead having this at and possible many others at the top of my contract; [ServiceKnownType(typeof(DataObject))], I could load the "ServiceKnownTypes" from a config file. I thought this would enable me to add types as time goes on without hardcoding them in my service contract.Schug
social.msdn.microsoft.com/Forums/en-US/wcf/thread/… This looks closer to what I am trying to accomplish. Any thoughts?Schug
A
2

If you want to specify known types in config, follow this example that you mentioned:

<system.runtime.serialization>
   <dataContractSerializer>
      <declaredTypes>
         <add type = "Contact,Host,Version=1.0.0.0,Culture=neutral,
                                                              PublicKeyToken=null">
            <knownType type = "Customer,MyClassLibrary,Version=1.0.0.0,
                                             Culture=neutral,PublicKeyToken=null"/>
         </add>
      </declaredTypes>
   </dataContractSerializer>
</system.runtime.serialization>

You don't have to do anything more than this - you don't have to "load" the config or anything - WCF will do that for you. You need to put this into your web.config (if you're hosting your service in IIS and if your client is a web app), or in your app's config (if you have a Windows service on the server side, or a console / winforms app on the client side). Just put the entries in the config, and WCF will handle the rest.

Basically, what you're saying here is: any method that has a Contact from my Host assembly could also be returning a Customer from my MyClassLibrary assembly instead.

So basically, you're defining that MyClassLibrary.Customer is most likely a descendant type of Host.Contact.

That's the same as defining on your data contract:

[DataContract]
[KnownType(typeof(Customer))]
class Contact
{...}

You have an object class Contact, but anywhere you use it, it could also really be a Customer class instance instead.

Ariose answered 9/3, 2010 at 15:15 Comment(4)
Ok this gives me a better understanding, Thanks. I did try what you said though, and the ServiceKnownTypes were not found. The only did in my App.config file in my service and host is that I didn't have any <knownType type = "..."> to specify. Here is mine at a glance. Do you have an idea what I'm doing wrong?Schug
Sorry I meant to paste this. <system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type = "Data,TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57f2af9570299a17"/> </declaredTypes> </dataContractSerializer> </system.runtime.serialization>Schug
@dawsoad: can you please update your original question with this additional info?? here in comments, it's really hard to read XML stuff!Ariose
Is this server side ? client side ? or both ? Thanks. (Upvote already)Posit

© 2022 - 2024 — McMap. All rights reserved.