【问题标题】:Object deserialization problem between two different DataContracts两个不同DataContracts之间的对象反序列化问题
【发布时间】:2019-05-07 23:43:29
【问题描述】:

在我的解决方案架构中,我关注的是:

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

{"无法使用 DataContractSerializer 反序列化具有根名称 'User' 和根命名空间 'http://schemas.datacontract.org/2004/07/ECMS.Business.Entities' 的 XML 主体(用于操作 'Login' 和合同 ('ISecurityService', 'http://tempuri.org/'))。确保将 XML 对应的类型添加到服务的已知类型集合中。"}

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; }
}

我需要作为以下实体返回客户端(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));
    }
}

【问题讨论】:

标签: c# wcf


【解决方案1】:

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

来自here

为了使数据合约等价,它们必须具有相同的命名空间 和名字

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多