我有一组对象(用于构建强类型视图的ViewModel对象),我从:
-
动态内容的外部J2EE服务。我使用LINQ2XML读取J2EE服务返回的XML响应,以填充我的对象
-
本地DB用于静态内容。我基本上将本地DB的静态内容与J2EE服务返回的动态内容合并在一起。我使用NHibernate和LINQ来做这个操作
一旦我用所需的所有数据填充我的ViewModels,我计划将它们存储在SESSION中(使用AppFabric用于进程外会话状态)。我有[Serializable]
属性为所有我的ViewModel类,因为它需要,如果我计划将它们存储在进程外会话。然而,我得到以下异常:
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Stack Trace:
[SerializationException: Type 'System.Linq.Enumerable+WhereEnumerableIterator`1[[TrvCity.Services.ViewModels.ResortsView, TrvCity.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +9452985
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +247
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +160
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +218
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +388
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +444
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +133
System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1708
我已经彻底验证了我所有的类,它们被标记为[Serializable]
。为了进一步调试,我手动(不使用步骤1 &2)使用MOCK DATA填充它们,并且我能够将它们放在会话中,在这种情况下我不会得到SerializationException。我觉得因为我使用LINQ (LINQ2XML和LINQ2Objects)来填充它们,所以它不喜欢它。
我希望有人能给我一些解决这个问题的指导。
下面是我的对象/类的代码片段:[Serializable]
public class ResortsView : SupplierView
{
public IList<ProductView> ResortProducts { get; set; }
public string CheckInTime { get; set; }
public string CheckOutTime { get; set; }
public virtual IList<ImageView> Images { get; set; }
}
[Serializable]
public class SupplierView
{
public string SupplierID { get; set; }
public string SupplierName { get; set; }
public string Description { get; set; }
public string Rating { get; set; }
public string URL { get; set; }
}
[Serializable]
public class ProductView
{
public string Code { get; set; }
public string Description { get; set; }
public decimal? Price { get; set; }
public ErrorView PricingError { get; set; }
}
这可能是因为延迟加载。在尝试将集合存储在会话中之前,先尝试急切地获取集合:
IEnumerable<ResortsView> resortsViews = ...
Session["resortsViews"] = resortsViews.ToArray();