Geeks With Blogs
Allan Rwakatungu's blog Developer and Enterprise Architect

On the 26th of May 2010 , I made a presentation to the .NET user group meeting (thanks to Malisa Ncube for organizing this event every month … ).

If you missed my presentation , we talked about why we should all be building services … better still using the .NET framework.

This blog post is an introduction to services , why you would want to build services and how you can build services using the .NET framework.

What is a service?

OASIS defines service as "a mechanism to enable access to one or more capabilities, where the access is provided using a prescribed interface and is exercised consistent with constraints and policies as specified by the service description." [1].

If the above definition sounds to academic , you can also define a service as loosely coupled units of functionality that have no calls to each other embedded in them.

Instead of services embedding calls to each other in their service code they use defined protocols that describe how services pass and parse messages. This is a good way to think about services if you’re from an objected oriented background. While in object oriented programming functions make calls to each other, in service oriented programming, functions pass messages between each other.

Why would you want to use services?

1. If your enterprise architecture looks like this

 

Services are the building blocks for SOA .

With SOA you can move away from the sphaggetti infrastructure that is common in most enterprises.

The complexity or lack of visibility of the integration points in your enterprises makes it difficult and costly to implement new initiatives and changes into the business - and even impossible in some cases - as it is not possible to identify the impact a change in one system might have to other systems.

With services you can move to an architecture like this

Your building blocks from Spaghetti infrastructure to something that is more well-defined and manageable to achieve cost efficiency and not least business agility - enabling you to react to changes in the market with speed and achieve operational efficiency and control are services.

2. If you want to become the Gates or Zuckerburger.

Have you heard about Web 2.0 ? Mashups?Software as a service (SAAS) ? Cloud computing ?

 They all offer you the opportunity to have scalable but low cost business models and they built using services. 

Some of my favorite companies that leverage services for their business models include

Services with the .NET framework

If you are a .NET developer and you want to develop services, Windows Communication Framework (WCF) is the tool for you.

WCF is Microsoft’s unified programming model (service model) for building service oriented applications.

( Before .NET 3.0 you had several models for programming services in .NET including .NET remoting, Web services (ASMX), COM +, Microsoft Messaging queuing (MSMQ) etc, after .NET 3.0 the programming model was unified into one i.e. WCF ).

Windows Communication Framework (WCF) provides you

1. A Software Development Kit (SDK) for creating SOA applications

2. A runtime for running services on the Windows platform

Why would you use Windows Communication Foundation if you’re programming services?
 

1. WCF supports interoperable and open standards e.g. WS* protocols for programming SOAP services

2. WCF has a unified programming model.
Whether you use TCP or Http or Pipes or transmitting using Messaging Queues, programmers need to learn just one way to program.

Previously you had .NET remoting, MSMQ, Web services, COM+ and they were all done differently

3. WCF has a productive programming model
You don’t have to worry about all the plumbing involved to write services.
You have a rich declarative programming model to add stuff like logging, transactions, and reliable messages in-built in the Windows Communication Framework.

4. WCF is feature rich supporting reliable messaging, transactions , AJAX and JSON, REST and so much more.

Understanding services in WCF

The basic principles of WCF are as easy as ABC
A – Address
This is where the service is located
B- Binding
This describes how you communicate with the service
e.g. Use TCP, HTTP or both. How to exchange security information with the service etc.
C – Contract
This defines what the service can do.
E.g. Pay water bill, Make a phone call

A - Addresses

In WCF, an address is a combination of transport, server name, port and path
Example addresses may include

http://localhost:8001
net.tcp://localhost:8002/MyService
net.pipe://localhost/MyPipe
net.msmq://localhost/private/MyService
net.msmq://localhost/MyService

B- Binding
 

There are numerous ways to communicate with services , different ways that a message can be formatted/sent/secured, that allows you to tailor your service for the compatibility/performance you require for your solution.

Transport

You can use
HTTP
TCP
MSMQ ,
Named pipes,
Your own custom transport etc

Message
You  can send a
plain text
binary,
Message Transmission Optimization Mechanism (MTOM) message

Communication security

You can have
No security
Transport security
Message security etc

Behaviour

You service can support
Transactions
Be reliable
Use queues
Support ajax etc

C - Contract

You define what your service can do using

Service contracts . Operations that your service can do, communications and behaviours

Data contracts . Messages that are passed from and into your service and how they are formatted

Fault contracts . Errors types in your service

 

As an example, suppose your service shows money.

You define your service contract using a interface

[ServiceContract]
public interface IShowMeTheMoney
{
  [OperationContract]
   Money Show();
}

You define the data contract by annotating a class it with the Data Contract attribute and fields you want to pass in the message as Data Members.

(Note:- In the latest versions of WCF you dont have to use attributes if you passing all the objects properties in the message)


[DataContract]
public Money
{
  [DataMember]
  public string Currency { get; set; }
  [DataMember]
  public Decimal Amount { get; set; }
  public string Comment { get; set; }
}

Features of Windows Communication Foundation

Windows Communication Foundation is not only easy to use but feature rich , offering you several options to tweak your service to fit your business requirements.

Some of the features of WCF include

  •  Hosting options in Windows process activation server (WAS) and Internet Information Services , Windows services, Console applications , Forms and Web.
  •  Workflow services You can combine WCF with Windows WorkFlow Foundation (WWF) to write workflow type services
  • Control how your data (messages) are transferred and serialized e.g. you can serialize your business objects as XML or binary
  • Control over session management , instance creation and concurrency management without writing code if you like
  • Queues and reliable sessions. You can store messages from the sending client and later forward them to the receiving application. You can also guarantee that messages will arrive at their destincation.
  • Transactions:  You can have different services participate in a transaction operations that can be rolled back if needed
  • Security. WCF has rich features for authorization and authentication  as well as keep audit trails
     
  • Web programming model. WCF allows developers to expose services as non SOAP endpoints
  • Inbuilt features that you can use to write JSON and services that support AJAX applications

And lots more

In my next blog I will show you how to build the real world application that we demo'ed at the meeting.

 

Posted on Monday, June 7, 2010 5:40 AM Services , Windows Communication Foundation , Service Oriented Architecture (WCF) | Back to top


Comments on this post: Building services with the .NET framework

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Allan Rwakatungu | Powered by: GeeksWithBlogs.net