Create a WCF Service library project
1. Open Visual Studio, and Select File > New > Project
2. Select WCF Service Library template under Visual C# > WCF templates
4. Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.
Create the Data Contract
7. Open StudentService.cs file that you created.
8. Add using System.ServiceModel;
9. Add using System.Runtime.Serialization;
10. Add the following code in the StudentService.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace StudentServiceLibrary
{
[DataContract]
public class StudentInfo
{
[DataMember]
public string StuId { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Email { get; set; }
}
class StudentService
{
}
}
Create the Service Contract
11. Add the following code in the ProductService.cs class
[ServiceContract]
public interface IStudentService
{
[OperationContract]
void AddStudent(Student stu);
[OperationContract]
IEnumerable<Student> ListAllStudents();
[OperationContract]
Student ListStudentById(string code);
}
Service Implementation
12. Add the following code in the ProductService.cs class
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class StudentService : IStudentService
{
Dictionary<string, Student> Students = new Dictionary<string, Student>();
void IStudentService.AddStudent(Student stu)
{
Students.Add(stu.StuId, stu);
}
IEnumerable<Student> IStudentService.ListAllStudents()
{
return Students.Values;
}
Student IStudentService.ListStudentById(string stuId)
{
return Students[stuId];
}
}
Service endpoints
13. Updated the name of the service in App.config as given below:
<service name="StudentServiceLibrary.StudentService">
<endpoint address="" binding="basicHttpBinding" contract="StudentServiceLibrary.IStudentService">
Test Service
15. Run the Service.