WCF: Exposing multiple endpoints

Of late I have been reading about WCF. Though I have many things to share but thought of sharing this link about how to expose the WCF Service as multiple end points i.e. suppose we wrote a WCF service to calculate home loan. The same service can be exposed as

  1. HTTP
  2. TCP
  3. Named Pipe

If the service is to be used by non-.NET based app then HTTP is the best option and if both service and client are in same network and both the app are .NET based tcp can be used and if both the service and client areon same machine amed pipe can be used.

The question then comes do a developer have to expose multiple interface, the answer is NO. The developer have to use the configuration files to expose the service with 3 end points.

This is one of the cool feature that I liked about WCF. the below link provides the similar stuff with an example


Click here

Understanding WCF


Windows Communication Foundation(WCF) that is part of .NET Framework 3.0 is in the line of Microsoft(MS) classic work of abstraction. MS have been innovator in providing abstraction over various technologies from many years. In the same tradition I do believe WCF is another achievement. Undoubtedly they understand the difficulties in any technologies being faced by technology implementors and then they use their experience to nake the technology simple by intoroducing the layer of abstraction.

Let me talk about WCf in particular now

  1. It was always challenge from many years to talk to remote objects.
  2. Many technologies have come and gone or become extinct as dinosors.
  3. To name a few will be CORBA or DCOM.
  4. But there was always problem with the technology itself and its implementation.
  5. Web Service was the most pouplar of all the technology and it still one of the best for such communication.
  6. The problem comes when we want to expose a remote object interface with multiple protocol interface such as HTTP, TCP etc.
  7. The problem with existing technology is that we have create multiple interface for the remote object i.e. each specific to each protocol.
  8. This really become a stumbling block while deciding on the protocol to be used as creating multiple interface results in maintainence overhead.
  9. So if we want a complete interoperable solution we need a web service based interface but if its standalone system the same Web Service interface becomes a performance bottleneck.
  10. WCF really becomes a great help for such scenario. It allows developer to create interface irrespective of the protocol to use.
  11. Developer can then configure the so called end points to decide on which protoocl to used for communication.
  12. The developer will create the interface and implement it. It will then expose the interface using multiple end points i.e. HTTP, TCP, Named Pipes.
  13. The client using the interface then can use the appropriate end point based their need.
  14. WCf as infrastructure takes care all overheads related to messaging etc

A Step by Step WCF Small Program For Beginners

Abstract

In this article, I am going to show you a small Addition programme using WCF. After going through this snippet, the reader will get a clear and basic understanding of the WCF programme.

Introduction

WCF (Windows Communication Framework) is a unification technology, which unites the following technologies:

  • NET remoting
  • MSMQ
  • Web services
  • COM+

It is based on SOA (Service Oriented Architecture).

What are Ends, Contract, Address, and Bindings?

The above terminologies are the core on which SOA stands. Every service must expose one or more ends by which the service can be available to the client. End consists of three important things: where, what and how:

Contract (What)

Contract is an agreement between two or more parties. It defines the protocol how the client should communicate with your service. Technically, it describes parameters and return values for a method.

Address (Where)

An address indicates where we can find this service. Address is a URL, which points to the location of the service.

Binding (How)

Bindings determine how this end can be accessed. It determines how communications are done. For instance, you expose your service, which can be accessed using SOAP over HTTP or BINARY over TCP. So for each of these communications medium two bindings will be created.

The Important Principles of SOA?

WCF is based on SOA. SOA is based on four important concepts:

Boundaries are Well Defined

In SOA, everything is formalized. The client who is consuming the service does not need to know how the implementation of the service is done. If you look at some old methodologies of communication like DCOM, any changes at server level the client also has to change. Therefore, the server and client implementation was so much bound that changes need to be done at all places. In SOA, the rule is if you do enhancement you do not need to change anything at the client. SOA based application only understands that there is an end point, contract, and bindings.

Note: Just to clarify shortly about end point and contract, any SOA service is exposed through an end point. End point defines three important aspects What, Where and How.

Services Evolve

Change is the law of nature and services will evolve. In SOA, services can be versioned and you can host those services in new ends. For instance, you have a service called as “Search Tickets (Ticket Number)“ which gives details based on Ticket Number and it's exposed on end point “ep1”. Tomorrow you want make your Search Tickets service more useful by also providing an extra option of allowing him to search by passenger name. Therefore, you just declare a new end “ep2” with service “Search Tickets (Ticket Number, Passenger Name)”. So the client who is consuming the service at end ep1 continues and at the other end, we have evolved our service by adding new ends ep2.

Services Share Only Schemas and Contracts

Services use Schemas to represent data and contracts to understand behavior. They do not use language dependent types or classes in order to understand data and behavior. XML is used to define schemas and contracts. Due to this, there is no heavy coupling between environments.

