Thursday, April 28, 2016

WCF Part 4 : Message , Channel , Client & Metdata


Message & Channel

Message is unit of data exchange between client and service. Message has several parts including body and headers.


WCF Runtime

Set of objects responsible for sending and receiving message, like formatting, applying security , transmitting and receiving message by applying various protocol.

Channels

This defines how message is transmitted to and from endpoint


  • Transport Channel - Handles sending & receiving message from network. Protcols like Http, TCP etc.
  • Protcol channel- Implements SOAP based protocol by processing and possibly modifying message.

WCF Client

An application desired to consume or use a service needs a client to do so.

Step 1   Get proxy class and endpoint details
             This creates a class and configuration file
Step 2   Call Operation
              Add class created above to application, create an object , invoke the method.
              When the client application calls the first operation, WCF automatically opens the                                underlying  channel. This underlying channel is closed, when the object is recycled.
Step 3     Close Wcf Object/client



Metadata

Characteristics of the service are described by the metadata. This metadata can be exposed to the client to understand the communication with service. Metadata can be set in the service by enabling the ServiceMetadata node inside the servcieBehaviour node of the service configuration file.

<system.serviceModel>
    <services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
        <endpoint address="" contract="IMathService"
          binding="wsHttpBinding"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceBehavior">
    <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


WCF : Service Contract

Service Contract


WCF Part 3 : Binding , Contracts & Service Host


Windows Communication Foundation (WCF) 

Chapter 2: Fundamentals

Binding & Behavior

Binding

Binding describes how we communicate with service.

Scenario : We have a service been used  - one client via soap http and other via TCP

<system.serviceModel>
    <services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
      <endpoint address="http://localhost:8090/MyService/MathService.svc" 
        contract="IMathService"
          binding="wsHttpBinding"/>
<endpoint address="net.tcp://localhost:8080/MyService/MathService.svc" 
contract="IMathService"
          binding="netTcpBinding"/> 
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  • common behaviors affect all endpoints globally.
  • Service behaviors affect only service-related aspects
  • Endpoint behaviors affect only endpoint-related properties
  • Operation-level behaviors affect particular operations.


Note:

Application can be controlled either through coding, configuring or through combination of both. Specification mention in the configuration can also be overwritten in code.


Contracts & Service Host

Contracts

All services are exposed as contract. Contract is standard way of describing what service does.

4 types of contracts
Service contract - It describes operation that a service provides. Ex. Service providing Temp based on zipcode.
Data Contract - This explains types of data (custom data type) that is exposed to client. By using data contract we let client know about Custom data type.
Message Contract - Default is SOAP, if this does not meet requirement, using message contract message format fitting requirement can be achieved.
Fault Contract - Provides documented error to client when service fails to work.

Service Host
  • Service host object is process of hosting the service and registering endpoints.
  • It loads service configuration endpoints, apply settings and start listeners to handle incoming service request.
  • System.ServiceModel.Servicehost namespace holds this object. Object is created while self hosting wcf service.



WCF Part 2 : Endpoints & Contract


Windows Communication Foundation (WCF) 

Chapter 2: Fundamentals

  • End Point 
  • Bindings and Behavior 
  • Contracts and Service host 
  • Message and Channel 
  • WCF client and Metadata
END Point 
A web service endpoint is an entity, processor, or resource that can be referenced and to which web services messages can be addressed. 
Endpoint references convey the information needed to address a web service endpoint. Clients need to know this information before they can access a service.

Endpoint consists 3 components

Address
Specifies where service is hosted, it is basically a web address.
Ex. http://localhost:8090/MyService/SimpleCalculator.svc

Binding

Binding describes how client communicate with the service.

Binding has several characteristics

Transport : Defines protocol like HTTP, TCP, MSMQ etc.
Encoding : Text, Binary, Message Transmission and Optimization Mechanism.
Protocol : Defines information to be used in the binding such as Security, transaction or reliable messaging capability.


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

BindingDescription
BasicHttpBindingBasic Web service communication. No security by default
WSHttpBindingWeb services with WS-* support. Supports transactions
WSDualHttpBindingWeb services with duplex contract and transaction support
WSFederationHttpBindingWeb services with federated security. Supports transactions
MsmqIntegrationBindingCommunication directly with MSMQ applications. Supports transactions
NetMsmqBindingCommunication between WCF applications by using queuing. Supports transactions
NetNamedPipeBindingCommunication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBindingCommunication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBindingCommunication between WCF applications across computers. Supports duplex contracts and transactions

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.


<system.serviceModel>
<services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
       <endpoint
         address="http://localhost:8090/MyService/MathService.svc" contract="IMathService"
          binding="wsHttpBinding"/> 
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

WCF Part 1 : Introduction


Windows Communication Foundation (WCF) 

Chapter 1 : Introduction







Code Base Name : Indigo
WCF is a programming platform and run-time system for building, configuring and deploying network distributed system.

Advantage


  • WCF is inter-operable with other services when compared to .Net Remoting,where the client and service have to be .Net. 
  • WCF services provide better reliability and security in compared to ASMX web services
  • In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements. 
  • WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code. 


Disadvantage

Making right design for your requirement is little bit difficult.

Different between web service and WCF Service

FeaturesWeb ServiceWCF
HostingIt can be hosted in IISIt can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming[WebService] attribute has to be added to the class[ServiceContraact] attribute has to be added to the class
Model[WebMethod] attribute represents the method exposed to client[OperationContract] attribute represents the method exposed to client
OperationOne-way, Request- Response are the different operations supported in web serviceOne-Way, Request-Response, Duplex are different type of operations supported in WCF
XMLSystem.Xml.serialization name space is used for serializationSystem.Runtime.Serialization namespace is used for serialization
EncodingXML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, CustomXML 1.0, MTOM, Binary, Custom
TransportsCan be accessed through HTTP, TCP, CustomCan be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
ProtocolsSecuritySecurity, Reliable messaging, Transactions

Wednesday, April 27, 2016

ASMX Web Service


Web API






Web API is the great framework for exposing your data and service to different-different devices. Moreover Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats) and you don't need to define any extra config settings for different devices unlike WCF Rest service.




Web API Features


  • It supports convention-based CRUD Actions since it works with HTTP verbs GET,POST,PUT and DELETE. 
  • Responses have an Accept header and HTTP status code. 
  • Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter. 
  • It may accepts and generates the content which may not be object oriented like images, PDF files etc. It has automatic support for OData. Hence by placing the new [Queryable] attribute on a controller method that returns IQueryable, clients can use the method for OData query composition. 
  • It can be hosted with in the applicaion or on IIS. 
  • It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection that makes it more simple and robust. 


Why to choose Web API ?


  • If we need a Web Service and don’t need SOAP, then ASP.Net Web API is best choice. 
  • It is Used to build simple, non-SOAP-based HTTP Services on top of existing WCF message pipeline. 
  • It doesn't have tedious and extensive configuration like WCF REST service. 
  • Simple service creation with Web API. With WCF REST Services, service creation is difficult. 
  • It is only based on HTTP and easy to define, expose and consume in a REST-ful way. 
  • It is light weight architecture and good for devices which have limited bandwidth like smart phones. It is open source.