简体   繁体   中英

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?

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM