This blog is intended to explain how to create a simple WCF Restful application using VB.Net
What is WCF?
Windows Communication Foundation (WCF) is a framework for building service oriented application.
In this example, we are going to create a simple application which is used for adding two numbers where parameters are passed on URL as query string and return values in XML format.
Steps to implement
1. Creating a WCF Service Library
WCF Service Library creates a default service interface and class to implement it. As we are not going to use it we can go ahead and delete the default service and class.
WCF Service Library –host-independent WCF service class library (.dll)
Open a new instance of Visual Studio 2013 and Select File -> New -> Project and then from New project Select WCF -> WCF Service Library, then Name it
Let us create a service interface named IWCFRestful and a class WCFRestful.
In Solution Explorer, we need to Right click on the project – > Add -> New Item and choose WCF Service, then name it.
2. Defining service interface and implementing into class
Then create a Service Interface that define a method for a collection of service and the interface/class with Operation Contract. The Interface IWCFRestful has a method “WebGet” attribute parameters.
Method – Get represents a “HTTP GET Request”
Request Format – For Xml format, we are using WebMessageFormat.Xml
Response Format – For Xml format in response, we use WebMessageFormat.Xml
BodyStyle – It can be neither request nor response are Wrapped.
Wrapped, WrappedResponse, WrappedRequest are the other possible values for BodyStyle.
UriTemplate – For service operation it Gets and Sets the URI Template.
URI – Uniform Resource Identifier
Once the interface is defined with method, then we can implement a service contract through class.
Then we can add the code to the method by implementing the service interface in it.
3. Creating a WCF Service application
WCF Service application create a service class with default class and codebehind in it. As we are not going to use it we can go ahead and delete the default service class and then we create a new Ajax-Enabled WCF service.
WCF Service application –hosted in IIS/WAS (Windows Activation Service)
In Solution Explorer, Right click on solution -> Add -> New Project -> choose WCF Service Application, then name it.
Ajax-Enabled service is a service that can be called from Ajax client side script. We can call the WCF Web service directly instead of making a call in code behind file.
In Solution Explorer, under project Right click- >Add -> New Item and choose WCF Service (Ajax-Enabled), then name it.
4. Result in XML
Now, it is ready to launch the application in the browser.