wcf 序列化继承 class 错误
wcf serialization inherited class error
我们有两个名为 "Frontend"(FE[= 的 控制台应用程序 50=]) 和 "Backend"(BE),
由 WCF 连接。我需要一个 摘要 class 和一些
在 BE 中继承了 classes,在 运行 时我从
之一实例化了一个对象
Activator 继承的 classes。
每当我想要 return 实例化对象时,就会出现与
相关的错误
序列化。这是我的简化代码
[DataContract]
public abstract class License
{
[DataMember]
public int ManagedObjectCount { get; set; }
}
[DataContract]
public class LicenseMay2018 : License
{
public Frontend.DataTypes.License GetLicenseInfo(xml xml)
{
Frontend.DataTypes.LicenseMay2018 licenseVerified;
var licXML = nodeData[0].InnerText;
//Deserialize license
XmlSerializer _serializer = new XmlSerializer(typeof(LicenseMay2018));
using (StringReader _reader = new StringReader(licXML))
{
licenseVerified = (Frontend.DataTypes.LicenseMay2018)_serializer.Deserialize(_reader);
}
}
}
[DataContract]
public class LicenseApril2018 : License
{
}
在 BE 方面,我 return 通过抽象 class 的类型,期望 return 继承 class,通过 Activator 和实例化,一切都很好。唯一的问题是在方法的最后,当它想 return 到 FE 时,似乎序列化并发回
public Frontend.DataTypes.License ActivateLicense(int LicenseFileId)
{
// create in instance of inehrited class, no matter licensemay2018 or april2018
string assemblyName = "NMS.Common";
var className = GetLicenseType(nodeVersion[0].InnerText);
// exaple : className = licensemay2018
var handle = Activator.CreateInstance(assemblyName, className);
var instance = (Frontend.DataTypes.License)handle.Unwrap();
return instance.GetLicenseInfo(xmlDoc);
}
在 运行 时我不知道确切的类型所以我按父类型创建实例 class,它工作并且创建了确切的对象
在returning的时候会出现这个错误
There was an error while trying to serialize parameter
http://tempuri.org/:ActivateLicenseResult. The InnerException message
was 'Type 'NMS.Frontend.DataTypes.LicenseMay2018' with data contract
name
'LicenseMay2018:http://schemas.datacontract.org/2004/07/NMS.Frontend.DataTypes'
is not expected. Consider using a DataContractResolver if you are
using DataContractSerializer or add any types not known statically to
the list of known types - for example, by using the KnownTypeAttribute
attribute or by adding them to the list of known types passed to the
serializer.'. Please see InnerException for more details.
我不知道这些来自哪里:http://tempuri.org and http://schemas.datacontract.org/2004/07 ??
您需要将您的继承类型告知序列化程序。像这样
为您的继承 类 添加 KnownType 属性
[DataContract]
[KnownType(typeOf(LicenseMay2018))]
[KnownType(typeOf(LicenseApril2018))]
public abstract class License
{
[DataMember]
public int ManagedObjectCount { get; set; }
}
[DataContract]
public class LicenseMay2018 : License
{
public Frontend.DataTypes.License GetLicenseInfo(xml xml)
{
return new licenseMay2018();
}
}
[DataContract]
public class LicenseApril2018 : License
{
}
我们有两个名为 "Frontend"(FE[= 的 控制台应用程序 50=]) 和 "Backend"(BE),
由 WCF 连接。我需要一个 摘要 class 和一些
在 BE 中继承了 classes,在 运行 时我从
之一实例化了一个对象Activator 继承的 classes。
每当我想要 return 实例化对象时,就会出现与
相关的错误序列化。这是我的简化代码
[DataContract]
public abstract class License
{
[DataMember]
public int ManagedObjectCount { get; set; }
}
[DataContract]
public class LicenseMay2018 : License
{
public Frontend.DataTypes.License GetLicenseInfo(xml xml)
{
Frontend.DataTypes.LicenseMay2018 licenseVerified;
var licXML = nodeData[0].InnerText;
//Deserialize license
XmlSerializer _serializer = new XmlSerializer(typeof(LicenseMay2018));
using (StringReader _reader = new StringReader(licXML))
{
licenseVerified = (Frontend.DataTypes.LicenseMay2018)_serializer.Deserialize(_reader);
}
}
}
[DataContract]
public class LicenseApril2018 : License
{
}
在 BE 方面,我 return 通过抽象 class 的类型,期望 return 继承 class,通过 Activator 和实例化,一切都很好。唯一的问题是在方法的最后,当它想 return 到 FE 时,似乎序列化并发回
public Frontend.DataTypes.License ActivateLicense(int LicenseFileId)
{
// create in instance of inehrited class, no matter licensemay2018 or april2018
string assemblyName = "NMS.Common";
var className = GetLicenseType(nodeVersion[0].InnerText);
// exaple : className = licensemay2018
var handle = Activator.CreateInstance(assemblyName, className);
var instance = (Frontend.DataTypes.License)handle.Unwrap();
return instance.GetLicenseInfo(xmlDoc);
}
在 运行 时我不知道确切的类型所以我按父类型创建实例 class,它工作并且创建了确切的对象
在returning的时候会出现这个错误
There was an error while trying to serialize parameter http://tempuri.org/:ActivateLicenseResult. The InnerException message was 'Type 'NMS.Frontend.DataTypes.LicenseMay2018' with data contract name 'LicenseMay2018:http://schemas.datacontract.org/2004/07/NMS.Frontend.DataTypes' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.'. Please see InnerException for more details.
我不知道这些来自哪里:http://tempuri.org and http://schemas.datacontract.org/2004/07 ??
您需要将您的继承类型告知序列化程序。像这样
为您的继承 类 添加 KnownType 属性[DataContract]
[KnownType(typeOf(LicenseMay2018))]
[KnownType(typeOf(LicenseApril2018))]
public abstract class License
{
[DataMember]
public int ManagedObjectCount { get; set; }
}
[DataContract]
public class LicenseMay2018 : License
{
public Frontend.DataTypes.License GetLicenseInfo(xml xml)
{
return new licenseMay2018();
}
}
[DataContract]
public class LicenseApril2018 : License
{
}