Following is the sample application for creating a WCF Service.
- Create new WCF Service application from add new templates in visual studio
- Template will create default file structure to the solution. Delete the .svc file and IService file. So that we will create the application from scratch.
- For manipulating with data in our service application, we added a database to our application and creating a sample table.
- We are using Linq to SQL as the context for fetching the data.
- Add Linq to SQL classes to the solution.
- Drag and drop the table to dbml designer.
- Add WCF Service to the service application.
- Create an interface with the following method declartion.
- Service contract and operation contract attribute come on importing System.Runtime.Serialization namespace.
- After creating IMySerivce, we will implement this interface into our service.
- Inherit the interface on to MyService class and implement the interface in to the class.
- After implementing the interface to the class, we need to extend the method to return list of person data. Default structure forms as below,
- Create the object for SamplePersonModelContext to access the person data .
- Once you run the service. WCF Client will be displayed which include the IMyService interface and GetPersonData method.
- Click on the GetPersonData method, GetPersonData method Request and Response will be shown. Click on invoke to send request to the service for fetching the person data.
Consuming the WCF Service to the web application:
- Create a sample web application and right click on the project and choose Add Service Reference to add the service.
- Specify the end point of the service and click on Go.
- In the services section, MyService will be displayed which is the service we created.
- In the operations section, GetPersonData will be displayed.
- Specify the meaningful namespace for the service reference and click on ok.
- Service reference will be added to the application.
- Create an object for the service client to access the operations of the service.
- When you run the application, request will be invoked to the service and response will be binded to the grid control in the form.
Sample WCF Service Sample Web Application