gpt4 book ai didi

wcf - WCF:配置已知类型

转载 作者:行者123 更新时间:2023-12-04 13:30:09 26 4
gpt4 key购买 nike

我想知道如何在WCF中配置已知类型。例如,我有一个人员类和一个雇员类。 员工类是人员类的子集。这两个类都标记有[DataContract]属性。

我不想硬编码类的已知类型,例如将[ServiceKnownType(typeof(Employee))]放在Person类上,以便WCF知道Employee是Person的子类。

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

<?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>

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

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.



以下是数据契约(Contract):
[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类库)。我只是在宿主项目中对其进行了普通编码。

我猜配置文件一定有问题(请参见上面的配置文件)。否则我一定会丢失一些东西。任何帮助将不胜感激。

最佳答案

我想我已经找到答案了。

我上面发布的配置文件如下所示:

<?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>

我刚刚添加的是 人员类的命名空间和 员工类的命名空间。并且不需要更长的版本和区域性值。...正确的配置应为:
<?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>

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

关于wcf - WCF:配置已知类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/560218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com