1
votes

I am newbie to WCF. Hence, this question. I am in the process of converting an asmx web service to WCF service.

I am having trouble defining data contracts.

I have two operations on my service contract, one returns an array of Customer, where Customer is my custom type with some fields.

Here, my question is which needs to declared as data contract.

Is it the Customer class or Customers class which contains an array of Customer or is it both?

Please do suggest

2
I would define Customer class, and then return a collection of that class or a single instance, depending on what the operation returns. - Tim
The operation contract returns array of customers which mean Customer and Customers both of them goes outside of the service.So, i do think both of them should be defined as Data Contracts..Is that what are you trying to say ? if i am not wrong - Sai Avinash

2 Answers

3
votes

You should read this MSDN article on DataContracts: Using Data Contracts.

In short:

You should define the DataContract attribute on every class you use in your service that goes across the line, so you would need the attribute on both Customer and Customers.

1
votes

Please check this sample code. In following class file i had create data contract for my wcf service. use namespace System.Runtime.Serialization for serialize the data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace BusinessLogic
{
     [DataContract]
     [Serializable]
     public class POSTheaterListArgs
    {
    public POSTheaterListArgs()
    {
        TheaterDates = new List<string>();
    }
    [DataMember]
    public List<string> TheaterDates { get; set; }
    [DataMember]
    public int TheaterId { get; set; }
    [DataMember]
    public int NumofScreen { get; set; }
    [DataMember]
    public int? CompanyId { get; set; }
    [DataMember]
    public int ChainId { get; set; }
    [DataMember]


    private int _Number;
    public int Number { get { return _Number; } set { _Number = value; } }

    private bool _status;
    public bool status { get { return _status; } set { _status = value; } }

}

For array data

    public POSTheaterListArgs()
    {
        TheaterDates = new List<string>();
    }
    [DataMember]
    public List<string> TheaterDates { get; set; }

will return you list array structure.

This may help you....