我有一个错误。您可以在下面看到我的代码和异常消息。
用户类
public class User
{
public virtual int ID { get; set; }
public virtual string UserName { get; set; }
public virtual string PassWord { get; set; }
public virtual DateTime AddedDate { get; set; }
public virtual UserGroup UserGroup { get; set; }
}
费用收入类别
public class expenseincomes
{
public virtual int ID { get; set; }
public virtual int ExpIncTypeID { get; set; }
public virtual decimal Amount { get; set; }
public virtual User User { get; set; }
}
地图类
public class UserMap : ClassMap<User>
{
public UserMap() {
Table("TUsers");
Id(x => x.ID);
Map(x => x.AddedDate);
Map(x => x.PassWord);
Map(x => x.UserName);
}
}
public class expenseincomesMap : ClassMap<expenseincomes>
{
public expenseincomesMap()
{
Table("texpenseincomes");
Id(x => x.ID);
Map(x => x.Amount);
Map(x=>x.ExpIncTypeID);
References(x => x.User).Column("UserID").Cascade.All();
}
}
Web API 配置类
config.EnableQuerySupport();
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "odata/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
当我调用 [http://localhost:2711/odata/expenseincomes/get] url 时,我收到此异常消息。
"$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":"Cannot serialize a Session while connected","ExceptionType":"System.InvalidOperationException","StackTrace":" at NHibernate.Impl.SessionImpl.System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract 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.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract 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.SerializeList(JsonWriter writer, IWrappedCollection 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.Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"}}
但是URL没有问题=> [http://localhost:2711/odata/Users/get]