惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - Leonny

IIS站点/虚拟目录中访问共享目录(UNC)以及建立后的应用程序的信任级别问题 谁说用Azure就一定要Vista/Win7?Window2003/xp下安装 Windows Azure Tools for Microsoft Visual Studio IE的window.showModalDialog出现内存不足的问题 - Leonny - 博客园 关于清理JBoss服务器端jsp文件缓存的问题 提取assemblyinfo.cs文件里的guid值 经常坐电脑旁的人必喝的健康饮品 Firefox图片模糊的问题 MS SQL Server2005如何得到记录的行号 [原创]OWC生成Excel的效能优化 [译]NUnitForms官方说明文档 C#取得MS Word的总页数 VS2008中文版安装ASP.NET MVC Beta WinForm下ComboBox添加项与设定预选项 成功申请了GAE Vista删你文件没商量 3大数据库的变量参数符号 新立得软件包管理器出错 Repeater中點擊按鈕事件時要注意頁面PostBack的問題 如何取得輸入字符串的寬度
關於Web Service 中使用NHibernate時無法序列化IList的問題
Leonny · 2007-09-22 · via 博客园 - Leonny

用NHibernate進行數據庫的ORM操作, 很多清況下都免不了要生成像IList這種泛型接口的對象集合.
但是如果再用到Web Service來進行這種對象的傳輸的話, 就會報錯.
原因是IList為接口類型, 不能被序列化.
今天剛好在項目中碰到這種問題. google了半天, 未果!
沒辦法, 還得自己想辦法.
最後通過以下方法解

//要用到的主要類
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;//Web Service 方法, 假定
[WebMethod(Description = "Input the order No. to get the order's information.")]
public byte[] GetOrderByNo(string orderNo)
{
    BinaryFormatter formatter 
= new BinaryFormatter();
    List
<string> orderList = new List<string>();
    OrderNoBLL onBll 
= new OrderNoBLL();    //以下是從NHibernate中
    OrderBLL bll = new OrderBLL();            //獲得對象的方法,具體的操作
    OrderNo on = onBll.GetByNo(orderNo);    //
    byte[] bs;
    
if (on != null)
    {
        MemoryStream ms 
= new MemoryStream();    //這裡是關鍵
        
//on.Order是一個Order對象, 其中有一個Items的IList<OrderItem>屬性
        formatter.Serialize(ms, on.Order);        //這裡可以進行序列化, ^_^
        bs = ms.ToArray();
    }
    
else
    {
        bs 
= new byte[0];
    }
    
return bs;
}

下面的代碼是Client端的接收和反序列化的代碼:

protected void btn_Click(object sender, EventArgs e)
{
    localhost.WebService service 
= new localhost.WebService();
    
byte[] bs = service.GetOrderByNo(no.Text);    //從這裡取得傳回的二進制數組
    Entity.Order order = null;
    
try
    {
        MemoryStream ms 
= new MemoryStream(bs);    //這裡是關鍵
        BinaryFormatter formatter = new BinaryFormatter();
        order 
= (Entity.Order)formatter.Deserialize(ms);    //再反序列化
        noLb.Text = order.ID;
    }
    
catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
}