Using WCF DataContract in MVC SessionState using AppFabric cache

后端 未结 2 1716
南旧
南旧 2021-02-10 01:05

I have a Data Access Layer, a Service Layer, and a Presentation Layer. The Presentation Layer is ASP.NET MVC2 RTM (web), and the Service Layer is WCF (services). It\'s all .NET

相关标签:
2条回答
  • 2021-02-10 01:44

    As an extension to the provided answer, I added these two methods to ease storing/retrieving the data.

        public static void Set<T>(HttpSessionStateBase session, string key, T value)
        {
            session[key] = new Wrapper(value);
        }
    
        public static T Get<T>(HttpSessionStateBase session, string key)
        {
            object value = session[key];
            if (value != null && typeof(T) == value.GetType())
            {
                return (T) value;
            }
            Wrapper wrapper = value as Wrapper;
            return (T) ((wrapper == null) ? null : wrapper.Value);
        }
    

    This makes it a little easier to set/get values from the session:

        MyDataContract c = ...;
        Wrapper.Set(Session, "mykey", c);
        c = Wrapper.Get<MyDataContract>(Session, "mykey");
    

    To make it even easier, add extension methods:

    public static class SessionWrapperEx
    {
        public static void SetWrapped<T>(this HttpSessionStateBase session, string key, T value)
        {
            Wrapper.Set<T>(session, key, value);
        }
    
        public static T GetWrapped<T>(this HttpSessionStateBase session, string key)
        {
            return Wrapper.Get<T>(session, key);
        }
    }
    

    And use as below:

        MyDataContract c = ...;
        Session.SetWrapped("mykey", c);
        c = Session.GetWrapped<MyDataContract>("mykey");
    
    0 讨论(0)
  • 2021-02-10 02:05

    Without going the full blown route of A or B, could you just make a generic ISerializable wrapper object and put those in your SessionState?

        [Serializable]
        public class Wrapper : ISerializable
        {
            public object Value { get; set; }
    
            void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
            {
                if (Value != null)
                {
                    info.AddValue("IsNull", false);
                    if (Value.GetType().GetCustomAttributes(typeof(DataContractAttribute), false).Length == 1)
                    {
                        using (var ms = new MemoryStream())
                        {
                            var serializer = new DataContractSerializer(Value.GetType());
                            serializer.WriteObject(ms, Value);
                            info.AddValue("Bytes", ms.ToArray());
                            info.AddValue("IsDataContract", true);
                        }
                    }
                    else if (Value.GetType().IsSerializable)
                    {
                        info.AddValue("Value", Value);
                        info.AddValue("IsDataContract", false);
                    }
                    info.AddValue("Type", Value.GetType());
                }
                else
                {
                    info.AddValue("IsNull", true);
                }
            }
    
            public Wrapper(SerializationInfo info, StreamingContext context)
            {
                if (!info.GetBoolean("IsNull"))
                {
                    var type = info.GetValue("Type", typeof(Type)) as Type;
    
                    if (info.GetBoolean("IsDataContract"))
                    {
                        using (var ms = new MemoryStream(info.GetValue("Bytes", typeof(byte[])) as byte[]))
                        {
                            var serializer = new DataContractSerializer(type);
                            Value = serializer.ReadObject(ms);
                        }
                    }
                    else
                    {
                        Value = info.GetValue("Value", type);   
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题