Skip to main content

Microsoft Silverlight

Answered Question return a complex object from a web service to silverlight clientRSS Feed

(0)

hereitcomes
hereitcomes

Member

Member

5 points

9 Posts

return a complex object from a web service to silverlight client

 Hey there,

I'm developing a photo service for users to upload their photos to a central server and be able to download them again in a client on another machine.  I have developed an asmx web service which returns photos individually by a Base64 encoded string.  How can I return the photo data AND the photo name in one go?  Can I encode an object of a custom class and extract that again at the other end?  Also, is isolated file storage limited to 1mb per file or 1mb total?

Thanks for any advice

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: return a complex object from a web service to silverlight client

You can add name and other thing with a list

public List<string> get_photo
{
List<string> k = new List<string>();

k.Add(photo_base64);
k.Add(photo_Name);
k.Add(.....);

return k;
}

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: return a complex object from a web service to silverlight client

For the base64, suppose you have got a string

string Base64Image; 

void GetImageCompleted...{
    if (e.Error == null)
    {
        Base64Image = e.Result[0];
    }
}

//..... Decode here

if (Base64Image != null)
{
     byte[] imageData = Convert.FromBase64String(Base64Image);
     MemoryStream ms = new MemoryStream(imageData);
     BitmapImage tempImage = new BitmapImage();
     tempImage.setSource(ms);
     ms.Dispose();
     Image resultImage = tempImage
}

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

hereitcomes
hereitcomes

Member

Member

5 points

9 Posts

Re: Re: return a complex object from a web service to silverlight client

 thanks for your advice, but "e.Result" is a string and can't be cast to a List<string>, so e.Result[0] returns a char.... what am I doing wrong?

My web service is an asmx web application (developed in visual studio) which returns a string.  The string is either an error string or a base64 encoded byte[] of photo data.

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: return a complex object from a web service to silverlight client

You can add name and other thing with a list

public List<string> get_photo
{
List<string> k = new List<string>();

k.Add(photo_base64);
k.Add(photo_Name);
k.Add(.....);

return k;
}

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

hereitcomes
hereitcomes

Member

Member

5 points

9 Posts

Re: Re: Re: return a complex object from a web service to silverlight client

 You said that already Steve, I'm assuming that's your recommendation for the web service method.  That's fine, but on the silverlight end, you can only access the result as "e.Result", which is just a string, and I can't cast that back to a List<string>

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Re: return a complex object from a web service to silverlight client

nope, are you using WCF Service?

[OperationContract]
    List<string> getphoto(...parameter here);

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

hereitcomes
hereitcomes

Member

Member

5 points

9 Posts

Re: Re: Re: Re: return a complex object from a web service to silverlight client

 no im using an asp.net web service

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Answered Question

Re: Re: Re: Re: Re: return a complex object from a web service to silverlight client

Try to fetch it to XML

public string GetPhoto()
{
        XElement PhotoInfo= new XElement("PhotoInfo");
            PhotoInfo.Add(new XElement("PhotoData", base64Image));
            PhotoInfo.Add(new XElement("PhotoName", picName));
            PhotoInfo.Add(new XElement("anyother...", something here...));

        XDocument mydoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("Photo data"),
            PhotoInfo
            );
        return mydoc.ToString();
}

In Silverlight

if (e.Error == null)
            {
                string base64Image; string photoname;
                XDocument xd = XDocument.Parse(e.Result);
                var v = from g in xd.Descendants("PhotoInfo")
                        select new
                        {
                            bImage = g.Element("base64Image").Value,
                            pname = g.Element("PhotoName").Value,
                        };

                foreach (var itm in v)
                {
                    base64Image = itm.bImage.Trim();
                    photoname = itm.pname.Trim();
                }

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

hereitcomes
hereitcomes

Member

Member

5 points

9 Posts

Re: Re: Re: Re: Re: return a complex object from a web service to silverlight client

 thanks very much Steve. I created a Photo class in my silverlight app with 2 string properties. then wrote "select new Photo" in the XDocument selections.  Works now

Thank you!

venablc85
venablc85

Member

Member

18 points

12 Posts

Re: Re: Re: Re: Re: return a complex object from a web service to silverlight client

The current quota for isolated storage is 1mb per an application (http://www.silverlight.net/QuickStarts/IsoStore/default.aspx)

However i did read somewhere that there will be the functionality in a later version to increase this programatically although i suspect this will involve ugly pop-up requesting the users permission. Not paticularly user friendly :-s

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities