Implementing a WCF Service in the Real World
In the previous chapter, we created a basic WCF service. The WCF service we
created, HelloWorldService, has only one method, called GetMessage. Because
this is just an example, we implemented this WCF service in one layer only.
Both the service interface and implementation are all within one deployable
component.
In this chapter and the next one, we will implement a WCF Service, which will be
called RealNorthwindService, to refl ect a real world solution. In this chapter
we will separate the service interface layer from the business logic layer, and
in the next chapter we will add a data access layer to the service.
In this chapter, we will create and test the WCF service by following these
steps:
-
Create the project using a WCF Service Library template
-
Create the project using a WCF Service Application template
-
Create the Service Operation Contracts
-
Create the Data Contracts
-
Add a Product Entity project
-
Add a business logic layer project
-
Call the business logic layer from the service interface layer
-
Test the service
Why layering a service?
An important aspect of SOA design is that service boundaries should be explicit,
which means hiding all the details of the implementation behind the service
boundary. This includes revealing or dictating what particular technology was
used.
Further more, inside the implementation of a service, the code responsible for
the data manipulation should be separated from the code responsible for the
business logic. So in the real world it is always a good practice to implement
a WCF service in three or more layers. The three layers are the service
interface layer, the business logic layer, and the data access layer.
-
Service interface layer: This layer will include the service contracts and
operation contracts that are used to defi ne the service interfaces that will
be exposed at the service boundary. Data contracts are also defi ned to pass in
to and out of the service. If any exception is expected to be thrown outside of
the service, then Fault contracts will also be defi ned at this layer.
-
Business logic layer: This layer will apply the actual business logic to the
service operations. It will check the preconditions of each operation, perform
business activities, and return any necessary results to the caller of the
service.
-
Data access layer: This layer will take care of all of the tasks needed to
access the underlying databases. It will use a specifi c data adapter to query
and update the databases. This layer will handle connections to databases,
transaction processing, and concurrency controlling. Neither the service
interface layer nor the business logic layer needs to worry about these things.
Layering provides separation of concerns and better factoring of code, which
gives you better maintainability and the ability to split layers out into
separate physical tiers, for scalability. The data access code should be
separated out into its own layer that focuses on performing translation
services between the databases and the application domain. Services should be
placed in a separate service layer that focuses on performing translation
services between the service-oriented external world and the application
domain.
The service interface layer will be compiled into a separate class assembly, and
hosted in a service host environment. The outside world will only know about
and have access to this layer. Whenever a request is received by the service
interface layer, the request will be dispatched to the business logic layer,
and the business logic layer will get the actual work done. If any database
support is needed by the business logic layer, it will always go through the
data access layer.
More Related links
ASP.NET Session
State Management questions with answers
Define Session, SessionId and Session State in ASP.NET. |
What is Session Identifier? | Advantages and disadvantages of using Session
State Management. | What are the Session State Modes? | Define each Session
State mode supported by ASP.NET.
ASP.NET Validation
Control questions with answers
Define Validation Control in ASP.NET. | What are the
validation types supported by ASP.NET? | Describe the steps to use Validation
Control.
Interview
questions and answers on .Net web service
What is XML Web Server? | Describe the Components that help locate and consume
XML Web Service. | Describe the process of communication between Client and Web
Service. | What are the components published while deploying of Web Service? |
Describe .disco file. | Describe the step to be followed to access web service
by client.
|