以下是我的web-api
。
public HttpResponseMessage GetMeterPing(DateTime start, DateTime end)
{
try
{
var startDateTime = start;
var endDateTime = end;
var result = medEntitites.tj_xhqd.Where(m => m.sjsj >= startDateTime && m.sjsj <= endDateTime)
.OrderByDescending(o => o.sjsj)
.Select(s => new { s.zdjh, s.sjsj, s.xhqd });
.Distinct();
return Request.CreateResponse(HttpStatusCode.OK, new { data = result });
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
WEB-API UR:L http://localhost:14909/api/meters/GetMeterPing/2018-04-27T00:00:00/2018-04-28T23:59:59
当我运行上述网址时,我遇到了异常
{
"$id": "1",
"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": {
"$id": "2",
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred while reading from the store provider's data reader. See the inner exception for details.",
"ExceptionType": "System.Data.Entity.Core.EntityCommandExecutionException",
"StackTrace": " at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.HandleReaderException(Exception e)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead()\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()",
"InnerException": {
"$id": "3",
"Message": "An error has occurred.",
"ExceptionMessage": "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.",
"ExceptionType": "MySql.Data.MySqlClient.MySqlException",
"StackTrace": " at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)\r\n at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)\r\n at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)\r\n at MySql.Data.MySqlClient.MySqlDataReader.Read()\r\n at MySql.Data.Entity.EFMySqlDataReader.Read()\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead()",
"InnerException": {
"$id": "4",
"Message": "An error has occurred.",
"ExceptionMessage": "Timeout in IO operation",
"ExceptionType": "System.TimeoutException",
"StackTrace": " at MySql.Data.MySqlClient.TimedStream.StopTimer()\r\n at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)\r\n at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)\r\n at MySql.Data.MySqlClient.MySqlStream.LoadPacket()\r\n at MySql.Data.MySqlClient.MySqlStream.ReadPacket()\r\n at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)\r\n at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)\r\n at MySql.Data.MySqlClient.ResultSet.GetNextRow()\r\n at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)\r\n at MySql.Data.MySqlClient.MySqlDataReader.Read()"
}
}
}
}
WebApiConfig
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
的Application_Start()
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters
.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
此外,我尝试了.ToList()
但无法获得理想的结果。
注意:我正在使用MySQL Database
更新1:
var result = medEntitites.tj_xhqd.Where(m => m.sjsj >= startDateTime && m.sjsj <= endDateTime)
.OrderByDescending(o => o.sjsj)
.Select(s => new { s.zdjh, s.sjsj, s.xhqd })
.Distinct()
.FirstOrDefault();
使用FirsOrDefault()
我收到一行
{
"data": {
"zdjh": "002999001089",
"sjsj": "2018-04-27T00:00:00",
"xhqd": "19"
}
}
但我希望API在给定的日期时间之间返回所有记录
如何摆脱这些例外?
任何帮助都将受到高度赞赏。