Lets consider a simple calculation program that does calculation like below.

Easiest way to calculate is to do it on click event of Calculate Button like below
private void CalculateButton_Click(object sender, EventArgs e)
{
if (IsNumeric(FirstTextBox.Text) && IsNumeric(SecondTextBox.Text))
{
switch (OperatorCombobox.SelectedItem.ToString())
{
case “+”:
ResultLabel.Text = (double.Parse(FirstTextBox.Text.ToString()) + double.Parse(SecondTextBox.Text.ToString())).ToString();
break;
case “-”:
ResultLabel.Text = (double.Parse(FirstTextBox.Text.ToString()) – double.Parse(SecondTextBox.Text.ToString())).ToString();
break;
case “*”:
ResultLabel.Text = (double.Parse(FirstTextBox.Text.ToString()) * double.Parse(SecondTextBox.Text.ToString())).ToString();
break;
case “/”:
if (double.Parse(SecondTextBox.Text.ToString()) != 0.0)
ResultLabel.Text = (double.Parse(FirstTextBox.Text.ToString()) / double.Parse(SecondTextBox.Text.ToString())).ToString();
else
ResultLabel.Text = “Division by zero!”;
break;
default:
ResultLabel.Text = (double.Parse(FirstTextBox.Text.ToString()) + double.Parse(SecondTextBox.Text.ToString())).ToString();
break;
}
}
else
{
ResultLabel.Text = “Please enter numbers!”;
}
}
public static System.Boolean IsNumeric(System.Object Expression)
{
if (Expression == null || Expression is DateTime)
return false;
if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
return true;
try
{
if (Expression is string)
Double.Parse(Expression as string);
else
Double.Parse(Expression.ToString());
return true;
}
catch { } // just dismiss errors but return false
return false;
}
Now consider a scenario where this same function is supposed to be used in many places in a same application under different scenarios. In this scenario, it would be advised to create a class library and create a .dll (Dynamic Link Library) out of it and add it as a reference to it in the application. Sample class file will be as below
using System;
namespace CalculateLibrary
{
public class Calculate
{
public string Calculate2numbers(string FirstNumber, string SecondNumber, string OperatorString)
{
….
}
private static System.Boolean IsNumeric(System.Object Expression)
{
…
}
}
}
And the button click event will look like below
private void CalculateButton_Click(object sender, System.EventArgs e)
{
CalculateLibrary.Calculate CalculateObject = new CalculateLibrary.Calculate();
ResultLabel.Text = CalculateObject.Calculate2numbers(FirstTextBox.Text, SecondTextBox.Text, OperatorCombobox.SelectedItem.ToString());
}
Now, consider a scenario where many windows/web applications will be accessing this same calculate class and it has to be accessed from a remote website. Assume this calculate function is one of the most difficult one and you want to keep the implementation hidden, and want others to use it over web. In this scenario, one would create a web service to make it accessible over a url. Simple web service code would look like below. Each method that you want to expose will have [WebMethod] attribute over its declaration. Other functions that need not be publicaly available will not have the [WebMethod] attribute over it
using System;
using System.Web.Services;
namespace CalculateWebService
{
/// <summary>
/// Summary description for CalculateService
/// </summary>
[WebService(Namespace = http://tempuri.org/)]http://tempuri.org/&)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CalculateService : System.Web.Services.WebService
{
[WebMethod]
public string Calculate2numbers(string FirstNumber, string SecondNumber, string OperatorString)
{
…
}
private static System.Boolean IsNumeric(System.Object Expression)
{
…
}
}
}
User has to add web reference/service reference depending on version of .Net you are using, and the button click event will look like below
private void CalculateButton_Click(object sender, System.EventArgs e)
{
CalculateService.CalculateServiceSoapClient CalculateServiceClient = new CalculateService.CalculateServiceSoapClient();
ResultLabel.Text = CalculateServiceClient.Calculate2numbers(FirstTextBox.Text, SecondTextBox.Text, OperatorCombobox.SelectedItem.ToString());
}
Now consider a scenario where web service has to be made more secure and reliable, accessible faster in local intranet or accessible in local system via named pipes but running on different thread, WCF [Windows Communication Foundation] Services is the way to go. For WCF service, one needs to have Service Contract and Data Contract where Service Contract is an interface and Data Contract is the implementation. When you create a WCF service with name CalculateService, it will create ICalculateService.cs and CalculateService.svc files. ICalculateService.cs will look like below
using System.ServiceModel;
namespace CalculateWCFService
{
[ServiceContract]
public interface ICalculateService
{
[OperationContract]
string Calculate2numbers(string FirstNumber, string SecondNumber, string OperatorString);
}
}
And CalculateService.svc will look like below
using System;
namespace CalculateWCFService
{
public class CalculateService : ICalculateService
{
public string Calculate2numbers(string FirstNumber, string SecondNumber, string OperatorString)
{
…
}
private static System.Boolean IsNumeric(System.Object Expression)
{
…
}
}
}
One has to add Service Reference to the WCF service. This assumes one is building apps in .Net 3.5 or .Net 4.0. The button click event will look like below
private void CalculateButton_Click(object sender, System.EventArgs e)
{
CalculateServiceReference.CalculateServiceClient CalculateServiceClientObject = new CalculateServiceReference.CalculateServiceClient();
ResultLabel.Text = CalculateServiceClientObject.Calculate2numbers(FirstTextBox.Text, SecondTextBox.Text, OperatorCombobox.SelectedItem.ToString());
}
While adding reference to the WCF service, in the advanced settings, if the user selects for Asynchronous Methods, the button click events will look like below. With Async methods, the UI will be more resonsive while the actual work happens on different thread.
private void CalculateAsyncButton_Click(object sender, System.EventArgs e)
{
CalculateServiceReference.CalculateServiceClient CalculateServiceClientObject = new CalculateServiceReference.CalculateServiceClient();
CalculateServiceClientObject.Calculate2numbersCompleted += new System.EventHandler<CalculateServiceReference.Calculate2numbersCompletedEventArgs>(CalculateServiceClientObject_Calculate2numbersCompleted);
CalculateServiceClientObject.Calculate2numbersAsync(FirstTextBox.Text, SecondTextBox.Text, OperatorCombobox.SelectedItem.ToString());
ResultLabel.Text = “Calculating asynchronously”;
}
void CalculateServiceClientObject_Calculate2numbersCompleted(object sender, CalculateServiceReference.Calculate2numbersCompletedEventArgs e)
{
ResultLabel.Text = e.Result;
}
Sample code is available below