两个不同DataContracts之间的对象反序列化问题

Object deserialization problem between two different DataContracts

在我的解决方案架构中,我遵循:

我有对象反序列化问题。在反序列化过程中,我的客户端应用程序出现以下异常。

{"Unable to deserialize XML body with root name 'User' and root namespace 'http://schemas.datacontract.org/2004/07/ECMS.Business.Entities' (for operation 'Login' and contract ('ISecurityService', 'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service."}

ECMS.Business.Entities 项目中我有以下实体

[DataContract]
public class User : IIdentifiableEntity
{
    [DataMember]
    public int Id { get; set; }
    public int EntityId
    {
        get => Id;
        set => Id = value;
    }
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public string FullName { get; set; }
    public string Password { get; set; }
    [DataMember]
    public int Role { get; set; }
    public bool IsActive { get; set; }

    public ICollection<UserSession> Sessions { get; set; }
    public ICollection<Visit> Visits { get; set; }
}

我需要return返回客户端(ECMS.Client.Entities项目)作为以下实体

[DataContract]
public class User : ObjectBase
{
    private int _id;

    [DataMember]
    public int Id
    {
        get => _id;
        set
        {
            if (_id != value)
            {
                _id = value;
                OnPropertyChanged();
            }
        }
    }

    private string _userName;

    [DataMember]
    public string UserName
    {
        get => _userName;
        set
        {

            if (_userName != value)
            {
                _userName = value;
                OnPropertyChanged();
            }
        }
    }

    private string _fullName;

    [DataMember]
    public string FullName
    {
        get => _fullName;
        set
        {
            if (_fullName != value)
            {
                _fullName = value;
                OnPropertyChanged();
            }
        }
    }

    private int _role;
    [DataMember]
    public int Role
    {
        get => _role;
        set
        {
            if (_role != value)
            {
                _role = value;
                OnPropertyChanged();
            }
        }
    }
}

[DataContract]
public abstract class ObjectBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

使用单独的服务器和客户端合同时,DataContact 命名空间必须匹配!

来自 here

For data contracts to be equivalent, they must have the same namespace and name