views:

134

answers:

2

Hi all,

I have the following web service:

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyTestClass
    {
        [OperationContract]
     public List<TrackInfo> GetAll(string songName)
        {
      return new List<tracksInfo>;
        }
}

I've made simple by purpose. Anyhows, when TrackInfo is declared in a class outside of the service reference, the service on the other end (meaning in the silverlight area) recognizes only the TrackInfo class, but cannot find the GetAll method. When I try putting the trackinfo class inside the reference but outside of MyTestClass the same problem occurs

I tried putting the trackinfo class inside the serivce as a datacontract:

[DataContract]
 public class TrackInfo
 {
  public int ID { get; set; }
  //Should consider using a stream instead
  public List<Image> FullSizeNotes { get; set; }
  public Image TrackNotes { get; set; }
  public Stream MidiFile { get; set; }
 }

Now the service recognizes the GetAll function, but does not recognize the properties of trackinfo.

+1  A: 

Data contracts are an "opt in" technique, unlike with the XML Serializer in ASMX services. That one serialized all public fields and public read/write properties unless you told it otherwise.

Data Contracts need to have the properties you want serialized decorated with the [DataMember] attribute.

[DataContract]
public class TrackInfo
{
        [DataMember]
        public int ID { get; set; }
        //Should consider using a stream instead
        [DataMember]
        public List<Image> FullSizeNotes { get; set; }
        [DataMember]
        public Image TrackNotes { get; set; }
        [DataMember]
        public Stream MidiFile { get; set; }
}
John Saunders
A: 

Another approach, which works with .NET 3.5 SP1, is to externalize your entity so you don't have to make them up. Be sure to read the comments for the full picture:

http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2009/07/13/Sharing-Entities-between-WCF-and-Silverlight.aspx

Basically you share the class file code between the client and server, and when setting the service reference, tell VS to reuse types that already exist on the client.

Pete

Pete