Custom Search
Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

WCF Architecture


The following figure illustrates the major components of WCF.



Figure 1: WCF Architecture

Contracts

Contracts layer are next to that of Application layer. Developer will directly use this contract to develop the service. We are also going to do the same now. Let us see briefly what these contracts will do for us and we will also know that WCF is working on message system.

Service contracts

- Describe about the operation that service can provide. Example, Service provided to know the temperature of the city based on the zip code, this service we call as Service contract. It will be created using Service and Operational Contract attribute.

Data contract

- It describes the custom data type which is exposed to the client. This defines the data types, are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or datatype cannot be identified by the client e.g. Employee data type. By using DataContract we can make client aware that we are using Employee data type for returning or passing parameter to the method.

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).

Read more...

Overview of WCF


WCF is a .NET platform for developing services .Service is any functionality that can be consumed by the outside world. A service defines well defined communication protocols to communicate with it.

A service can be located anywhere , it could be on local machine on the intranet or even internet.A client consumes the functionality exposed by the service.A service can be anything it can be an independent application like asp.net or it can be another service.

Client and Service communicate with each other by sending messages.

SOAP Message SOAP Message

Client HTTP/TCP /MSMQ HTTP/TCP/MSMQ Server

WCF.gif

Figure: Basic Client Server Communication

Since the functionality of the service is hidden to the outside world. Service exposes metadata that client uses to communicate with the service. Metadata can be in any technology neutral format like wsdl.

Client always uses proxy to communicate with the service irrespective of where service is located. In WCF every service has endpoints and the client uses theses endpoints to communicate with the service.

Every endpoint has three important pieces of information.

Address , Binding and Contract.

Address specifies the location of the service.

Binding specifies how to communicate with the service(protocol).

Contract specifies what the service does.

A typical endpoint in the configuration file appears as :

<endpoint address = "http://localhost/TestService/" binding="wsHttpBinding"
contract = "MyNamespace.IContract"/>

WCF has mainly following types of contracts
  1. Service Contract: Describes the operations of the services
  2. Operation Contract: Describes the operations available on the service
  3. Data Contract: Define the data types passed to and from the service
  4. Fault Contract: Define the erros raised by the service
  5. Message Contract: Allows the service to interact directly by using the message.
Basic WCF service

System.ServiceModel.dll is the main assembly for WCF services. To create a WCF service we need to apply the service contract attribute either on the interface or the class.

namespace WcfService1
{
     [ServiceContract]
    public interface IService1    {
        [OperationContract]
        string HelloWorld();

    }
    public class Service1 : IService1    {      
          public string HelloWorld()
            {
                return "Hello WCF";
            }
      }
}

We can also use explicit interface implementation like this :

         string IService1.HelloWorld()
            {
                return "Hello WCF";
            }

Note when using explicit interface implementation we can't use the public access modifier.

Method Overloading

To use method overloading we need to use the name attribute of the OperationContract attribute.

If we write the following implementation for method overloading it will throw an exception at service host load time.

        [OperationContract]
        string HelloWorld();

        [OperationContract]
        string HelloWorld(string Message);

To use method overloading in WCF service we need to use the Name attribute of the OperationContract attribute.

        [OperationContract(Name="HelloWorld")]
        string HelloWorld();

        [OperationContract(Name = "HellowWorldMssg")]
        string HelloWorld(string Message);

In the later articles we will see how to host the service and call it using the client application.

Data Contracts

We can pass only the primitive datatypes to OperationContracts .If we want to pass custom data types to OperationContracts we need to use the DataContract Attribute on the Custom datatype.

    [DataContract]
    class Customer    {
        int name;
        [DataMember]
        public int Name
        {
            get { return name; }
            set { name = value; }
        }
        int address;
        [DataMember]
        public int Address
        {
            get { return address; }
            set { address = value; }
        }
    }

Read more...

Back to TOP