将 linq-to-sql 对象存储在会话中,或替代解决方案

发布于 2024-11-18 07:44:17 字数 1024 浏览 7 评论 0原文

我将会话存储在会话状态服务器上。 (将其存储在进程中不起作用。)

我尝试使用 Visual Studio (2010) 中内置的 linq-to-sql-designer 生成的类作为对象来存储和表示我的网站中的一些数据。

在某些情况下,我想将其中一些对象放入会话中,以便能够在另一个网页上访问它们。

问题是,当我这样做时,我收到一个错误,指出 linq-to-sql 的某些部分不可序列化。

我已将数据上下文“序列化模式”设置为“单向”,无论这意味着什么。

然后,我创建了一些能够更好地序列化的备用对象,用包含一些 xml 的基本字符串替换一些已转换为 linq.xelement 字段的 sql-xmldata 字段。这些对象仅包含基本类型。

仍然错误。

然后我尝试使用 DataContractJsonSerializer 获取可以放入会话中的 byte[]:

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;

这里的问题是 Json 字符串似乎在某些元素的中间被切断,因此之后无法反序列化。当我尝试使用 XmlWriter 时,我遇到了一个问题,它由于某种原因在大约 43000 字节处截断了 xml 文本。

事实证明这非常混乱。

必须有更好的方法。

我的一个想法是简单地将对象提交到某个临时 sql server 表,从 sql server 行获取 guid,将 guid 存储在会话中而不是整个对象集,然后从 sql server 重新读取数据另一页。

什么是真正有效的更好的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-11-25 07:44:17

在读取 MemoryStream 之前尝试刷新 JsonWriter:

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

Try to Flush the JsonWriter before reading the MemoryStream:

...
ds.WriteObject(w, fi);
w.Flush();
ms3.Position = 0;
buf = ms.ToArray();
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文