我怎样才能用.net core 2.0替换SoapFormatter序列化和反序列化?

时间:2018-05-14 00:39:21

标签: asp.net-core wcf-serialization

我正在努力将旧的wcf项目迁移到.net core 2.0。在某些地方,代码使用SoapFormatter类来序列化和反序列化Soap格式的数据。在.net core 2.0中不再支持Soap Formatter所以我需要替换这段代码才能开始使用XMLSerialization。已经有一些数据存储在数据库中,使用SoapFormatter进行序列化,因此我无法迁移已存储的数据。有没有办法我可以使用Xml序列化序列化和反序列化已存在的数据而不会破坏它?

使用soapformatter进行反序列化的代码片段是:

var buffer = Convert.FromBase64String(piqBlob);
---------

public UserQuestionAnswers Deserialize(byte[] buffer)
        {
        using (var stream = new MemoryStream(buffer))
        {
            var questionsAnswers =
                Serializer.Deserialize(stream) as BusinessProcesses.Authentication.UserQuestionAnswers;
            if (questionsAnswers == null) return null;

            var uQuestionAnswers = new UserQuestionAnswers
            {
                Domain = questionsAnswers.Domain,
                KeyLabel = questionsAnswers.KeyLabel,
                Username = questionsAnswers.Username,
                UserUid = questionsAnswers.UserUid,
                QuestionAnswers = (from item in questionsAnswers.QuestionAnswers
                                   where item != null
                                   select new QuestionAnswer
                                   {
                                       QuestionId = item.QuestionId,
                                       QuestionHash = item.QuestionHash,
                                       AnswerHmac = item.AnswerHMAC,
                                       QuestionText = string.Empty
                                   }).ToArray()
            };
            return uQuestionAnswers;
        }
    }

如果您需要更多相关信息,请与我们联系。 如果评价很高,我也对任何第三方库开放?

0 个答案:

没有答案