2010-08-29 194 views
4

我能夠返回一個圖片從我的.NET Web服務的字節數組..從.NET Web服務

我的問題是,返回一個以上的圖像,如果我想返回不止什麼一個請求中的單個圖像。 例如,我的網站目前的方法看起來是這樣的:

public byte[] GetImage(string filename) 

。但我試圖找出是我怎麼會實現更多的東西是這樣的:

public ..?... GetAllMemberStuffInOneHit(string memberID) 

這例如,返回一些圖像,以及一些其他信息,例如成員姓名和部門。

可以這樣做嗎?請幫助,謝謝。

+0

什麼樣的網絡服務? WCF或ASMX? – 2010-08-29 02:11:34

回答

4

絕對。不要只返回一個字節數組,而要創建一個具有字節數組以及潛在的其他有用信息(文件名,內容類型等)的DTO類。然後,只需返回這些IList。事情是這樣的:

public class MyImage 
{ 
    public byte[] ImageData { get; set; } 
    public string Name { get; set; } 
    public MyImage() 
    { 
     // maybe initialize defaults here, etc. 
    } 
} 

public List<MyImage> GetAllMemberStuffInOneHit(string memberID) 
{ 
    // implementation 
} 

因爲你的方法,顧名思義是要返回更多的信息,您可以創建更多的DTO類,並建立他們共同創造一個「成員」對象。事情是這樣的:

public class Member 
{ 
    public List<MyImage> Images { get; set; } 
    public string Name { get; set; } 
    public DateTime LastLogin { get; set; } 
    // etc. 
} 

public Member GetAllMemberStuffInOneHit(string memberID) 
{ 
    // implementation 
} 
+0

謝謝,生病讓你知道它是如何:) – Mikey 2010-08-29 03:00:21

+0

你們是輝煌!謝謝,它工作。 最後一件事,在我的客戶端應用程序中,我必須更改配置值-BufferSize和MaxArrayLength等......我將它們分別從16384和65536更改爲116384和165536。 爲什麼這些最初是如此限制,增加這些項目是否存在風險? – Mikey 2010-08-29 03:34:58

+0

@Mikey:唯一的風險就是允許大量的數據包通過服務邊界傳輸。在我上一份工作中,我們把它定爲2 GB,這有點令人不安,但工作得很好。 – David 2010-08-29 11:55:21

1

一個簡單的解決方法就是返回一個List<byte[]>集合,像這樣:

[WebMethod] 
public List<byte[]> GetAllImages(string memberID) 
{ 
    List<byte[]> collection = new List<byte[]>(); 
    // fetch images one at a time and add to collection 
    return collection; 
} 

要使用此,你需要這條線在你Service.cs文件的頂部:

using System.Collections.Generic;