views:

36

answers:

2

Hello people,

I'm able to return an image as byte array from my .NET web service..

My question is this, what if i want to return more than a single image in one request. For example, my web method currently looks something like this:

public byte[] GetImage(string filename)

BUT what I'm trying to work out is how i would achieve something more like this:

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

This would return, for example, a few images, along with some other information such as a members name and department.

Can this be done? Please help, Thank you.

+4  A: 

Absolutely. Instead of returning just a byte array, create a DTO class that has the byte array as well as potentially other useful information (file name, content type, etc.). Then just return an IList of those. Something like this:

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
}

Since your method name implies that you want to return even more information, you could create more DTO classes and build them together to create a "member" object. Something like this:

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
}
David
Thank you, ill let you know how it goes :)
Mikey
You guys are brilliant! Thanks, it worked.One final thing, in my client app, i had to change config values -BufferSize and MaxArrayLength etc... I changed them from 16384 and 65536 to 116384 and 165536 respectively. Why are these initially so restrictive, are there any risks in increasing these items?
Mikey
@Mikey: The only risk, really, is allowing large data packets to be transferred across the service boundary. At my last job we had it set at 2 GB, which was a little unsettling but worked fine.
David
+1  A: 

A simple solution is to just return a List<byte[]> collection, like this:

[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;
}

To use this, you'll need this line at the top of your Service.cs file:

using System.Collections.Generic;
MusiGenesis