类型不可序列化,因为它不是公共datacontractserializer

时间:2013-12-27 18:57:39

标签: c# wcf serialization datacontractjsonserializer

我是wcf和windows phone 8应用程序的新手,并且调用wcf服务(其他开发人员)并试图反序列化它有时工作正常的数据,但大多数时候它给了我错误

  

“数据合约类型   'TEST.Model.Response`1 [[TEST.Model.Announcement,TEST,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'不可以   反序列化,因为成员'ResponseData'不公开。制造   成员公众将修复此错误。或者,你可以做到   内部,并使用您的InternalsVisibleToAttribute属性   汇编以启用内部成员的序列化 - 请参阅   文档了解更多详情。请注意这样做是肯定的   安全隐患。“

MY Model Class

public class Response<T> where T: class
{
    public string MethodName { get; set; }

    public int ResponseCode { get; set; }

    public string ResponseMessage { get; set; }

    public List<T> ResponseData { get; set; }

}

public class Announcement
{
    public int AnnouncementId { get; set; }

    public string AnnouncementTitle { get; set; }

    public string CreatedBy { get; set; }

    public string CreatedDate { get; set; }

    public string DataShortVersion { get; set; }

    public string ModifiedBy { get; set; }

    public string ModifiedDate { get; set; }

    public int SortIndex { get; set; }
}

当我尝试打电话为

var request = ar.AsyncState as WebRequest;
Stream reader = request.EndGetResponse(ar).GetResponseStream(); 

DataContractJsonSerializer jsonSerializer ;\\
jsonSerializer = new DataContractJsonSerializer(typeof(TEST.Model.Response<TEST.Model.Announcement>));
Response<Announcement> objResponse = (Response<Announcement>)jsonSerializer.ReadObject(reader); //error raised

1 个答案:

答案 0 :(得分:2)

以下SSCCE代码有效。尝试自己创建一个问题,看看它与下面的问题有什么不同。这可能会引导您回答。

public class StackOverflow_20806241
{
    public class Response<T> where T : class
    {
        public string MethodName { get; set; }
        public int ResponseCode { get; set; }
        public string ResponseMessage { get; set; }
        public List<T> ResponseData { get; set; }
    }
    public class Announcement
    {
        public int AnnouncementId { get; set; }
        public string AnnouncementTitle { get; set; }
        public string CreatedBy { get; set; }
        public string CreatedDate { get; set; }
        public string DataShortVersion { get; set; }
        public string ModifiedBy { get; set; }
        public string ModifiedDate { get; set; }
        public int SortIndex { get; set; }
    }
    public static void Test()
    {
        string JSON = @"{'MethodName':'m1','ResponseCode':1,'ResponseMessage':'msg','ResponseData':[
            {'AnnouncementId':1,'AnnouncementTitle':'t1','CreatedBy':'c1','CreatedDate':'d1','DataShortVersion':'v1','ModifiedBy':'m1','ModifiedDate':'md1','SortIndex':1},
            {'AnnouncementId':2,'AnnouncementTitle':'t2','CreatedBy':'c2','CreatedDate':'d2','DataShortVersion':'v2','ModifiedBy':'m2','ModifiedDate':'md2','SortIndex':2}
        ]}";
        JSON = JSON.Replace('\'', '\"');
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JSON));
        DataContractJsonSerializer jsonSerializer;
        jsonSerializer = new DataContractJsonSerializer(typeof(Response<Announcement>));
        try
        {
            var objResponse = (Response<Announcement>)jsonSerializer.ReadObject(ms);
            Console.WriteLine("Response:");
            Console.WriteLine("  {0} - {1} - {2}", objResponse.MethodName, objResponse.ResponseCode, objResponse.ResponseMessage);
            Console.WriteLine("  [{0}]", string.Join(", ", objResponse.ResponseData.Select(rd =>
                string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}",
                    rd.AnnouncementId, rd.AnnouncementTitle,
                    rd.CreatedBy, rd.CreatedDate,
                    rd.DataShortVersion, rd.ModifiedBy,
                    rd.ModifiedDate, rd.SortIndex))));
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex);
        }
    }
}