Hello All, We are going to start new batch from next week. message/call or mail us for more details.

4 September 2013

Implementation of WCF Service

In implementation of WCF involve the creation of method, types, services which are used by the client.

Endpoint


All possible communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Each Endpoint provides clients access to the functionality offered by a WCF service.

WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world.

All the WCF communications are take place through end point. End point consists of three components.

Ø  Address

Basically this are URLs, that specifies where this WCF service is hosted (on which server iis, self etc) that will use by the client. To access the service use this url to connect to the service. e.g. http://localhost:8090/MyFirstService/MyCalculator.svc

Ø  Binding


Binding will describes how client will communicate with service. There are number of different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

 The binding process has following characteristics as:

  • Transport – this defines what protocol is being used like HTTP, Named Pipes, TCP, and MSMQ are some type of protocols.
  • Encoding (This is Optional) – There are basically three types of encoding are available- namely: Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
  • Protocol (This is also Optional) - It defines what information to be used in the binding such as Security, transaction or reliable messaging capability etc.

The following table gives some list of protocols supported by WCF binding.

 

Binding
Description
BasicHttpBinding
Basic Web service communication. No security by default
WSHttpBinding
Web services with WS-* support. Supports transactions
WSDualHttpBinding
Web services with duplex contract and transaction support
WSFederationHttpBinding
Web services with federated security. Supports transactions
MsmqIntegrationBinding
Communication directly with MSMQ applications. Supports transactions
NetMsmqBinding
Communication between WCF applications by using queuing. Supports transactions
NetNamedPipeBinding
Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding
Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding
Communication between WCF applications across computers. Supports duplex contracts and transactions

Ø  Contract


 Contract is the specification of what operation will endpoint communicates with particular communication request.   In other word Contract collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. Each operation is a simple exchange pattern such as one-way, duplex and request/reply.

 Basically process of Contract is acronyms as ABC‘s process. This is depicted as follows:

 A is stand for: What is the Address of   service

B is stand for:  what type of binding is being used (e.g http, tcp , MSQUE etc.)

C is stand for: what methods are contracted for use by the client.

 

 

Implementation of WCF Service

 

Example:


Endpoints will be mentioned in the web.config file on the created service.

<system.serviceModel>

<services>

      <service name="MathService"

        behaviorConfiguration="MathServiceBehavior">

       <endpoint
         address="http://localhost:8090/MyCalculator/Calculator.svc" contract="ICalculator"
          binding="wsHttpBinding"/> 

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="MathServiceBehavior">

          <serviceMetadata httpGetEnabled="True"/>

          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

 Types of Contracts:


There are basically four types of Contract signed by the WCF for providing Service to its clients are given below:

Service Contract


Service contracts describe the operation that service can provide. For E.g., a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute.


Attribute that implement these contracts are:


[ServiceContract]

And [OperationContract]

Data Contract


Data contract describes the custom data type which is exposed to the client. This defines the data types, which 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 data types cannot be identified by the client e.g. Employee data type. By using DataContract we can make client to be aware of Employee data type that are returning or passing parameter to the method.

Attribute that implement this contract is:

[DataContract]

 And [DataMember]

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.

[MessageContract]

[MessageHeader]

 [MessageBodyMember]

Fault Contract


Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.

 

 

Example:

Ex1: create simple “Hello WCF World!”this   SAMPLE ILLUSTRATES THE BASIC concept of WCF programming.
Ex2: Create a simple calculator in WCF and consume it in Windows and WEB application. This example evolve a bit of arithmetic programming in WCF but this example may be helpful for understanding basic concept  of creation and using of it.

No comments:

Post a Comment