我尝试执行GET
请求并返回List
个clsGWKlant
个对象。但我继续为属性JsonSerializationException
获取prpCreditCardNummer
,表示从字符串到double的转换无效。当我使用调试器时,我可以看到clsGWKlant
对象已正确添加到列表中而没有任何错误。但是返回列表会给出错误。我也想知道异常消息中转换为Double
错误的位置是什么? clsGWKlant
和clsGWaySnelStart
是来自dll
的类型。如何解决此错误?
我试过了:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
哪个不起作用,我也希望我的Web API仍然能够以JSON和XML返回响应
使用GET
Advanced Rest Client Application (Google Chrome extension)
时出错
{
Message: "An error has occurred."
ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."
ExceptionType: "System.InvalidOperationException"
StackTrace: null
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Error getting value from 'prpCreditCardNummer' on 'SnelStartGatewayNET.clsGWKlant'."
ExceptionType: "Newtonsoft.Json.JsonSerializationException"
StackTrace: " at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "The conversion from string to type Double is invalid."
ExceptionType: "System.InvalidCastException"
StackTrace: " at SnelStartGatewayNET.clsGWKlant.get_prpCreditCardNummer() at GetprpCreditCardNummer(Object ) at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Input string was not in a correct format."
ExceptionType: "System.FormatException"
StackTrace: " at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)"
}-
}-
}-
}
HTTP GET
方法
[HttpGet]
public IEnumerable<clsGWKlant> Get()
{
var klant = new clsGWKlant();
var klanten = new List<clsGWKlant>();
var gway = new clsGWaySnelStart();
gway.mtdGWayAdmiOpenenViaLoginSettings(0, Extensions.LoginSettings("databasename"));
var done = false;
while (!done)
{
if (klant.mtdGWayKlantReadNext() == false)
{
Console.WriteLine("No more customers found");
done = true;
break;
}
klanten.Add(klant);
}
gway.mtdGWayAdmiSluiten();
return klanten;
}
clsGWKlant.cs creditcardnumber property
public int prpCreditCardNummer
{
get
{
try
{
return Conversions.ToInteger(this.varClsGWKlantType.GetProperty("prpCreditCardNummer").GetValue(RuntimeHelpers.GetObjectValue(this.varClsGWKlant), new object[0]));
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
throw ex.InnerException;
}
}
set
{
try
{
this.varClsGWKlantType.GetProperty("prpCreditCardNummer").SetValue(RuntimeHelpers.GetObjectValue(this.varClsGWKlant), (object) value, new object[0]);
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
throw ex.InnerException;
}
}
}