Service Compatibility is Policy Based

Policy describes the capabilities of the system. Depending on policies, the service can degrade to match the service for the client. For instance, your service needs to be hosted for two types of clients, one which uses Remoting as the communication methodology while the other client uses DCOM. An ideal SOA service can cater to both of them according to their communication policies.

Code Snippet

(Service/Server Side)

Steps Involved:

Step 1: Open VS2008 , create project and choose "Windows Service Application", just give any name to your project. I named it "MyService".

Step 2: You will see the solution window, in that open "IMyService.cs".

In that, you will see [ServiceContract] - below this your interface name is declared. There after you'll see[OperationContract] - your function contract should be defined here. The implementation of the function will be defined in "MyService.svc.cs" as provided in the picture.


Step 3: Open "MyService.svc.cs" and write code here as I have written code for my "addfunction". You can write your code inside your function. The picture is shown below:

Step 4: This is the most important step. In this, we are declaring the end point. Inside this, we can define an end point as shown in the picture. End point is defined automatically. We can also define it by program.

Step 5: Save the project and run it. This will display like this. Copy the address from explorer address bar.

Code Snippet

(Client Side)

Let the server service run (the above page).

Step 1: Open a web application in another VS2008 and right click on solution name and then go to "Add Service Reference" one window will open paste the previously copied link to the address bar and press GO. Then the service will appear in service section, then press OK. The service reference will appear in solution explorer as below:

Step 2: Now add one button on the default page, then double click on button you'll be in code behind section. Here create the object of the service which appeared in the solution explorer. Something like this:

Collapse
protected void Button1_Click(object sender, EventArgs e) { ServiceReference1.MyServiceClient cls =   new wcfProxycall.ServiceReference1.MyServiceClient(); cls.Open(); Response.Write( cls.addData(5, 4)); cls.Close(); }

Step 3: Now save the project and run it. If everything goes fine, then the output will be like this:

Hope this article may helped you to understand and build a simple addition program in WCF.

Basics of WCF - Contracts


Windows Communication Foundation (WCF) application is divided two layers namely Services and Clients

The WCF services can be exposed in different ways through Web, Windows Service, Self Hosting [Running Console Application]. The Endpoint [Which is having the Address, Binding, Contract(ABC)] is definded to expose service or consume it.

In our sample code so far we have developed Console Based application for both Service and Client Applications; then we have deployed the serive in IIS 6.0 in Windows Server 2003 in the first set of applications. The next set of applications are Web Applications.

For developing a WCF Service we have gone through steps
A. Sevice Layer
Step I - Designing Contracts
Step II - Service Hosting and Selecting Bindings
Step III - Configuring for Hosting the Service

B. Client Application
Step IV - Based on Sevice creating Proxy for invoking the Service

Now we will discuss futher in details.

We have looked that in both application, we have the one common thing, that is Endpoint includes the Service Contact, Address and Binding; only service hosting and client applications are different. The service contact is first item to be designed fo a Service.

A service contract is all about:

  • Grouping of operations
  • Signature of the operations for exchanging Mesages
  • Data types of these messages.
  • Protocols and serialization formats for communicating the messages

The contact is a set of specific messages organized into basic message exchange patterns (MEPs), such as request/reply, one-way, and duplex

One Way - Datagram-style delivery
Request-Reply - Immediate Reply on same logical thread
Duplex - Reply later and on backchannel (callback-style)

Three Types of Contracts
Service Contract - Operations, Behaviors and Communication Shape
Data Contract - Defines Schema and Versioning Strategies
Message Contract - Allows defining application-specific headers and unwrapped body content

All the contracts are defined on .NET application as CLR types and and on the wire it represents as XML format - WSDL/XSD/SOAP. This is implemented through Attributes. Here the details for all the types

Service
The Service and operations defines in a service through ServiceContract and OperationContract attrubutes.
Mapping: CLR types -> Web Services Description Language (WSDL)

Data
Describes a data structure using DataContract and DataMember attributes.
Mapping: CLR types -> XML Schema Definition (XSD)

Message
Defines the structure of the message on the wire using MessageContract, MessageHeader, MessageBody
Mapping: CLR types -> Simple Object Access Protocol (SOAP) messages.

Fault/Exception
For any CLR exceptions defined as fault contract using FaultContract attribute and the fault's CLR Type converts to SOAP faults.
Mapping: CLR types -> SOAP faults

LINQ 101 Samples



.NET Cheat Sheets

.NET Cheat Sheets

.NET Format String Quick Reference

Visual Studio Built-in Code Snippets

ASP.NET 2.0 Page Life Cycle & Common Events


Other Cheat Sheet Links

More Cheat Sheats :

cheat sheets

You can download it here: