user1386919
user1386919

Reputation:

Datacontract in wcf

How to initialize class in WCF?

Suppose I just want to make a sum of two member in a class.

I have coded in iService like:

[DataContract]
class Sample
{
    public int i { get; set; }
    public int q { get; set; }
}

[OperationContract]
public int Sum(Sample obj);

And in service:

public int Sum(Sample obj)
{
}

What added coding require to make that run, as I am confuse with the class declaration in both the pages?

Upvotes: 0

Views: 1838

Answers (1)

Davin Tryon
Davin Tryon

Reputation: 67326

Usually, it would be set up like below. You should clearly separate the service from the data contracts.

[ServiceContract]
public interface ISampleService
{
        [OperationContract]
        int sum(SampleData obj);
}

public class SampleService : ISampleService
{
        public int sum(SampleData obj)
        {
           // logic here
        }
}

[DataContract]
public class SampleData
{
       [DataMember]
       public int i { get; set; }

       [DataMember]
       public int q { get; set; }
}

Upvotes: 4

Related Questions