WCF InvalidDataContractException: "Type cannot be serialized" 但标记正确

WCF InvalidDataContractException: "Type cannot be serialized" but is marked correctly

我收到 InvalidDataContractException:

Type 'LicenseWatchingServiceLibrary.LicenseInfoContainerExpiring' cannot be serialized. Consider marking it [...]

但它已经被标记(见代码)。我已经搜索过,但 none 我找到的解决方案对我有用。此处使用 WCF 在 Windows 服务和 ui 应用程序之间进行通信。部分代码:

抛出异常的位,位于windowswcf服务应用中:

private void Run()
    {
        //[...]

        List<LicenseInfoContainerExpiring> expiringLicenses;
        List<LicenseInfoContainerUntrusted> untrustedLicenses;
        readLicenseInfoLists(out expiringLicenses, out untrustedLicenses, out warningNeeded);
        if (warningNeeded)
        {
            LWSProxy.WatchLicenses(expiringLicenses, untrustedLicenses); //breaks here
        }
    }

class 库中的接口

[ServiceContract]
public interface ILicenseWatchingServiceUXHost
{
    [OperationContract]
    void WatchLicenses(List<LicenseInfoContainerExpiring> elcs, List<LicenseInfoContainerUntrusted> ulcs);
}

LicenseInfoContainerExpiring.cs,也位于 class 库中:(所有这些,以防万一我忘记了什么地方)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace LicenseWatchingServiceLibrary
{
    [DataContract]
    public partial class LicenseInfoContainerExpiring : ILicenseInfoContainer
    {
        #region precode

        #region properties
        [DataMember]
        public string licenseName { get; private set; } 
        [DataMember]
        public DateTime licenseExpirationDate { get; private set; }
        [DataMember]
        public int licenseDaysUntilExpired { get; private set; } 
        [DataMember]
        public string licenseType { get; private set; } 
        [DataMember]
        public string licenseActivationID { get; private set; } 
        #endregion

        #region objects
        [DataMember]
        private CultureInfo ci = new CultureInfo("en-US"); 
        #endregion

        #endregion

        #region constructor

        public LicenseInfoContainerExpiring() { }

        public LicenseInfoContainerExpiring(string name, string ID, string type, string expirationDate)
        {
            licenseName = name;
            licenseType = type;
            licenseActivationID = ID;

            //calculate expiration
            if (expirationDate != "permanent")
            {
                licenseDaysUntilExpired = (DateTime.ParseExact(expirationDate, "dd-MMM-yyyy", ci) - DateTime.Now.Date).Days;
            }
            else
            {
                licenseDaysUntilExpired = 9999999;
            }
        }
        #endregion

    }
}

添加已知类型:

[DataContract]
[KnownType(typeof(GregorianCalendar))]
public partial class LicenseInfoContainerExpiring : ILicenseInfoContainer
{}

您的数据成员具有私有设置器。那么就不可能在反序列化时设置它的值。也制作二传手 public。

请记住,您的数据协定是在反序列化时甚至没有调用默认构造函数的情况下实例化的。

namespace LicenseWatchingServiceLibrary
{
    [DataContract]
    public partial class LicenseInfoContainerExpiring : ILicenseInfoContainer
    {
        #region precode

        #region properties
        [DataMember]
        public string licenseName { get; set; } 
        [DataMember]
        public DateTime licenseExpirationDate { get; set; }

        // ...
    }
}