Thursday, April 28, 2016

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.



No comments:

Post a Comment