When you create a WCF application in Visual Studio. The project creates 2 classes for you. It also link it to the WCF Test Client.
Interface and Data Contract class (.cs)
Both Interface and Data Contact. The Data Contact can be its own separate class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetSomeData();
[OperationContract]
string GetDocumentName(MyDocument pDocument);
[OperationContract]
MyDocument GetDocument(MyDocument pDocument);
}
[DataContract]
public class MyDocument
{
[DataMember]
public string DocumentName { get; set; }
[DataMember]
public int NumberOfPage { get; set; }
}
}
Service Class (.svc)
Implements IService1 Interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
public class Service1 : IService1
{
public string GetSomeData()
{
return string.Format("You got some data.");
}
public string GetDocumentName(MyDocument pDocument)
{
return "The name is: " + pDocument.DocumentName;
}
public MyDocument GetDocument(MyDocument pDocument)
{
pDocument.DocumentName = "The name has been changed.";
pDocument.NumberOfPage = 12;
return pDocument;
}
}
}
WCF Test Client
Comments