A Web service is a unit of managed code that can be remotely invoked using HTTP, that is, it can be activated using HTTP requests. So, web services allows you to expose the functionality of your existing code over the network. Once it is exposed on the network, other application can use the functionality of your program. Web services is used to make the application platform and technology independent. Web services uses standardized industry standard protocol for the communication.
If we expose our functionality as web service, this service can not only used by web client, but also can be consumed by desktop applications, news feeds and android applications also. So with all these advantages let us see the steps to convert Asp.Net application to application consuming web service by using TrendsInInvestment as an example
- Analyze the .Net application code and make a note of its logic and the data its accessing(may be from database).
On analyzing TrendsInInvestment web application we could find that the application fetches equity and derivative values from the database and plots the values as a graph on the web page. The web application presents graph either for 1 month or 3 month or 5 month duration for both equity and derivative values.
- Categorize the logic of the code into server side logic and client side logic
On further analysis, we can conclude that processing of fetched values and plotting of those values can be a client side logic and fetching of data from external database for the specific duration of time can be a server side logic.
- Categorize the server side logic into different functions
On analysing server side logic, the web application fetches equity plot values, derivative plot values and holidays for the specified duration of time. So we can implement these three functions in three different methods.
- Choose the .Net framework version required to implement
Data tables fetched from data tables could not be sent as web service response, since SQL data tables are not serializable. we need to represent those table values into serializable format. we can use methods like XML, binary XML or CSV formats. For better representation we will consider XML format. And in order to avail all the latest features of .Net library. we will be considering .Net framework 4.0.
- Accordingly open a new ASP.Net web service template project from visual studio
On the default web service template you will find the following default elements
On Service.cs file you will find default HelloWorld() web method.
- Add web methods to implement all the server side logical functionalities
As discussed earlier, we have three functions to be implemented as web methods. Here am adding GetEquityPlotValues method, GetDerivativePlotValues method and GetHolidays method and copied the corresponding functionalities into web methods which accepts start date and end date and provides the requested data between the specified dates by fetching from the database. we need to take care of validating input dates and exception handling for database errors in each of these methods. Here in each method the database values will be represented in XML format using XElement. And finally each method returns the XML of database output.
- If you are fetching data from external database, add a connection string in your web.config file
Here TrendsInInvestment web application is accessing external MS Sql database, So in order to access database we need Sql connection string. the better practice is to add connection string in web.config file. so if database location changes we can easily adapt by changing connection string in web.config file.
- Additional functionalities for your web service(Optional)
If in case your web service has to be consumed by authenticated users, create a table in database for authentication details like username, password, authToken, lastSessionTimestamp. And add a web method for authentication of users by that accepts username and password and validates the same values from the database.Authentication has been implemented in TrendsInInvestment web service.
- Caching of response
If you want to save the server processor cycles and Database access cycles, we can cache the most frequently requested data in processor memory and serve the cached data on further requests. Caching can be done by using HttpContext.Current.Cache class with key and object value pairs.
- Pagination of response
If you want to be extra cautious about consumers launching denial of service attacks. You can paginate the responses for long duration requests by restricting response to few months and informing the user about the pagination and identifier for next page of results. This can be done by appending an extra XML node for pagination identifier with the restricted or paginated data.
- Build and launch the web service in visual studio and you will be provided with test browser functionality as follows.
This Video shows the brief demo of original web application and modified web application that consumes web service