Showing posts with label Develop Services. Show all posts
Showing posts with label Develop Services. Show all posts

Friday, 5 August 2016

Restful WebApi Routing | Customizing WebApi Routes

In this article, I would show conventional and attribute based routing. Open WebApiConfig.cs file add new http route as shown below, just to avoid conflict between each route, removed api in the 2nd route table. Framework hierarchically traverse through route table till it finds the match. We should make sure to arrange the route properly to avoid conflicts.
Below screen shot shows the routing of api using ProductAPI and DefaultAPI. Both render same result its just the urls are different.
Lets do a PUT operation using ProductAPI routing table.
Coming back to product controller, lets define the HTTP Verb and ActionName as shown below
Now WebApi operations can be performed using the actionname instead of method names.
Configuring Attribute based routing. I added Route attribute to each method as shown below.
Now, WebApi operations are performed using the route name.

Restful API | Enabling CRUD Operations


This is continuation of previous article, here we will enable POST, PUT and DELETE operations for the webapi. Lets first add support for POST operation, in service layer add new interface method AddProduct and the implementation is shown below. Using EF, we add new entry to the model and render the data requested back to client.
In webapi, lets add POST method which takes Product Model as input parameter, this invokes service layer AddProduct method.
Lets run and test the functionality. In Postman, lets do a post request with a product data and the information is saved in DB.
I made changes in product table to have primary key as ProductID and enabled auto increment for this column. In order update the model, open edmx file right choose update model from database option and in refresh table choose the update and click finish as shown below.
Let try to place once more POST request with invalid ProductID, exception is thrown and http 400 Bad Request status is got back with the exception details.
Here is the corresponding changes in interface and implementation to add PUT and DELETE operation.

Let add functionalities to support PUT and DELETE operation in WebApi.
Run and test the project using Postman for PUT and DELETE feature. 

PUT Operation, updated the price and quantity information but this updated the completed record, say all the columns. Best way to do update using EF is to get the data from DB and use it as part of update.
DELETE operation, removed the product 3 from DB.

Restful API with Entity Framework | 3-Tier WebApi

Let create new WebApi using 3-Tier Architecture. Open  VS 2015 >New Projects > Visual C#  >  Web> ASP.Net Web Application and add empty webapi projects for service and then data layers as shown below.
Add EntityFramwork reference using nuget manager in Datalayer. We would build a model from Database (Database First approach)
In datalayer, lets add a new model; right click > add new item > Data > Choose ADO.NET Entity Data Model and provide model name and click add.
Choose EF Designer from DB and provide the connection string and then choose the product table and click finish.
Product model is shown below,
In Service layer, lets add a service interface and  implementation for it, let build webapi for GET Operations
Include reference of datalayer. Here is the service implementation for Get and GetById methods, these methods read the data from the model using EF.
In WebApi project, add a product controller and then include references of service and datalayer. Lets Implement GET operations as shown below,
Run the Webapi project and browse urls as shown below, model is rendered from the product table as shown.
Lets add a breakpoint and hit api/product url.

Similarly, add a breakpoint and hit api/product/2 url.
Let refactor the code for the Get Operation with Id,
Browse for product 3, empty product is rendered

Restful WebApi Getting Started

In this walk through, I would build a webapi for render model data on GET operations.
Open  VS 2015 >New Projects > Visual C#  >  Web> ASP.Net Web Application.
Choose Empty project as shown and enable MVC and WebAPI
In Models folders add class product.cs and define the model as shown
Then in the controllers folder add a new controller and choose Empty WebAPI 2 Controller
Open product controller class and this inherits Api Controller, include reference of model and create object contains product model data. Lets implement HTTP GET verb as shown below, one return complete model and other return specify model based on the model requested for using the Id parameter.
Navigate to WebApiConfig.cs and see the routing configuration.
Lets run the project and you see that webapi isn't available. but its running now navigate to api/product and this renders list of products available and again navigate to the api/product/1 this returns the product model by Id
Using POST-Man, we can test our api as shown below. 

Developing WCF Service | Booking Service


In this walkthrough, I would build a WCF Service to support Banking Transaction in ATM. Create a new project, Open  VS 2012 >New Projects > Visual C#  >  WCF  > WCF Service Application.
Lets implement Booking service, below is the interface for booking service having single operation BookService. DataContracts are Booking Request and Booking Response as shown below.
Booking Service Implementation is shown below, its uses Hashtable to store transaction data. Validation is performed on the Booking Request and either on success or failure, booking response is constructed and echoed back to the client. Note, successful transactions are added to hashtable
Lets run the service and test it. Below is the service url and wsdl
Using test client, lets test the service. Service request should have unique contact number others, service responds stating already service request exists. There is max cap size of 10 set in  the hashtable. Beyond 10 unique request, service would respond back saying service is unavailable.
Considerations while develope WCF services for BizTalk interfaces:>>

BizTalk generates schema based on below details, 
Request DataContract :BookingRequest
Response DataContract: BookingRespone
Service Contract Method: BookService(Param Reqtype)
Service Contract Response schema gets auto-generated with a suffix "RESPONSE" to the Service Contract method "BookService", , as refer below.

Service Contract Method Response schema: BookService+Response >> BookServiceResponse

We should specify unique names Request, Response Data Contract and Service Contract Method, This would avoid duplicate schema for service contract response.

Developing WCF Services | WCF Service for Banking Transaction


In this walkthrough, I would build a WCF Service to support Banking Transaction in ATM. Create a new project, Open  VS 2012 >New Projects > Visual C#  >  WCF  > WCF Service Application.
Visual studio provides a mock service implementation as shown below,lets run and test the services. Select the service1.svc file and then run the project this launches the test client.
In the WCF-Test Client, you can see the list of method/operation all available in the service. Double click the GetData operation and right pane, provide the request data and click invoke to trigger the service. Service Response is shown in the bottom half of right pane. Similarly test the other operation.
Lets implement Banking service, below is the interface for banking service having two operations ATMTransaction and ValidateCustomer. DataContracts are Transaction Request and Response as shown below.
Here is the service implementation layer, for IBankingService. Service Layer call internal method to perform required operation.
Here is business Logic implementation for ATM Transaction Operation which uses internal Transaction and InterATMTransaction methods. These methods connects to DB using ADO.net as shown
Similarly, ValidateCustomer Operation is shown below
WCF Serive implementation is completed, lets run and host the service locally in IIS. WCF service connects to below database, CustomerAccount and ATMTransactions tables are shown below. 
Here is the service wsdl of BankingService. Having ATMTransaction and ValidateCustomer Operations
Lets test the service from WCF-Test Client as shown below and the transactions are stored in the SQL tables.