This error had me baffled today, and I could find little reference to it from Google or Live Search.

It was the result of creating a simple WCF Service with two methods as follows and then trying to add a reference to it from Silverlight 2:

[ServiceContract]
public interface IWcfService
{
    [OperationContract]
    string HelloWorld(string name);
    [OperationContract]
    List<Person> GetPeople();
}

 

public class WcfService : IWcfService
{
    public string HelloWorld(string name)
    {
        return "Hello World " + name;
    }


    #region IWcfService Members


    public List<Person> GetPeople()
    {
        List<Person> people = new List<Person>();
        people.Add(new Person() { FirstName = "Fred", LastName = "Bloggs", Age = 23 });
        people.Add(new Person() { FirstName = "Fred", LastName = "Smith", Age = 24 });
        people.Add(new Person() { FirstName = "John", LastName = "Bloggs", Age = 29 });
        return people;
    }

    #endregion
}
 
The service config looked good (having changed to a basicHttpBinding - which is the what Silverlight 2 supports:
 
<system.serviceModel>
  <behaviors>
   <serviceBehaviors>
    <behavior name="WcfServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    
   </serviceBehaviors>
  </behaviors>
  <services>
   <service behaviorConfiguration="WcfServiceBehavior" name="WcfService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
     contract="IWcfService">
     <identity>
      <dns value="localhost" />
     </identity>
    </endpoint>
   </service>
   
  </services>
 </system.serviceModel>

 

But I kept getting this error.  And the other 6 delegates on the course I was delivering were getting exactly the same problem!  I kept thinking that this was something to do with Silverlight, but I was barking up the wrong tree.  It wasn't until I got on the train home, that it suddenly hit me, I had not marked the Person class with the DataContract and DataMember attributes!  This was what was needed to sort it out:

[DataContract]
public class Person
{
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public int Age { get; set; }
}

 

Still, in my defence, I think the cause of the error message wasn't that obvious.

Hope this helps someone!

Cheers

Ian

Technorati Tags: ,