In this tutorial, I'll show you how to call a web service from the client side, such as in ASP.NET or Windows Forms Application.
To practice demo, you should create a new windows forms project, then right-click on the References->Add Service Reference
Enter your web service url, then click the Go button to finish. In the namespace textbox, you should enter a value as same as your namespace project. You can do it the similar way with ASP.NET MVC project.
Now, We will design a simple UI as shown below to call our function in the web service.
You can use the DataGridView to display the customer data returned from the GetAll method in our web service. I'm adding a databinding to the DataGridView, so you can see the column names.
Add an event handler to your buttons as below. As you can see, the CustomerServiceSoapClient class will automatically generate and you can use CustomerServiceSoapClient class to access methods in your web service.
private void btnHelloWorld_Click(object sender, EventArgs e)
{
CustomerServiceSoapClient client = new CustomerServiceSoapClient();
MessageBox.Show(client.HelloWorld(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnLoad_Click(object sender, EventArgs e)
{
CustomerServiceSoapClient client = new CustomerServiceSoapClient();
Customer[] customers = client.GetAll();
customerBindingSource.DataSource = customers;
}
Press F5 to run your project