Showing posts with label WCF Service. Show all posts
Showing posts with label WCF Service. Show all posts

Friday, 5 August 2016

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.

Introduction To WCF | Basics of WCF


What is WCF?

WCF is a service Oriented programming in .net framework 3.0 which provide unique platform for all type of Communications in .net for distributed Applications.

It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF.


Basic Bare Bones of WCF.Endpoints (ABC terms).
  • Address
  • Binding
  • Contract
All the WCF communications are take place through end point. End point consists of three components.(i.e ABC)


AddressBasically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service.
         e.g http://localhost:3446/Service1.svc.

BindingBinding will describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

ContractCollection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. Each operation is a simple exchange pattern such as one-way, duplex and request/reply
Service Contract: Service contracts describe the operation that service can provide.

Operation Contract: Attribute Which is used to define the Method as part of Service.
Message Contract: Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
Policies and Binding: Specify conditions required to communicate with a service e.g security requirement to communicate with service, protocol and encoding used for binding.

Service Runtime: It contains the behaviors that occur during runtime of service.
Throttling Behavior- Controls how many messages are processed.
Error Behavior - Specifies what occurs, when internal error occurs on the service.
Metadata Behavior - Tells how and whether metadata is available to outside world.
Instance Behavior - Specifies how many instance of the service has to be created while running.
Transaction Behavior - Enables the rollback of transacted operations if a failure occurs.
Dispatch Behavior - Controls how a message is processed by the WCF Infrastructure.

Messaging
Messaging layer is composed of channels. A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a channel stack. Channels are the core abstraction for sending message to and receiving message from an Endpoint. Broadly we can categories channels as

Transport Channels: Handles sending and receiving message from network. Protocols like HTTP, TCP, name pipes and MSMQ.
Protocol Channels: Implements SOAP based protocol by processing and possibly modifying message. E.g. WS-Security and WS-Reliability.

Activation and Hosting
Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF service can be hosted by following mechanism

IIS: Internet information Service provides number of advantages if a Service uses Http as protocol. It does not require Host code to activate the service, it automatically activates service code.

Windows Activation Service: (WAS) is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.

Self-Hosting: WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.

Windows Service: WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).