开发者

Store linq-to-sql objects in session, or alternate solution

开发者 https://www.devze.com 2023-03-17 04:26 出处:网络
I am storing my session on a session-state server. (Storing it inproc doesn\'t work.) I am trying to use the classes generated by the linq-to-sql-designer built in to visual studio (2010) for objects

I am storing my session on a session-state server. (Storing it inproc doesn't work.)

I am trying to use the classes generated by the linq-to-sql-designer built in to visual studio (2010) for objects to store and represent some data in my website.

On some occasions, I want to put a few of those objects into the session to be able to access them on another webpage.

The problem is that when I do that, I get an error that some parts of the linq-to-sql stuff is not serializable.

I have set my datacontext "serialization mode" to "unidirectional", whatever that means.

Then I created some alternate object that would be better able to be serialized, replacing some sql-xmldata fields that were translated to linq.xelement fields with a basic string that contains some xml. The objects then only contain basic types.

Still errors.

Then I tried using the DataContractJsonSerializer to get a byte[] that I can put into the session:

byte[] buf = null;
var ds = new DataContractJsonSerializer(fi.GetType());
using (var ms = new MemoryStream())
{
  using (var w = JsonReaderWriterFactory.CreateJsonWriter(ms))
  开发者_高级运维{
    ds.WriteObject(w, fi);
    ms3.Position = 0;
    buf = ms.ToArray();
  }
}
return buf;

The problem here is that the Json-string seems to be cut off in the middle of some element and therefore cannot be deserialized afterwards. When I tried with the XmlWriter I had a problem that it cut off the xml text at around 43000 bytes for some reason.

This is turning out to be pretty messy.

There has to be a better way.

One idea I got was to simply submit the objects to some temporary sql server table, get the guid from the sql server row, store the guid in the session instead of the entire set of objects, and then reread the data from the sql server on the other page.

What is a better solution that actually works?


Try to Flush the JsonWriter before reading the MemoryStream:

...
ds.WriteObject(w, fi);
w.Flush();
ms3.Position = 0;
buf = ms.ToArray();
...
0

精彩评论

暂无评论...
验证码 换一张
取 消