There are two in the WCF service that define which data types are passed to and from the service.
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.
-
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]
- interface IStudent
- {
- [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]
- public int SudentID { get;set; }
- [DataMember]
- public string 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.
-
Then 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". Modify 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 you have done before creating EndPoint.
Now your WCF Service is completed; you can publish it and use it in a different application.
Here we have discussed how to create a WCF service and a little description of WCF services.