Before reading this blog, I would recommend you to read the previous parts of this series.
Let's start now.
STEP 1
Create a new ASP.Net Web Site (say WebServiceExample)
STEP 2
Right-click on Website folder (WebServiceExample) > Add > Service Reference.
The following window will open up.
Add the URL of your web service in the “Address” textbox and hit “Go”. It will show you all the methods in the web service that you have created.
Also, at the bottom, there is an option to add “Namespace”. You can specify any relevant name here. (say ServiceReference1) and Click “OK”.
STEP 3
When you add the reference to the service, the following code gets generated in the web.config file.
BINDINGS
- Bindings are required to communicate with the web service.
- Bindings define where to connect the web service to an endpoint location for execution.
- Bindings basically are the intermediate between the client and web service to communicate.
- By default the web services uses basicHttpBinding.
System-Provided Bindings
WCF includes a set of system-provided bindings that are designed to cover most application requirements and scenarios. The following classes represent some examples of system-provided bindings,
- BasicHttpBinding
An HTTP protocol binding suitable for connecting to Web services that conforms to the WS-I Basic Profile 1.1 specification (for example, ASP.NET Web services [ASMX]-based services).
- WSHttpBinding
An HTTP protocol binding suitable for connecting to endpoints that conform to the Web service specifications protocols.
- NetNamedPipeBinding
Uses the .NET binary encoding and framing technologies in conjunction with the Windows named pipe transport to connect to other WCF endpoints on the same machine.
- NetMsmqBinding
Uses the .NET binary encoding and framing technologies in conjunction with the Message Queuing (also known as MSMQ) to create queued message connections with other WCF endpoints..
STEP 4
Add a new web form (say calculations.aspx) and Add UI as per your requirements.
Step 5
Copy and paste the following code in the code-behind file
- protected void btnAddTwoNumbers_Click(object sender, EventArgs e) {
- ServiceReference1.MyWebServiceSoapClient client = new ServiceReference1.MyWebServiceSoapClient();
- lblResult1.Text = String.Format("Answer: {0} + {1} = {2}", txtfirstNumber1.Text, txtsecondNumber1.Text, client.AddTwoNumbers(Convert.ToDouble(txtfirstNumber1.Text), Convert.ToDouble(txtsecondNumber1.Text)));
- }
- protected void btnAddThreeNumbers_Click(object sender, EventArgs e) {
- ServiceReference1.MyWebServiceSoapClient client = new ServiceReference1.MyWebServiceSoapClient();
- lblResult2.Text = String.Format("Answer: {0} + {1} + {2} = {3}", txtfirstNumber.Text, txtsecondNumber.Text, txtthirdNumber.Text, client.AddThreeNumbers(Convert.ToDouble(txtfirstNumber.Text), Convert.ToDouble(txtsecondNumber.Text), Convert.ToDouble(txtthirdNumber.Text)));
- }
Step 6
Run the website and click on both buttons. The Output will be as shown below.