【发布时间】:2016-06-27 09:15:49
【问题描述】:
我无法从我的 web 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);
}
}
};
其余枚举模型看起来相同,但只是名称不同,并且在它们各自的 switch case 下或多或少有一些选项。
我现在一直在尝试做的是编写我自己的 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;
}
}
}
}
我已经浏览了整个项目,问题似乎源于我的枚举并且它们无法序列化。
非常感谢任何和所有帮助。
【问题讨论】:
-
您在谈论枚举,但您正在向我们展示课程。我不明白你的方法,你为什么使用一个类来表示一个简单的枚举?
-
@FedericoDipuma 我从一位已辞职的前同事那里继承了其中的一些代码。我不是 100% 确定所有的设计选择,但我正在尽力理解它们。
-
@Gurkang - 你标记了这个问题json 但异常错误消息提到了 XML。你用的是哪个?您正在尝试进行单例序列化,它在每个序列化程序中的工作方式都不同(或者对于
XmlSerializer根本没有,请参阅XmlSerialization with a singleton。
标签: c# json serialization asp.net-web-api enums