Building services with .Net Part 1
Posted
by Allan Rwakatungu
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Allan Rwakatungu
Published on Mon, 07 Jun 2010 16:40:28 GMT
Indexed on
2010/06/07
17:53 UTC
Read the original article
Hit count: 762
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 the.
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
- https://www.salesforce.com/ (cloud CRM)
- http://www. twitter.com (more people use twitter clients built by 3rd parties than their official clients)
- http://www.kayak.com/ (compares data from other travel sites to give information to users in one location)
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. An Software Development Kit (SDK) for creating SOA applications
2. A runtime for running services on the Windows platform
Why should you use Windows Communication Foundation if you’re programming services?
1. It supports interoperable and open standards e.g. WS* protocols for programming SOAP services
2. It 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. 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.
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
No security
Transport security
Message security
Authenticating and authorizing callers 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 :- Define operations that your service can do, communications and behaviours
Data contracts :- Define the messages that are passed from and into your service and how they are formatted
Fault contracts :- Defines errors types in your service
As an example, suppose your service 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 simple but feature rich , offering you several options to tweak your service to fit your business requirements.
Some of the features of WCF include
1. Workflow services You can combine WCF with Windows WorkFlow Foundation (WWF) to write workflow type services
2. Control how your data (messages) are transferred and serialized e.g. you can serialize your business objects as XML or binary
3. control over session management , instance creation and concurrency management without writing code if you like
4. 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.
5.Transactions: You can have different services participate in a transaction operations that can be rolled back if needed
6. Security. WCF has rich features for authorization and authentication as well as keep audit trails
7. Web programming model. WCF allows developers to expose services as non SOAP endpoints
8. 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 you can use WCF features to write a real world business service.
<-[if gte mso 9]>
© Geeks with Blogs or respective owner