SlideShare a Scribd company logo
http://debugmode.net




Getting Started with
        WCF
           Dhananjay Kumar
           @debug_mode
What definition you get on web ?
Service Oriented programming model to
develop connected applications


SDK to develop and deploy services on windows

Unifies the existing suite of .Net distributed
technologies into a single programming model.

                Microsoft Innovation & Practice Team,
                                              MSCoE     2
Getting Started with WCF




Developer Evangelist Telerik

Microsoft MVP

Mindcracker MVP

@debug_mode

fb.com/debugmode.net

Delhi User Group

C-Sharp corner User Group
Services and Clients



     Service   Message   Client
Let us start
with a Demo
Endpoints


  Client                      Service

                          Endpoint

     Endpoint   Message   Endpoint
End Points




  A          B   C   E


                         7
End Points


                          Contracts
             Address



                       Binding




              End Point
                                      8
End Point

ADDRESS
           •Where
BINDING
           •How
CONTRACT
           •What
9
Address (A)
Every service is associated with a unique
  address.

         Location of
         the Service

                          Address
                           of the
          Transport
                          Service
           protocol
           used in
            service

                                            10
Address (A)

Address specifies where the service is residing.


This is an URL( Uniform Resource Locator).


Address URL identifies , location of the service

Address should follow the Web Service
Addressing(WS-Addressing) standard.
                                                   11
Address (A)


                 WS
              Addressing

Scheme   Machine       Port   Path



                                     12
Address (A)

              • This is top level portion of the address.
 Scheme         This is not as same as of protocols.

              • This identifies the machine name. This
Machine         can be a public URL or a local identifier

              • This is port number. This is an optional
   Port         part.

              • This is used to locate the path. This
   Path         gives path of the service.
                                                        13
Address (A)
               • http://localhost:8001/Dell
   HTTP        • http://localhost/Dell

               • net.tcp://localhost:800/Dell
    TCP        • net.tcp://localhost/Dell

               • net.msmq/private/MyService
  MSMQ         • Net.msmq/://localhost/Dell


               • net.pipe://localhost/MyPipe
    IPC




               • Net.pipe://localhost.MyService
Peer network



                                                  14
Binding (B)
Describes how a service communicates.


This specifies which Protocol to be used.


This specifies the encoding method to format the message content.


This specifies the Security requirements


This specifies the message exchange format.


This specifies message session mode.

Developer could create custom Binding also.
15
Binding (B)
Transport Protocol

Message Encoding

Communication Pattern

Security

Transaction Property

Inter Operability

16
Binding (B)
      Choosing Binding

                  No           WCF      Yes
                                To
                               WCF



                                                        No                        Yes
                                                                   Disconnected
                                                                       Calls



 No                Yes
         Legacy                        No                    Yes
                                              Cross
          ASMX                                Machine




WS                     Basic         IPC                       TCP                      MSMQ


 17
Binding (B)
Binding Classes
Binding Name as of class   Transport    Encoding    Interoperable

BasicHttpBinding           HTTP/HTTPS   Text,MTOM   Yes
NetTcpBinding              TCP          Binary      No
NetPeerBinding             P2P          Binary      No
NetNamedPipeBinding        IPC          Binary      No
WSHttpBinding              HTTP/HTTPS   Text,MTOM   Yes
WSFederationHttpBinding    HTTP/HTTPS   Text,MTOM   Yes
WSDualHttpBinding          HTTP         Text,MTOM   Yes
NetMsmqBinding             MSMQ         Binary      No
MsmqIntegrationBinding     MSMQ         Binary      Yes

18
Binding (B)
Configuring Binding

     <endpoint address="Calculator"
               bindingSectionName="basicProfileBinding"
               bindingConfiguration="Binding1"
               contractType="ICalculator" />
     <bindings>
       <basicProfileBinding>
         <binding configurationName="Binding1"
                  hostnameComparisonMode="StrongWildcard"
                  transferTimeout="00:10:00"
                  maxMessageSize="65536"
                  messageEncoding="Text"
                  textEncoding="utf-8"
         </binding>
       </basicProfileBinding>
     </bindings>
