introduction
This article provides a detailed introduction to creating a simple WCF Service. It is very simple to create a WCF Service in C#. Before continuing to describe the procedure to create a WCF service, I would like to provide a small description of WCF Services.
WCF Service
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. Fore more details, follow the link: http://msdn.microsoft.com/en-us/library/ms731082.aspx .
Contracts are needed to create a WCF Service., There are a minimum of two contracts required to create a simple WCF Service.
Contracts in WCF
The contract is a platform-neutral and standard way of describing what the service does. There are four type of contracts that WCF supports, but you can use a minimum of two main contracts to start working in a WCF service, here I have explained only two contracts. The details of the main contract are the following:
Service Contracts
Data Contracts
1. Service Contract
This contract describes which operation the client can perform on the service. There are two types of Service Contracts available in the WCF Application.
2. Service Contract
This contract defines an interface for the client, in other words if you create an interface in the WCF Application and you are not using the Service Contract tag the interface then it will not be accessable to the client. It should be declared before the name of the interface.
For example:
[ServiceContract]
public interface IStudent
{
}
Operation Contract
The Operation Contract defines which function will be accessible to the client, mean if you don't declare the operation contract before declaring your function, the function or method will not be available to the client. Declare the tag within the interface before declaring the function to be accessible for the client.
For example:
[OperationContract]
Student Get(int ID);
[OperationContract]
string Set(Student SD);
Data Contracts
There are two in the WCF service that define which data types are passed to and from the service.
DataContract
This contract declares the simple CLR understandable (Entity) class in the WCF Service. This position of Data Contracting is before the class declaration.
For example:
[DataContract]
classStudent:IStudent
{
[DataMember]
publicint SudentID { get;set; }
[DataMember]
publicstring StudentName { get;set; }
}
DataMember
This attribute is used to define the fields of an entity (properties of class). The tag is declared before the property declaration.
For example:
[DataMember]
publicint SudentID { get;set; }
[DataMember]
publicstring StudentName { get;set; }
The details mentioned above are about the WCF Service and Contract needed to create a Service. Now let's go to the procedure to create a simple WCF Service application.
In this article I have described a simple WCF Service that stores and retrieves details of students, such as StudentName and StudentID. Let's discuss the procedure to create the same service I have created.
Procedure to Create a WCF Service
Open Visual Studio and create a new project. The New Project Template Window will open, select the WCF option from the left side tree view items. The template related to WCF will display in the right side, then select "WCF Service Library".
Note 1: WCF Service Supports Visual Studio 2008 and later and SQL Server 2005 and later.
Note: It is a good practice to define the service in the Class library and refer to the class library in the Host project. Use a service class in the host project.
Rename the service application to "MyServiceLibrary" and set the location, solution and solution name if necessary.
Remove the Iservice, Service1 and Service classes from your project.
Add an interface to your project having the name Istudent, define the Service Contract above the interface name and OperationContract or OperationContracts above the method or methods in your Interface to make it accessible to the client.
Here I have declared two methods named Get and Set to retrieve and save student details.
For example:
namespace MyService
{
[ServiceContract]
interfaceIStudent
{
[OperationContract]
Student Get(int ID);
[OperationContract]
string Set(Student SD);
}
}
Add an Entity (SimpleCLR Understandable class) and rename it to Student; on the above classname declare the DataContract tag.
Declare the properties necessary; here I have declared two properties (StudentID and StudentName) and the above on every property declare the DataMember tag.
For example:
[DataContract]
classStudent
{
[DataMember]
publicint SudentID { get;set; }
[DataMember]
publicstring StudentName { get;set; }
}
Add another class to implement the interface function / methods, in the class you can add code into the method body that will be available in the client. Rename the class name to Student or whatever depending on your needs. Implement the interface and their functions here.
Only the function having the declaration Operation Contract tag above it will be accessible to the client. Or those functions that are not declared with the OperationContract tag will not be accessible to the client.
Than after completing above 9 steps you need to create an EndPoint. WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world. Three components Address, Binding and Contract belong to an End Point.
The address specifies where the WCF Service is hosted and the Binding will describe how the client will communicate with the service. A Contract is a collection of operations that specify what the end point will communicate with in the outside world
Open the Solution Explorer to configure the end point. Right-click on the app.config file then select "EditWCF Config".
The Config window will open, on the left side expand the service folder then again expand Service1.
In the service1 you will find the endpoint folder, expand that again; you will find two more end points by default. Right-click on the endpoints, click on "New ServiceEndPoint".
On the right pan you will get a field of the new service endpoint. Set the name of the endpoint; I have given it "Student". Specify the address, the details of address I have already discussed . Select the binding as basic HTTP Bind.
Click on the Contract; the contract browser will open, find your service library from the bin folder of your application. Open the library DLL; there you will find an interface or interfaces. Select the one whose name is in the name field of EndPoint. Then click on the "Open" button.
Select the client folder and follow the procedure to create a client Endpoint.
Up to this step you have created a WCF Library, now we will create a WCF Service Application.
Right-click on the project solution then select "Add" -> "New project". In the project template windows select WCF Service application.
Rename it to MyServiceApplication and remove the interface Iservice.cs. Rename the Service.svc file to MyStudentService.
Expand "mystudentService"; you will get the Service1.cs file there as a child, remove "service1" because we are using a library.
Add the reference of MyServicelibrary into your MyServiceApplication.
Double-click on "MystudentService"; it will open, then remove the "CodeBinding" tag from "mystudentService". Modiry the Service tag to "MyServicelibrary" Service then close and save.
Again right-click on the web.config file and select "Edit WCF config file". Right-click on the services and select "New Service" in the right side pan. Type the name of your service. Here I have given "StudentService".
Do the same thing that your have done before creating EndPoint.
Now your WCF Service is completed; you can publish it and use it in a difference application.
Summary
Here we have discussed how to create a WCF service and a little description of WCF services.