问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/2409497/how-to-add-service-known-types-from-external-config-file