tnvbalajiWCF

download tnvbalajiWCF

of 11

Transcript of tnvbalajiWCF

  • 7/27/2019 tnvbalajiWCF

    1/11

    WCF Tutorial

    tnvbalaji.com 1

    Note: Please refer http://tnvbalaji.com/terms-of-use/ for TERMS OF USE

    before reading/using the document content.

  • 7/27/2019 tnvbalajiWCF

    2/11

    WCF Tutorial

    tnvbalaji.com 2

    INDEX

    INDEX.......................................................................................................................2

    WCF Basics ...................................................................................................................3

    Definition of WCF .....................................................................................................3

    Features of WCF........................................................................................................3

    Terms of WCF ...........................................................................................................3

    WCF step by step Tutorial .............................................................................................5

    Steps for creating wcfMathSerLib.............................................................................5

    Result Using WCF Test Client ..................................................................................9

    Steps for creating ConsoleMathClient.......................................................................9

  • 7/27/2019 tnvbalajiWCF

    3/11

    WCF Tutorial

    tnvbalaji.com 3

    WCF Basics

    Definition of WCF

    Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF we can build secure, reliable, transacted solutions

    that integrate across platforms.

    WCF is a unified framework which provides

    1. NET Remoting 2.Distributed Transactions 3.Message Queues and 4.Web Services

    into a single service-oriented programming model for distributed computing.

    WCF interoperate between WCF-based applications and any other processes that

    communicate via SOAP (Simple Object Access Protocol) messages

    Features of WCF

    1. Service Orientation2. Interoperability3. Multiple Message Patterns4. Service Metadata5. Data Contracts6. Security7. Multiple Transports and Encodings8. Reliable and Queued Messages9. Durable Messages10.Transactions11.AJAX and REST Support12.Extensibility

    To know more about features of WCF

    see: http://msdn.microsoft.com/en-us/library/ms733103.aspx

    Terms of WCF

    A WCF service is exposed to the outside world as a collection of endpoints.

    EndpointEndpoint is a construct at which messages are sent or received (or both).

    Endpoint comprises ofABC

    What are ABC's of WCF ?

    A. Address - Address is a location that defines where messages can be sent

    B. Binding - Binding is a specification of the communication mechanism (abinding) that described how messages should be sent

  • 7/27/2019 tnvbalajiWCF

    4/11

    WCF Tutorial

    tnvbalaji.com 4

    C. Contract - Contract is a definition for a set of messages that can be sent or

    received (or both) at that location (a service contract) that describes what

    message can be sent.

    ServiceA construct that exposes one or more endpoints, with each endpoint exposing

    one or more service operations.Contracts:A contract is an agreement between two or more parties for common understanding

    and it is a platform-neutral and standard way of describing what the service does. In

    WCF, all services expose contracts.

    Types of Contracts:1) Operation Contract: An operation contract defines the parameters and return type

    of an operation.

    [OperationContract]

    double Add(double i, double j);

    2) Service Contract: Ties together multiple related operations contracts into a single

    functional unit.

    [ServiceContract] //System.ServiceModel

    publicinterfaceIMath{

    [OperationContract]

    double Add(double i, double j);[OperationContract]

    double Sub(double i, double j);

    [OperationContract]Complex AddComplexNo(Complex i, Complex j);[OperationContract]

    Complex SubComplexNo(Complex i, Complex j);}

    3) Data Contract: The descriptions in metadata of the data types that a service uses.

    // Use a data contract

    [DataContract] //using System.Runtime.SerializationpublicclassComplex

    {privateint real;

    privateint imaginary;

    [DataMember]

    publicint Real { get; set; }

    [DataMember]

    publicint Imaginary { get; set; }}

  • 7/27/2019 tnvbalajiWCF

    5/11

    WCF Tutorial

    tnvbalaji.com 5

    WCF step by step Tutorial

    This is the Basic WCF Tutorial wcfMathSerLib will be created in a step by step

    approach. This wcfMathSerLib will be tested by ConsoleMathClient and with

    WCF Test Client

    Steps for creating wcfMathSerLib

    1. Open Visual Studio 2010 and File->NewProject

    1. select WCF in Recent Templates

    2. select WCF Service Library

    3. Give Name as wcfMathServiceLibrary

    4. Click OK

  • 7/27/2019 tnvbalajiWCF

    6/11

    WCF Tutorial

    tnvbalaji.com 6

    2. Delete IService1.cs and Service1.cs

    3. Add IMath.cs and MathService.cs and add the code listed below

    IMath.cs

    using System.Runtime.Serialization;using System.ServiceModel;

    namespace WcfMathServLib{

    [ServiceContract] //System.ServiceModelpublicinterfaceIMath{

    [OperationContract]double Add(double i, double j);

    [OperationContract]

    double Sub(double i, double j);

    [OperationContract]

    Complex AddComplexNo(Complex i, Complex j);[OperationContract]Complex SubComplexNo(Complex i, Complex j);

    }

    // Use a data contract

    [DataContract] //using System.Runtime.SerializationpublicclassComplex

    {

    privateint real;privateint imaginary;

  • 7/27/2019 tnvbalajiWCF

    7/11

    WCF Tutorial

    tnvbalaji.com 7

    [DataMember]

    publicint Real { get; set; }

    [DataMember]

    publicint Imaginary { get; set; }}

    }

    MathService.cs

    namespace WcfMathServLib

    {publicclassMathService : IMath{

    publicdouble Add(double i, double j){

    return (i + j);}

    publicdouble Sub(double i, double j){

    return (i - j);}

    publicComplex AddComplexNo(Complex i, Complex j){

    Complex result = newComplex();result.Real = i.Real + j.Real;result.Imaginary = i.Imaginary + j.Imaginary;

    return result;}

    publicComplex SubComplexNo(Complex i, Complex j){

    Complex result = newComplex();result.Real = i.Real - j.Real;

    result.Imaginary = i.Imaginary - j.Imaginary;

    return result;}

    }}

    4.Modify the App.config file as shown

    App.config

  • 7/27/2019 tnvbalajiWCF

    8/11

    WCF Tutorial

    tnvbalaji.com 8

  • 7/27/2019 tnvbalajiWCF

    9/11

    WCF Tutorial

    tnvbalaji.com 9

    Result Using WCF Test Client

    1. Run the WcfMathServLib project you will get the WCF Test Client2. Select each method say AddComplexNo Give the values in Request3. Click on Invoke button4. See the results in Response

    Steps for creating ConsoleMathClient

    1. Open Visual Studio 2010 and File->NewProject2. select Visual C#->Windows in Installed Templates3. select Console Application4. Give Name as ConsoleMathClient5. Click OK

  • 7/27/2019 tnvbalajiWCF

    10/11

    WCF Tutorial

    tnvbalaji.com 10

    2. Go to Solution Explorer Right click on ConsoleMathClient -> Select Add Service

    Reference the below dialog will be displayed

    1. Click on Discover button

    2. Give namespace as MathServiceReference and click OK

  • 7/27/2019 tnvbalajiWCF

    11/11

    WCF Tutorial

    tnvbalaji.com 11

    The service reference will be added now modify the program.cs as shown below.

    Program.csusing System;using ConsoleMathClient.MathServiceReference;

    namespace ConsoleMathClient{

    classProgram{

    staticvoid Main(string[] args)

    {Console.WriteLine("Press to run the client....");

    Console.ReadLine();

    MathClient math = newMathClient();Console.WriteLine("Add of 3 and 2 = {0}", math.Add(3, 2));

    Console.WriteLine("Sub of 3 and 2 = {0}", math.Sub(3, 2));

    Complex no1 = newComplex();

    no1.Real = 3;no1.Imaginary = 3;

    Complex no2 = newComplex();no2.Real = 2;no2.Imaginary = 2;

    Complex result = newComplex();result = math.AddComplexNo(no1, no2);Console.WriteLine("Add of 3+3i and 2+2i = {0}+{1}i", result.Real,

    result.Imaginary);

    result = math.SubComplexNo(no1, no2);

    Console.WriteLine("Sub of 3+3i and 2+2i = {0}+{1}i", result.Real,result.Imaginary);

    Console.ReadLine();}

    }

    }

    Result

    Compile and Run the project to see the Result

    *********** THE END ***********