내 웹 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
일반 문자열에 추가하는 내 자신의 함수 를 작성 하는 것입니다. 이 문자열을 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;
}
}
}
}
전체 프로젝트를 살펴 봤는데 문제는 내 열거 형에서 비롯된 것으로 보이며 직렬화 할 수 없습니다.
모든 도움을 주시면 대단히 감사하겠습니다.
각 enum 클래스에 대해 빈 생성자를 추가하여 작동하게했습니다.
따라서 CreditRecommendation 클래스의 경우 다음과 같습니다.
public CreditRecommendation() { }
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다