为Web API响应序列化类型的安全枚举



我很难以正确的方式从我的Web API中发送回我的模型。

我得到此例外消息:The 'ObjectContent 1 ' 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并将它们附加到普通字符串中。我当时想只是将此字符串进行硬编码。但是我认为这不是解决此问题并处理此问题的正确方法。

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

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

我已经完成了整个项目,问题似乎源于我的枚举,而它们无法序列化。

任何帮助都非常感谢。

从我可以看到您的代码需要重构的内容。将课程用于枚举类型确实是一个糟糕的设计选择,没有优势。

重构类型,然后使用简单的枚举:

public enum CreditRecommendation {
    Buy,
    Sell,
    Hold,
    NoRating
}

然后,如果您希望它们在JSON响应中序列化为字符串,只需将正确的转换器添加到Web API JSON Serialializer实例:

 config.Formatters.JsonFormatter.SerializerSettings.Converters.Add
            (new Newtonsoft.Json.Converters.StringEnumConverter());

假设config是您的HttpConfiguration实例。

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

因此,对于信用授权课程,现在看起来像这样:

public CreditRecommendation() { }

最新更新