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

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 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);
    }
}