blogspot visit counter

Wednesday, 1 May 2013

Basic Step by Step WCF WebService

Basic Step by Step WCF WebService[-2]
Categories :- create restful wcf service api using post ,   Introduction of WCF web service


Introdution- : -
 In my Previous Article I have create restful wcf service api using post and Introduction of WCF web service  In this post i am explain how i can create a WCF service in asp.net and call through windows from


Description : -
Creating a simple 'Hello World'-like WCF service is very simple with Visual Studio 2010. Open Visual Studio 2010 and select New => Project => WCF Service. Name it for example, HelloWorldWCF. It will initially create 2 files for you (IService.cs and IService.svc). Delete them.
Right-click on your project and add a new WCF Service named HW.svc. Your project should look something like this.

Open the IService1.cs  file and modify it so it looks like this.




namespace HelloWorld
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
     
        [OperationContract]
        float AccountBalance(int AccountNumber);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [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; }
        }
    }
}


Then open the Service1.svc.cs file which is the code-behind for the HW.svc file. Modify it so it looks like this. And write the code as shown below



namespace HelloWorld
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {

        public float AccountBalance(int AccountNumber)
        {
            //access a secured database for this.
            if (AccountNumber == 786)
            {
                return 56789.0F;

            }
            else
            {
                return 0.0F;
            }

        }
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}


That's it. You have just created your first WCF service. To run it, right-click on the IService1.cs  and select 'Set as Start Page' and then push F5. The WCF Testclient opens up. Double click on the method you just created, enter in a value for the parameter and select the call button.Image As Shown Below

Press F5 for execute the service and you get he following screen


Second Screen Is



Check You WCF Service As shown in figure


Consuming the WCF service from a Windows Form application is just as easy as creating it. In the WCF Testclient, note the http address and port that Visual Studio is using to run you WCF webservice. It may look like this (http://localhost:4428/Service1.svc). You will need this url from within the Windows Form to add a reference to the WCF service.
Open another instance of Visual Studio 2010. Leave the instance you used to created the service open and the personal web service running. If you close it down, you will not be able to access the url above that is running the service.
In the new instance of Visual Studio 2010, select New => Project => Windows Form Application. Add a button and a label to the form. In the Solution explorer, right-click references and select Add Web Resource and enter the url we noted from the WCF Testclient, then Go. Give the service a logical namespace and select ok. In this example I named it AccountBalance.


 Add On Button and One Label on the form as shown below(in this form label is used for display the values)


Write Click on callwcfservice and Add Service Reference As Shown Below




Type The WFC Service Name and click ok as shown below fogure



Your solution explorer should look something like this.


  Lastly, add the code to the button clicked event to call the WCF service.

private void button1_Click(object sender, EventArgs e)
        {
            AccountBalance.Service1Client client =new AccountBalance.Service1Client();
            double message = client.AccountBalance(786);
            lbl_value.Text = message.ToString();

        }

Run the Form and click on Button then You will Get the output of the web service .



If you are like this article then please give me you valuable comment related to this post and mail me if you get any error .

Download sample code attached



1 comment:

  1. I am very appreciate your post of wcf. Because this very nice post for making wcf service you are provide very clear for create wcf service project. thank 's sir..................

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...