0

I'm having trouble getting my WCF service to work. In the code below I can call my service's HelloWorld() method and it works, but I cannot call the GetCustomer() method successfully (it just prints "Address:", not the customer name and address. Anyone know how this could be? I do not understand why my service is not passing a customer object to the client. I believe I am implementing what was shown here.If anyone can take a look at this I would really appreciate it. Thanks in advance!

// Service Interface:
[ServiceContract(Namespace="http://www.myservice.com")]
public interface IAddressService
{
  [OperationContract]
  string HelloWorld();

  [OperationContract]
  Customer GetCustomer();
}

// Service:
public class AddressService : IAddressService
{
  public string HelloWorld()
  {
    return "Hello World";
  }

  public Customer GetCustomer()
  {
    return new Customer() 
    { 
      Id = 1, 
      Address = "1212 Forest Lane Irving TX", 
      FirstName = "Vladamir"
    };
  }
}


// Customer class (my custom data object):
[DataContract] 
public class Customer
{
  private int id;
  private string address;
  private string firstName;

  [DataMember]
  public int Id
  {
    get { return id; }
    set { id = value; }
  }

  [DataMember] 
  public string Address
  {
    get { return address; }
    set { address = value; }
  }

  [DataMember]
  public string FirstName
  {
    get { return firstName; }
    set { firstName = value; }
  }
}

//Client code:
class Program
{
  static void Main(string[] args)
  {
    EndpointAddress epa = new EndpointAddress("http://localhost:8000/AddressService/AddressAppService");
    IAddressService service = ChannelFactory<IAddressService>.CreateChannel(new BasicHttpBinding(), epa);

    Console.WriteLine(service.HelloWorld());
    Customer c1 = service.GetCustomer();
    Console.Write(c1.FirstName + " Addresss: " + c1.Address);

    Console.WriteLine("Press <ENTER> to exit the client.");
    Console.ReadLine();
  }
}
Community
  • 1
  • 1
John Fischer
  • 1,115
  • 3
  • 13
  • 24

2 Answers2

2

I would update the service reference. Typically if you've partial designed your class, e.g. you originally had:

[DataContract]
public class Customer
{
    [DataMember]
    public string Address { get; set; }
}

And added a service reference while the type was in this state, but then added some more properties:

[DataContract]
public class Customer
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Address { get; set; }
}

...if you don't update the service reference (from the client code), you're data contract will only describe the shape of the Customer type when the reference was first added.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
0

I found out what the problem was. I fixed this by adding the namespace to my DataContract. That means I changed

[DataContract]

to

[DataContract(Namespace="www.myservice.com")]

and I was able to download my custom object.

John Fischer
  • 1,115
  • 3
  • 13
  • 24