Put objects that are decorated with [DataContract] in a StateServer?

In any case, to place objects decorated with attributes DataContract, but not decorated with attributes Serializable, in SqlServer StateServer? In other words, I would prefer not to decorate these objects with an attribute Serializable, since I will also have to implement IXmlSerizable for all these objects, because they do not have empty constructors and non-public setters for properties.

+3
source share
2 answers

There is no absolutely easy way to do this, no, unfortunately.

: ASP.NET , ASP.NET, DataContractSerializer SQL Server ( , ).

MSDN -.

- .

[Serializable] ...

+2

, ISerializable DataContractSerializer.

:

WCF DataContract MVC SessionState AppFabric

AppFabric (StateServer), SqlSessionStateStore ( OOB StateProviders BinaryFormatter )

, . ( -peasy)

, , , [DataContract], [DataContract] , . ( DataContract - DTO , ). , , .

, , :

/// <summary>
/// Represents a thread-safe, per-session state object, stored in the ASP.NET Session. 
/// </summary>
[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();
    /// <summary>
    /// ignore serializing the Customer object because we're serializing the wrapper object instead, which uses the more robust DataContractSerializer.
    /// </summary>
    [XmlIgnore]
    public Customer Customer
    {
        get
        {
            return this._customerWrapper.Customer;
        }
        set
        {
            this._customerWrapper.Customer = value;
        }
    }

}

/// <summary>
/// Custom wrapper object to instruct the OutOfProc StateProvider how to serialize this item. 
/// Instead of using the BinaryFormatter, we'll use the more robust DataContractSerializer.
/// </summary>
[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) 
                { // breakpoint never hit

                }
            }

            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);
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1736731/


All Articles