19
Service Contract (C)

A WCF Contract is a collection of Operations


All WCF services exposes contract.

This is a platform neutral and standard way to say , what
the service will do.

Defines , what a Service communicates.

20
Contract (C)
Types of Contracts
 Service • Describes which operation client can perform on the services.
Contract

           • Defines which Data Type are passed to and from the services. It provides built-
  Data       in contract for implicit type.
Contract

           • Which error raise by service and how service propagates and handles error to its
 Fault       client.
Contract

         • Allow the service to interact directly with the message . Message contract can
Message    be typed or un typed. This can be used for interoperability.
Contract


21                                                                                              21
Contract (C)
 Service Contract

A Service Contract reflects specific business


Describe which operations client can
perform

Maps CLR types to WSDL
 22
Contract (C)
 Service Contract

Interfaces are used to explicitly define a Contract


Classes may also be used to define a Contract.

[ServiceContract] attribute is being used by
interface/class to qualify them as a contract.

ServiceContract are implicitly public.
 23
Contract (C)
Service Contract
     // Define a service contract.
     [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
     public interface IDataContractCalculator
     {
         [OperationContract]
         ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
         [OperationContract]
         ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
         [OperationContract]
         ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
         [OperationContract]
         ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);
     }




24
Contract (C)
Data Contract
These are the contractual agreement about the format
and structure of the payload data in message exchange
between a service and its consumer.

Defines which Data types are passed to and from the
service.


Its specifies CLR type to XML schema.
25
Contract (C)
 Data Contract
DataContract are preferred WCF way to enable
Serialization.

WCF defines implicit contract for built in types like int and
string.

Developer has to explicitly expose complex types as Data
Contract.

[DataContract] and [DataMember] attribute are used to
define a type as Data Contract.
 26
Contract (C)
Data Contract

     [DataContract]
     public class ComplexNumber
     {
         [DataMember]
         public double Real = 0.0D;
         [DataMember]
         public double Imaginary = 0.0D;
     }


27
Contract (C)
 Message Contract
It gives control over SOAP message structure on both header and body
content.

Developer can designate optional SOAP headers.


It provides additional control over the WSDL generation.


It is used to interoperate with another non- WCF service.


It is used to control security issue at level of message.
 28
Contract (C)
 Fault Contract

This translates .Net Exception to SOAP fault propagated to consumer


This can be applied to operation only.


This is not Inheritable.


This can be applied multiple times.

This enables developer to declare which faults a given service operation
might issue if things goes wrong.
 29
Creating End Point


Could be created declaratively in configuration file.


Could be created imperatively through code.

Any thing done through code can be done with configuration file and
vice versa.

It is considered good practice to use configuration file to specify End
points. This accommodates changes without recompiling the code.
 30
Defining Endpoints


<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.serviceModel>
    <services>
      <service serviceType="CalculatorService">
        <endpoint address="Calculator"
                  bindingSectionName="basicProfileBinding"
                  contractType="ICalculator" />
      </service>
    </services>
  </system.serviceModel>
</configuration>
Multiple End Point

For service exposed to multiple clients ; it makes sense to
specify more than one End point.

This enables client to use the endpoint that is most
applicable for them.

When creating multiple endpoints each client must have
unique address

If two client uses the same address , error will raise at
service load time.
  32
Multiple End Point
• <endpoint address="http://localhost:8890/a"
  binding="wsHttpBinding"
  contract="HotsingSamples.IService1"/>
• <endpoint address="http://localhost:8000/b"
  binding="basicHttpBinding"
  contract="HotsingSamples.IService1"/>
• <endpoint address="net.tcp://localhost:8001/c"
  binding="netTcpBinding"
  contract="HotsingSamples.IService1"/>


33
Hosting
WCF services can not exist in void.


It must be hosted.

WCF services are hosted in windows process called host
process.

