1

What's my Problem

Object returned from the ASMX service is used in Silverlight application. Class has methods but the result from the ASMX WebMethod does not show methods on the object.

Tell me more

here is my class

public class Dog
{
      public string Name{get;set;}    
      public void Bark();
}

here is the WebMethod

[WebMethod]
public List<Dog> Findlabrador()
{
    blah blah blah
    return list_of_labrador;
}

the silverlight code

void LabradorFetchCompleted(object sender, LabradorFetchCompletedEventArgs e)
{
  var list_of_labrador = e.Result;
  foreach(var labradorDog in list_of_labrador)
  {
      labradorDog.Bark();
      //** WTH my labrador can't BARK** Bark method is not shown in intellisense there is compilation error if i explicitly specify 
  }
}

I am a programmer not a layman

Ok hmm, let me put in your words. Here are steps for you to reproduce the issue

  • Create a Silverlight Application project ( Let VS create Website to host the application)

  • Create a Silverlight Class library create the Dog class inside it

  • Compile the Silverlight Class library to assembly(Dog.dll)

  • Add reference to Dog.dll silverlight assembly to the silverlight application project

  • Add a WebService application to the project ( DogService.asmx note the asmx extension)

  • Add a reference to the Silverlight Dog.dll assembly for the DogService

  • return hardcoded List<Dog> class from a WebMethod inside it

  • Add a reference from the Service to Silverlight application, create a instance of proxy client and invoke the method

  • Watch as your Dog too can't Bark :(

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

2 Answers2

5

Methods are never serialized. Only data. Your methods, events, indexers, constructors, etc, will never be serialized.

You should not be using ASMX services anyway. Use WCF instead. WCF, among other things, gives you the ability to share datatypes between the client and service. This would allow something like "serializing methods": the same methods could be used both on the client and server.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • that's why i cast the ouput object explicitly to Dog like `var labradors = e.Result as List;` and surprisingly labradors is `null` . How come this happens even though instance of class is sent from the service – Deeptechtons May 10 '12 at 05:17
  • 1
    An instance is _not_ sent: see [Basics: How Web Services Work](http://johnwsaunders3.wordpress.com/2008/09/23/basics-how-web-services-work/) – John Saunders May 10 '12 at 05:20
  • +1 for the link. Post was made in 2008 and holds good event 2011 :) – Deeptechtons May 10 '12 at 05:21
  • Could you explain why the List sent from service cannot be converted to List inside silverlight app. I get the error `ObservableCollection` cannot be converted to `ObservableCollection` Does this mean the service reference generated a Dog class for the service during process of adding reference ? – Deeptechtons May 10 '12 at 06:00
  • That is correct. It's a totally different and unrelated class. – John Saunders May 10 '12 at 06:09
  • that means i will have to manually create instance for silverlight app and map the properties from the service to instance. ooh god :) – Deeptechtons May 10 '12 at 06:14
3

You are supposed to define all common classes using portable class libraries, http://msdn.microsoft.com/en-us/library/gg597391.aspx

And then when consuming the web service within Silverlight, you should ask the proxy generator to reuse those classes. That makes sure you get all the functions.

Web service definition (WSDL) only takes care of fields/properties. Methods are not transferred over the wire.

Lex Li
  • 60,503
  • 9
  • 116
  • 147