WCF Tutorial, Part 2: Create a Simple Service and Client
The following steps will be creating a simple service which is used to reserve a room.
Task 1: Define Service and Data Contract
Create the DataContract
-
Open Visual Studio
-
Create an Empty Solution called RoomReservation
-
Add a Class Library project to the solution and name it RoomReservationContracts
-
Create a class called RoomReservation
This class will be used to represent data from which retrieves from database and will be using across the network.
RoomResercation.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Runtime.CompilerServices; using System.Runtime.Serialization; namespace RoomReservationContracts { [DataContract] public class RoomReservation : INotifyPropertyChanged { private int id; [DataMember] public int ID { get { return id; } set { SetProperty(ref id, value); } } private string roomName; [DataMember] [StringLength(50)] public string RoomName { get { return roomName; } set { SetProperty(ref roomName, value); } } private DateTime startTime; [DataMember] public DateTime StartTime { get { return startTime; } set { SetProperty(ref startTime, value); } } private DateTime endTime; [DataMember] public DateTime EndTime { get { return endTime; } set { SetProperty(ref endTime, value); } } private string contact; [DataMember] [StringLength(30)] public string Contact { get { return contact; } set { SetProperty(ref contact, value); } } private string text; [DataMember] [StringLength(50)] public string Text { get { return text; } set { SetProperty(ref text, value); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnNotifyPropertyChanged(string propertyName) { PropertyChangedEventHandler eventHandler = PropertyChanged; if (eventHandler != null) eventHandler(this, new PropertyChangedEventArgs(propertyName)); } protected virtual void SetProperty<T>(ref T item, T value, [CallerMemberName] string propertyName = null) { if (!EqualityComparer<T>.Default.Equals(item, value)) { item = value; OnNotifyPropertyChanged(propertyName); } } } }
-
INotifyPropertyChanged
Interface is used to notify clients, typically binding clients, that a property value has changed. -
DataContract
andDataMember
attributes used to determine that the class is used to sending data across WCF service. -
A
DataContract
is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged. -
StringLengthAttribute
Class specifies the minimum and maximum length of characters that are allowed in a data field. It is used for both validating user input as well as define column schemas on creating the database table.
Create the Service Contract
-
Add a new class to the project called IRoomService
IroomService.cs
using System; using System.ServiceModel; namespace RoomReservationContracts { [ServiceContract] public interface IRoomService { [OperationContract] bool ReserveRoom(RoomReservation roomReservation); [OperationContract] RoomReservation[] GetRoomReservations(DateTime fromTime, DateTime toTime); } }
-
Operations offered by the service can be defined by an interface.
-
The service contract is defined with the attribute ServiceContract.
-
The operations defined by the service have the attribute OperationContract.