, ISerializable DataContractSerializer.
:
WCF DataContract MVC SessionState AppFabric
AppFabric (StateServer), SqlSessionStateStore ( OOB StateProviders BinaryFormatter )
, . ( -peasy)
, , , [DataContract], [DataContract] , . ( DataContract - DTO , ). , , .
, , :
[Serializable]
public class SessionContext
{
#region Static
private const string SESSION_CONTEXT_KEY = "SessionContext";
private static object _syncRoot = new object();
public static SessionContext Current
{
get
{
HttpSessionState session = HttpContext.Current.Session;
if (session[SESSION_CONTEXT_KEY] == null)
{
lock (_syncRoot)
{
if (session[SESSION_CONTEXT_KEY] == null)
{
session[SESSION_CONTEXT_KEY] = new SessionContext();
}
}
}
return (SessionContext)session[SESSION_CONTEXT_KEY];
}
}
#endregion
private SessionContext()
{
}
public User User { get; set; }
private CustomerWrapper _customerWrapper = new CustomerWrapper();
[XmlIgnore]
public Customer Customer
{
get
{
return this._customerWrapper.Customer;
}
set
{
this._customerWrapper.Customer = value;
}
}
}
[Serializable]
public class CustomerWrapper : ISerializable
{
public Customer Customer { get; set; }
internal CustomerWrapper() { }
internal CustomerWrapper(Customer customer) : this() { this.Customer = customer; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (this.Customer != null)
{
info.AddValue("IsNull", false);
using (var ms = new MemoryStream())
{
try
{
var serializer = new DataContractSerializer(this.Customer.GetType());
serializer.WriteObject(ms, this.Customer);
info.AddValue("Bytes", ms.ToArray());
info.AddValue("IsDataContract", true);
}
catch (Exception ex)
{
}
}
info.AddValue("Type", this.Customer.GetType());
}
else
{
info.AddValue("IsNull", true);
}
}
public CustomerWrapper(SerializationInfo info, StreamingContext context)
{
if (!info.GetBoolean("IsNull"))
{
var type = info.GetValue("Type", typeof(Type)) as Type;
using (var ms = new MemoryStream(info.GetValue("Bytes", typeof(byte[])) as byte[]))
{
var serializer = new DataContractSerializer(type);
this.Customer = (Customer)serializer.ReadObject(ms);
}
}
}
}