wcf examples

Upload: ravimmcc

Post on 09-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 WCF Examples

    1/11

    WCF Examples

    Introduction

    WCF (Windows Communication Framework) is a unification technology, which unitesthe following technologies:

    NET remoting MSMQ Web services COM+

    It is based on SOA (Service Oriented Architecture).

    What are Ends, Contract, Address, and Bindings?

    The above terminologies are the core on which SOA stands. Every service must exposeone or more ends by which the service can be available to the client. End consists ofthree important things: where, what and how:

    Contract (What)Contract is an agreement between two or more parties. It defines the protocol how theclient should communicate with your service. Technically, it describes parameters andreturn values for a method.

    Address (Where)An address indicates where we can find this service. Address is a URL, which points to

    the location of the service.

    Binding (How)Bindings determine how this end can be accessed. It determines how communicationsare done. For instance, you expose your service, which can be accessed using SOAPover HTTP or BINARY over TCP. So for each of these communications medium twobindings will be created.

    Step 1: Open VS2008 , create project and choose "Windows Service Application", justgive "MyService" as the name to our project..

    Start Programs VS2008File New Project

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page1 of11

  • 8/8/2019 WCF Examples

    2/11

    Rename the existing IService1.cs as IMyService.csRename the Service1.svc file as MyService.svc

    Also Change Service1 into MyService in web.configAlso in MyService.svc file.

  • 8/8/2019 WCF Examples

    3/11

    CodeBehind="MyService.svc.cs" %>

    Step : 2 IMyService.cs

    This is an interface program. Modify the GetData function declaration. Type the newmethod findSI.

    Coding :

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace WCFTest{

    [ServiceContract]public interface IMyService{

    [OperationContract]string GetData();[OperationContract]double findSI(double x, double y, double z);

    [OperationContract]CompositeType GetDataUsingDataContract(CompositeType composite);

    }[DataContract]

    public class CompositeType{

    bool boolValue = true;string stringValue = "Hello ";[DataMember]public bool BoolValue{

    get { return boolValue; }set { boolValue = value; }

    }[DataMember]

    public string StringValue{get { return stringValue; }set { stringValue = value; }

    }}

    }

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page3 of11

  • 8/8/2019 WCF Examples

    4/11

    Step : 3 MyService.svc.CS

    Here coding for the methods declared in the interface are written.

    using System;

    using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace TestWCF{

    public class MyService : IMyService{

    public string GetData(){

    return string.Format("Welcome to mmcc");

    }public double findSI(double x, double y, double z){

    return x * y * z / 100;}public CompositeType GetDataUsingDataContract(CompositeType composite){

    if (composite.BoolValue){

    composite.StringValue += "Suffix";}

    return composite;}

    }}

    Step : 4

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page4 of11

  • 8/8/2019 WCF Examples

    5/11

    This is the most important step. In this, we are declaring the end point. Inside this, wecan define an end point as shown in the picture. End point is defined automatically. Wecan also define it by program.

    web.config

    Step : 5

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page5 of11

  • 8/8/2019 WCF Examples

    6/11

    Save the project and run it. This will display like this. Copy the address from exploreraddress bar.

    http://localhost:2157/MyService.svc

    Let the server service run (the above page).

    Client Side :

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page6 of11

  • 8/8/2019 WCF Examples

    7/11

    Start Programs VS2008File New WebSite

    Step 1:

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page7 of11

  • 8/8/2019 WCF Examples

    8/11

    Open a web application in another VS2008 and right click on solution name and then goto "Add Service Reference" one window will open paste the previously copied link to theaddress bar and press GO. Then the service will appear in service section, then pressOK. The service reference will appear in solution explorer as below:

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page8 of11

  • 8/8/2019 WCF Examples

    9/11

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page9 of11

  • 8/8/2019 WCF Examples

    10/11

    Step : 2

    Default.aspx

    Design :

    Coding :

    protected void Button1_Click(object sender, EventArgs e){

    double x, y, z;x = Convert.ToDouble(txtP.Text);y = Convert.ToDouble(txtN.Text);z = Convert.ToDouble(txtR.Text);

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page10 of11

  • 8/8/2019 WCF Examples

    11/11

    ServiceReference1.MyServiceClient cls =new ServiceReference1.MyServiceClient();

    cls.Open();//Response.Write(cls.GetData());//Response.Write(cls.addData(x, y));txtResult.Text = cls.GetData().ToString();txtSi.Text = cls.findSI(x, y, z).ToString();cls.Close();

    }

    Output :

    Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17 24323605, 9444037410 47482025 Page11 of11