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

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security 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);
    }
}