序列化Web api响应的类型安全枚举?

古尔康

我很难从网络api以正确的方式将模型发送回去。

我收到以下异常消息:The 'ObjectContent1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

这是我的模型的样子:

public class ValidationCreditReport
    {
        public int companyId { get; set; }
        public string companyName { get; set; }
        public CreditRecommendation recommendationSecure { get; set; }
        public CreditRecommendation recommendationUnsecure { get; set; }
        public CreditOutlook creditOutlook { get; set; }
        public IndicativeRating indicativeCorporateRating { get; set; }
        public IndicativeRating indicativeSeniorUnsecured { get; set; }
        public IndicativeRating indicativeSeniorSecured { get; set; }
        public Currency currency { get; set; }

        public override string ToString()
        {
            return companyId + " " + companyName + " " + recommendationSecure.Name + " " + recommendationUnsecure.Name + " " +
                creditOutlook.Name + " " + indicativeCorporateRating.Name + " " + indicativeSeniorSecured.Name + " " + indicativeSeniorSecured.Name + " " + currency.Name;
        }
    }

这是我的枚举类型:

public class CreditRecommendation
    { 
        public string Name{ get; set; }
        private CreditRecommendation (string name)
        {
            Name = name;
        }

        public static CreditRecommendation Buy = new CreditRecommendation("Buy");
        public static CreditRecommendation Sell = new CreditRecommendation("Sell");
        public static CreditRecommendation Hold = new CreditRecommendation("Hold");
        public static CreditRecommendation NoRating = new CreditRecommendation("NoRating");

        public static CreditRecommendation FromName(string name)
        {
            switch (name)
            {
                case "Buy":
                    return Buy;
                case "Sell":
                    return Sell;
                case "Hold":
                    return Hold;
                case "NoRating":
                    return NoRating;
                default:
                    throw new ArgumentException("Provided name invalid: " + name);
            }
        }
    };

其余的枚举模型看起来相同,但名称不同,并且在各自的切换情况下有更多或更少的选择。

我现在试图做的是编写自己的toString()函数,该函数调用枚举.name并将它们追加到普通字符串中。我当时只是想将此字符串硬编码为json字符串。但是我认为这不是解决这个问题和处理这个问题的正确方法。

最后,这是我实际尝试将数据发送回去的地方:

public HttpResponseMessage GetLastPublishedData(String companyName)
        {
            using (SqlConnection connection = CreateSqlConnection())
            {
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var service = CreateCreditService(connection, transaction);
                        var msg = service.ValidateAndPrepareCreditReport(companyName);
                        var result = CreateValidationCreditReport(msg);
                        transaction.Commit();
                        return this.Request.CreateResponse<ValidationCreditReport>(HttpStatusCode.OK, result);
                    }
                    catch{
                        transaction.Rollback();
                        throw;
                    }

                }
            }
        }

我遍历了整个项目,问题似乎源于我的枚举,并且它们无法序列化。

任何帮助都将不胜感激。

古尔康

我通过为每个枚举类添加一个空的构造函数来使其工作。

因此,对于CreditRecommendation类,现在看起来像这样:

public CreditRecommendation() { }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章