WCF:配置已知类型 [英] WCF: Configuring Known Types

查看:16
本文介绍了WCF:配置已知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 WCF 中配置已知类型.例如,我有一个 Person 类和一个 Employee 类.Employee 类是 Person 类的子类.这两个类都标有 [DataContract] 属性.

I want to know as to how to configure known types in WCF. For example, I have a Person class and an Employee class. The Employee class is a sublass of the Person class. Both class are marked with a [DataContract] attribute.

我不想对类的已知类型进行硬编码,例如在 Person 类中放置一个 [ServiceKnownType(typeof(Employee))] 以便 WCF 知道 Employee 是 Person 的子类.

I dont want to hardcode the known type of a class, like putting a [ServiceKnownType(typeof(Employee))] at the Person class so that WCF will know that Employee is a subclass of Person.

现在,我在主机的 App.config 中添加了以下 XML 配置:

Now, I added to the host's App.config the following XML configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Person, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null">
          <knownType type="Employee, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>

我编译了它,运行了主机,在客户端添加了一个服务引用并添加了一些代码并运行了客户端.但是出现了错误:

I compiled it, run the host, added a service reference at the client and added some code and run the client. But an error occured:

格式化程序抛出异常,而试图反序列化消息:尝试时出错反序列化参数http://www.herbertsabanal.net:person.InnerException 消息是错误在第 1 行位置 247. 元素'http://www.herbertsabanal.net:person'包含数据'http://www.herbertsabanal.net/Data:Employee'数据合同.解串器没有映射到的任何类型的知识本合同.添加类型对应于 'Employee' 到已知类型列表 - 例如,通过使用KnownTypeAttribute 属性或将其添加到已知列表中传递给的类型DataContractSerializer.'.请参见InnerException 了解更多详情.

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://www.herbertsabanal.net:person. The InnerException message was 'Error in line 1 position 247. Element 'http://www.herbertsabanal.net:person' contains data of the 'http://www.herbertsabanal.net/Data:Employee' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'Employee' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

以下是数据合同:

[DataContract(Namespace="http://www.herbertsabanal.net/Data", Name="Person")]
    class Person
    {
        string _name;
        int _age;

        [DataMember(Name="Name", Order=0)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [DataMember(Name="Age", Order=1)]
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }


[DataContract(Namespace="http://www.herbertsabanal.net/Data", Name="Employee")]
    class Employee : Person
    {
        string _id;

        [DataMember]
        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }
    }

顺便说一句,我的服务没有使用类库(WCF 类库或非 WCF 类库).我只是在宿主项目中对其进行了简单编码.

Btw, I didn't use class libraries (WCF class libraries or non-WCF class libraries) for my service. I just plain coded it in the host project.

我猜一定是配置文件有问题(请参阅上面的配置文件).或者我一定错过了什么.任何帮助将不胜感激.

I guess there must be a problem at the config file (please see config file above). Or I must be missing something. Any help would be pretty much appreciated.

推荐答案

我想我现在找到了答案.

I guess I have found the answer now.

我上面贴的配置文件是这样的:

The configuration file I posted above looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Person, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null">
          <knownType type="Employee, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>

我刚刚添加的是 Person 类和 Employee 类的命名空间.并且不需要更长的版本和文化值......正确的配置应该是:

What I just added was, the Namespace of the Person class and the Employee class. And no need for the longer Version and Culture values.... The correct configuration should be:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="WCFWithNoLibrary.Person, WCFWithNoLibrary">
          <knownType type="WCFWithNoLibrary.Employee, WCFWithNoLibrary" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>

现在它更短,更有意义.但如果使用第三方库,则需要添加版本、文化、公钥.

Now it is shorter and makes more sense. But if 3rd party libraries are used, then adding version, culture, publickeytokens would be required.

这篇关于WCF:配置已知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