A single host process can host multiple service.


A same service can be hosted in multiple host process.
 34
WCF basic task cycle

                             1.
                         Defining
                          Service
                         Contract
                                              2.
           5.                             Impleme
        Building                            nting
         Clients                           Service
                                          Contract



                                       3.
                 4.
                                    Configuri
              Hosting
                                       ng
              Services
                                    Services

                                                     35
DEBUG_MODE           http://debugmode.net




             Dhananjay Kumar

More Related Content

WCF for begineers

  • 1. http://debugmode.net Getting Started with WCF Dhananjay Kumar @debug_mode
  • 2. What definition you get on web ? Service Oriented programming model to develop connected applications SDK to develop and deploy services on windows Unifies the existing suite of .Net distributed technologies into a single programming model. Microsoft Innovation & Practice Team, MSCoE 2
  • 3. Getting Started with WCF Developer Evangelist Telerik Microsoft MVP Mindcracker MVP @debug_mode fb.com/debugmode.net Delhi User Group C-Sharp corner User Group
  • 4. Services and Clients Service Message Client
  • 6. Endpoints Client Service Endpoint Endpoint Message Endpoint
  • 7. End Points A B C E 7
  • 8. End Points Contracts Address Binding End Point 8
  • 9. End Point ADDRESS •Where BINDING •How CONTRACT •What 9
  • 10. Address (A) Every service is associated with a unique address. Location of the Service Address of the Transport Service protocol used in service 10
  • 11. Address (A) Address specifies where the service is residing. This is an URL( Uniform Resource Locator). Address URL identifies , location of the service Address should follow the Web Service Addressing(WS-Addressing) standard. 11
  • 12. Address (A) WS Addressing Scheme Machine Port Path 12
  • 13. Address (A) • This is top level portion of the address. Scheme This is not as same as of protocols. • This identifies the machine name. This Machine can be a public URL or a local identifier • This is port number. This is an optional Port part. • This is used to locate the path. This Path gives path of the service. 13
  • 14. Address (A) • http://localhost:8001/Dell HTTP • http://localhost/Dell • net.tcp://localhost:800/Dell TCP • net.tcp://localhost/Dell • net.msmq/private/MyService MSMQ • Net.msmq/://localhost/Dell • net.pipe://localhost/MyPipe IPC • Net.pipe://localhost.MyService Peer network 14
  • 15. Binding (B) Describes how a service communicates. This specifies which Protocol to be used. This specifies the encoding method to format the message content. This specifies the Security requirements This specifies the message exchange format. This specifies message session mode. Developer could create custom Binding also. 15
  • 16. Binding (B) Transport Protocol Message Encoding Communication Pattern Security Transaction Property Inter Operability 16
  • 17. Binding (B) Choosing Binding No WCF Yes To WCF No Yes Disconnected Calls No Yes Legacy No Yes Cross ASMX Machine WS Basic IPC TCP MSMQ 17
  • 18. Binding (B) Binding Classes Binding Name as of class Transport Encoding Interoperable BasicHttpBinding HTTP/HTTPS Text,MTOM Yes NetTcpBinding TCP Binary No NetPeerBinding P2P Binary No NetNamedPipeBinding IPC Binary No WSHttpBinding HTTP/HTTPS Text,MTOM Yes WSFederationHttpBinding HTTP/HTTPS Text,MTOM Yes WSDualHttpBinding HTTP Text,MTOM Yes NetMsmqBinding MSMQ Binary No MsmqIntegrationBinding MSMQ Binary Yes 18
  • 19. Binding (B) Configuring Binding <endpoint address="Calculator" bindingSectionName="basicProfileBinding" bindingConfiguration="Binding1" contractType="ICalculator" /> <bindings> <basicProfileBinding> <binding configurationName="Binding1" hostnameComparisonMode="StrongWildcard" transferTimeout="00:10:00" maxMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" </binding> </basicProfileBinding> </bindings> 19
  • 20. Service Contract (C) A WCF Contract is a collection of Operations All WCF services exposes contract. This is a platform neutral and standard way to say , what the service will do. Defines , what a Service communicates. 20
  • 21. Contract (C) Types of Contracts Service • Describes which operation client can perform on the services. Contract • Defines which Data Type are passed to and from the services. It provides built- Data in contract for implicit type. Contract • Which error raise by service and how service propagates and handles error to its Fault client. Contract • Allow the service to interact directly with the message . Message contract can Message be typed or un typed. This can be used for interoperability. Contract 21 21
  • 22. Contract (C) Service Contract A Service Contract reflects specific business Describe which operations client can perform Maps CLR types to WSDL 22
  • 23. Contract (C) Service Contract Interfaces are used to explicitly define a Contract Classes may also be used to define a Contract. [ServiceContract] attribute is being used by interface/class to qualify them as a contract. ServiceContract are implicitly public. 23
  • 24. Contract (C) Service Contract // Define a service contract. [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface IDataContractCalculator { [OperationContract] ComplexNumber Add(ComplexNumber n1, ComplexNumber n2); [OperationContract] ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2); [OperationContract] ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2); [OperationContract] ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2); } 24
  • 25. Contract (C) Data Contract These are the contractual agreement about the format and structure of the payload data in message exchange between a service and its consumer. Defines which Data types are passed to and from the service. Its specifies CLR type to XML schema. 25
  • 26. Contract (C) Data Contract DataContract are preferred WCF way to enable Serialization. WCF defines implicit contract for built in types like int and string. Developer has to explicitly expose complex types as Data Contract. [DataContract] and [DataMember] attribute are used to define a type as Data Contract. 26
  • 27. Contract (C) Data Contract [DataContract] public class ComplexNumber { [DataMember] public double Real = 0.0D; [DataMember] public double Imaginary = 0.0D; } 27
  • 28. Contract (C) Message Contract It gives control over SOAP message structure on both header and body content. Developer can designate optional SOAP headers. It provides additional control over the WSDL generation. It is used to interoperate with another non- WCF service. It is used to control security issue at level of message. 28
  • 29. Contract (C) Fault Contract This translates .Net Exception to SOAP fault propagated to consumer This can be applied to operation only. This is not Inheritable. This can be applied multiple times. This enables developer to declare which faults a given service operation might issue if things goes wrong. 29
  • 30. Creating End Point Could be created declaratively in configuration file. Could be created imperatively through code. Any thing done through code can be done with configuration file and vice versa. It is considered good practice to use configuration file to specify End points. This accommodates changes without recompiling the code. 30
  • 31. Defining Endpoints <?xml version="1.0" encoding="utf-8" ?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <service serviceType="CalculatorService"> <endpoint address="Calculator" bindingSectionName="basicProfileBinding" contractType="ICalculator" /> </service> </services> </system.serviceModel> </configuration>
  • 32. Multiple End Point For service exposed to multiple clients ; it makes sense to specify more than one End point. This enables client to use the endpoint that is most applicable for them. When creating multiple endpoints each client must have unique address If two client uses the same address , error will raise at service load time. 32
  • 33. Multiple End Point • <endpoint address="http://localhost:8890/a" binding="wsHttpBinding" contract="HotsingSamples.IService1"/> • <endpoint address="http://localhost:8000/b" binding="basicHttpBinding" contract="HotsingSamples.IService1"/> • <endpoint address="net.tcp://localhost:8001/c" binding="netTcpBinding" contract="HotsingSamples.IService1"/> 33
  • 34. Hosting WCF services can not exist in void. It must be hosted. WCF services are hosted in windows process called host process. A single host process can host multiple service. A same service can be hosted in multiple host process. 34
  • 35. WCF basic task cycle 1. Defining Service Contract 2. 5. Impleme Building nting Clients Service Contract 3. 4. Configuri Hosting ng Services Services 35
  • 36. DEBUG_MODE http://debugmode.net Dhananjay Kumar