如何从外部配置文件添加服务已知类型 [英] How to add service known types from external config file
问题描述
我很难理解如何准确地添加已知类型;对于WCF,从我的wcf外部的配置文件。我发现了一个例子,如何设置配置文件,但是,我有点困惑的文件设置的方式,我不知道我实际应该调用这个配置文件加载服务已知类型到我的wcf。下面是包含已知类型的配置文件示例。
http://codeidol.com/csharp/wcf/Data-Contracts/Data-Contract-Hierarchy/
我困惑为什么你必须添加一个类型,然后指定另一个类型作为刚添加的那个类型的子类。在我看来,你只需添加类型联系人,指定其程序集; 主机,那就是它。为什么一个knownType元素标记跟在指定另一个类型的add类型元素标记之后?
此外,一旦我有正确的配置文件设置,什么时候,如何从我的wcf调用它?任何援助将不胜感激。非常感谢!
更新1:
**好的,这让我更好理解,谢谢。我没有尝试你说的话,并且没有找到ServiceKnownTypes。我在我的App.config文件中唯一不同的是在我的服务和主机是,我没有任何knownType type =...来指定。这里是我的一目了然。你有我的想法,我做错了吗?
< system.runtime.serialization>
< dataContractSerializer>
< declaredTypes>
< add type =Data,TestService,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 57f2af9570299a17/>
< / declaredTypes>
< / dataContractSerializer>
< /system.runtime.serialization>
对不起,先前发表评论部分,我希望这更清楚。
更新2:
这是更接近我想完成的事情。您的想法是什么?
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread / 6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof = required
如果您要在配置中指定已知类型,请按照您提到的示例:
< system.runtime。序列化>
< 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>
你不必做任何事情 - 你不必配置或任何东西 - WCF将为你做这个。你需要把它放入你的web.config(如果你在IIS中托管你的服务,如果你的客户端是一个Web应用程序),或在你的应用程序的配置(如果你有一个Windows服务在服务器端,或控制台/ winforms应用程序在客户端)。
基本上,你在这里说的是任何方法,有一个联系
从我的主机
程序集也可以从我的<$ c>返回客户
$ c> MyClassLibrary 。
基本上,你要定义 MyClassLibrary.Customer
很可能是 Host.Contact
的后代类型。
这与定义数据相同合约:
[DataContract]
[KnownType(typeof(Customer))]
class Contact
{...}
您有一个对象类 code>,但是在使用它的任何地方,它也可以是
Customer
类实例。
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
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.
这篇关于如何从外部配置文件添加服务已知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!