How to Consume BasicHttpBinding in Windows Application

This is the second part of the WCF tutorial.

If you want to understand how to create a basic HTTP Binding in WCF then just see How to Create Basic HTTP Binding in WCF.

As you have gone through my first article where we just created a WCF BasicHttpBinding service and tested on wcftestclient, now it's time to consume the service. This service is in a Windows application.

Since this is a basic way, as you know you can explore the basics in depth.

Let's start with the creation of a Windows application.

  • Create a Windows application project.

  • Start Visual Studio and select New project from the Start page or from the File menu, select New and then Project.

  • After selecting, a dialog will pop up showing all the Templates provided by Visual Studio.

  • From the Installed Templates select C# and inside that select Windows and inside that you will select Windows Forms Application.

  • Now we need to name this project. I am naming it WCF_TUTO_Pxpress2 . And click OK.

    windows form application

  • After adding the project you will just find it like in this view of the solution.

    view of your solution

  • Now the first thing to do is to Add a service reference to the Windows application then we will design a Windows Forms form for taking the input and calling this service.

  • For adding a reference to the Windows application just right-click on the References folder and select Add Service Reference.

    Add service reference

  • After clicking on the Add Service Reference a new dialog will pop up for asking Address.

    add service

  • I will just add the URL of the service that we created in the first Tutorial of WCF.

    created in first Tutorial of WCF

  • And if there is any problem when adding the URL or if you get an error like in the following snapshot then just run the application that is exposing the services.

    This occurs when the service is not running or hosted.

    address

  • After this just add the service URL: http://localhost:2226/Service1.svc and click on the GO button.

    If you want to change the namespace then you can change it.

    Namespace

  • After adding the Address and Namespace just click the OK button.

    Your solution will be like in this view after adding the Service reference to it.

    solution after adding Service reference

  • Now I will design a window for taking the input from the form and passing it to the service.

    We require 2 parameters as input to this service.

    parameters as input to this service

    Here I have the following design windows.

    1. 2 TextBoxes [ Mobiles no , Pizza code ]
    2. Button [Book order ]

      design windows

  • Now on the click of Bookorder button we need to call the service.

    Bookorder

    Code snippet
    1. string message = string.Empty; //empty string  
    2.   
    3. //Creating object service we have added   
    4. PizzaserviceRef.Service1Client objps = new PizzaserviceRef.Service1Client();   
    5. //getting output by passing parameters as input to this service.  
    6. message = objps.OrderPizza_by_Mobileno(Txtmobileno.Text, txtpizzacode.Text);  
    7. // displaying message  
    8. MessageBox.Show(message);  
  • After completing the coding now we just run the application and pass input to run the service.

     run service
Final output

Final output

We have completed the consumption of the WCF BasicHttpBinding service in a Windows application.