Monday, 18 March 2013

A Simple Example of WCF Service


Introduction
Here we look at an example of a WCF Service in which we create a service for applying simple calculator functions (like add, subtract, multiply and divide).  
Step 1: First we open the Visual Studio.
  • Click on File:-> New:-> Project.
  • After that, the following window will be appear.
fi1.gif
Here we select the WCF in the Project Type  and then we select the WCF Service Library in it. After that, we specify the name of this library to be Calc (which we can define in the Name Section). And then we click on the OK Button.
Step 2: When we click on the Ok Button, the Calc.cs will be opened. In the Solution Explorer, we delete the following files, since we want to create the service from the start.
fi2.gif

Step 3: 
In Calc.cs, first we create the class public and then we create it as a WCF Data Contract. For this we first add the namespace System.Runtime.Serialization. And then we write the following code.
Code
using System.Runtime.Serialization;
namespace Calc
{
    [DataContract]
    public class Calc
{
        [DataMember]
        public double n1;
        [DataMember]
        public double n2;
}
}

Step 4: 
After that we add another class.
fig3.gif
fig14.gif

Here we name it ICalcService.cs.
Step 5: Now we declare as an interface not a class so we change the code like this.
Code
public interface ICalcService
{
}

Here we type the following operations:
public interface ICalcService
{
double Add(double n1, double n2);
double Subtract(double n1, double n2);
double Multiply(double n1, double n2);
double Divide(double n1, double n2);
}

After that, we delare it as a Service Contract, which is in a different namespace:System.ServiceModel. Now we look at the code:

using System.ServiceModel;
namespace Calc
{
    [ServiceContract]
    public interface ICalcService
{
  [OperationContract]
  double Add(double n1, double n2);
  [OperationContract]
  double Subtract(double n1, double n2);
  [OperationContract]
  double Multiply(double n1, double n2);
  [OperationContract]
  double Divide(double n1, double n2);
}
}

Step 6: 
After that we add another class: CalcService.cs. It is an actual service implementation class, so here we can specify the ICalcService like this.
Code
 
public class CalcService:ICalcService
{

}Here we implement an interface by right-clicking and choosing the option Implement Interface Explicitly:
fig5.gifNow we add the ServiceBeahvior like this:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
 
It specifies the behavior of our service and InstanceContextMode.Single means it creates a single instace of our service. Now we write the following code in it.

using System.ServiceModel;
namespace Calc
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CalcService:ICalcService
{
  public double Add(double n1, double n2)
{
  return n1 + n2;
}
public double Subtract(double n1, double n2)
{
 return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
}

Step 7: Now we try to run the program; it can not be run since it is not yet completed. Now right-click on the App.config file and select the Edit WCF Configuration option.

fig6.gif

After that the following window will be appear.
ing7.gif

After that we select the Calc.CalcService in the Configuration option:
ing8.gif

After that we click on the Name Option:
img9.gif

After that we click on EndPoints.
fig10.gif

And then we click on Empty Name.
fig11.gif

Here we click on Contart and again select the Calc.dll.
Step 8: Now we run the program.
fig12.gif

Step 9: 
After that we click on Add or any other function. When we click on Add the following window will be appear:
fig13.gif

Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:
fig4.gif
Login to add your contents and source code to this article
Article Extensions
Contents added by pola satyam on Nov 21, 2012
Introduction
Here we look at an example of a WCF Service in which we create a service for applying simple calculator functions (like add, subtract, multiply and divide).  
Step 1: First we open the Visual Studio.
  • Click on File:-> New:-> Project.
  • After that, the following window will be appear.

fi1.gif
Here we select the WCF in the Project Type  and then we select the WCF Service Library in it. After that, we specify the name of this library to be Calc (which we can define in the Name Section). And then we click on the OK Button.
Step 2: When we click on the Ok Button, the Calc.cs will be opened. In the Solution Explorer, we delete the following files, since we want to create the service from the start.
fi2.gif

Step 3: 
In Calc.cs, first we create the class public and then we create it as a WCF Data Contract. For this we first add the namespace System.Runtime.Serialization. And then we write the following code.
Code
using System.Runtime.Serialization;
namespace Calc
{
    [DataContract]
    public class Calc
{
        [DataMember]
        public double n1;
        [DataMember]
        public double n2;
}
}

Step 4: 
After that we add another class.
fig3.gif
fig14.gif

Here we name it ICalcService.cs.
Step 5: Now we declare as an interface not a class so we change the code like this.
Code
public interface ICalcService
{
}

Here we type the following operations:
public interface ICalcService
{
double Add(double n1, double n2);
double Subtract(double n1, double n2);
double Multiply(double n1, double n2);
double Divide(double n1, double n2);
}

After that, we delare it as a Service Contract, which is in a different namespace:System.ServiceModel. Now we look at the code:

using System.ServiceModel;
namespace Calc
{
    [ServiceContract]
    public interface ICalcService
{
  [OperationContract]
  double Add(double n1, double n2);
  [OperationContract]
  double Subtract(double n1, double n2);
  [OperationContract]
  double Multiply(double n1, double n2);
  [OperationContract]
  double Divide(double n1, double n2);
}
}

Step 6: 
After that we add another class: CalcService.cs. It is an actual service implementation class, so here we can specify the ICalcService like this.
Code
 
public class CalcService:ICalcService
{

}Here we implement an interface by right-clicking and choosing the option Implement Interface Explicitly:
fig5.gifNow we add the ServiceBeahvior like this:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
 
It specifies the behavior of our service and InstanceContextMode.Single means it creates a single instace of our service. Now we write the following code in it.

using System.ServiceModel;
namespace Calc
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CalcService:ICalcService
{
  public double Add(double n1, double n2)
{
  return n1 + n2;
}
public double Subtract(double n1, double n2)
{
 return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
}

Step 7: Now we try to run the program; it can not be run since it is not yet completed. Now right-click on the App.config file and select the Edit WCF Configuration option.

fig6.gif

After that the following window will be appear.
ing7.gif

After that we select the Calc.CalcService in the Configuration option:
ing8.gif

After that we click on the Name Option:
img9.gif

After that we click on EndPoints.
fig10.gif

And then we click on Empty Name.
fig11.gif

Here we click on Contart and again select the Calc.dll.
Step 8: Now we run the program.
fig12.gif

Step 9: 
After that we click on Add or any other function. When we click on Add the following window will be appear:
fig13.gif

Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:
fig4.gif

Saturday, 16 February 2013

Creating and Consuming Your First WCF Service


Introduction

I will examine how to create and consume a WCF service. WCF is a next-generation programming platform and runtime system for building, configuring and deploying service-oriented applications. For more details, please see here.

Creating a WCF Service

I will create a stock service to demonstrate a WCF service. To create a WCF service, please follow these steps:
  1. Launch Visual Studio 2008.
  2. Click on File -> new -> project, then select WCF service application.
  3. It will create a WCF service application template.
I will delete the default contract and then create an IStock contract as shown below.

Using the Code

[ServiceContract]
    public interface IStock
    {
        [OperationContract]
        Stock GetStock(string Symbol);   
    }
The above contract has one method that returns a stock object for a given symbol. Here is our Stock class that has SymbolDateCompany and Close properties respectively.
[DataContract]
    public class Stock
    {
        [DataMember]
        public string Symbol { get; set; }
        [DataMember]
        public DateTime Date { get; set; }
        [DataMember]
        public string Company { get; set; }
        [DataMember]
        public decimal Close { get; set; }
    }
Next, I will delete the default service and create a Stock service that will implement the Istock contract as shown below:
 public class Stocks : IStock
    {
        #region IStock Members
        public Stock GetStock(string Symbol)
        {
            Stock st = null;
            switch (Symbol.ToUpper())
            { 
                case "GOOG":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, 
   Company = "Google Inc.", Close = 495 };
                    break;
                case "MSFT":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, 
   Company = "Microsoft Corporation", Close = 25 };
                    break;
                case "YHOO":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, 
   Company = "Yahoo! Inc.", Close = 17 };
                    break;
                case "AMZN":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, 
   Company = "Amazon.com, Inc.", Close = 92 };
                    break; 
            }
            return st;
        }
        #endregion
    }
In the above service, I implemented IStock contract that has a GetStock method which returns stock object for a given Symbol.
Now, I will have the following endpoints in my web.config:
<service behaviorConfiguration="WcfSample.Service1Behavior" name="WcfSample.Stocks">
<endpoint address="" binding="wsHttpBinding" contract="WcfSample.IStock">
<identity>
 <dns value="localhost"/>
 </identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
In the above configuration, we have address="" which is localhost, binding="wsHttpBinding" andcontract="WcfSample.IStock".
Now I will compile the service and build a client to consume the service.

Creating a Client to Consume Service

To create a client, I will create a web application. Please follow these steps.
  1. Right Click on Solution -> Add -> new project, then select ASP.NET web application.
  2. It will create a web application template.
  3. Now, I will add the service reference. To add a service reference, select client application, then add a service reference. Since our client is in a same solution, I will click discover and service in the solution as shown below:
  4. In default.aspx, I will create a simple UI, a textbox to enter the stock symbol and a button to call the service to get stock information. Here is our code behind:

    ServiceReference2.StockClient sc = new ServiceReference2.StockClient();
    ServiceReference2.Stock st = sc.GetStock(TextBox1.Text.Trim());
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("<B>Company:</B> {0}<br />", st.Company);
    sb.AppendFormat("<B>Date: </B>{0}<br />", st.Date);
    sb.AppendFormat("<B>Close: </B>{0}<br />", st.Close);
    sb.AppendFormat("<B>Symbol: </B>{0}<br />", st.Symbol); 
    Label1.Text = sb.ToString(); 
  5. Here are a few screenshots from our final application:

Tuesday, 29 January 2013

Building a Setup for a Windows Forms Application

Building a Setup for a Windows Forms Application

 

This step by step tutorial guides you towards how to create a setp for a Windows Forms application using Visual Studio 2005.

Step 1: First of all, create a Windows Forms application. 

Step 2: Next add a setup project by clicking on File-> Add-> New Project.

By clicking here the window will open like this:



Figure 1:

Step 3: Select here Setup and Deployment in the left side bar in project types.



Figure 2:

Here we find three options as following:
  1. Application Folder
  2. User's Desktop
  3. User's Program Menu
Step 4: These three options are the three places where we want to copy our setup fules during the installation process. From here we have to select one option. Suppose I select User's Desktop. Then Right click on User's Desktop option and select Add-> Project Output. The window will look like as.



Figure 3:

Step 5: After clicking on Project Output, a new window will open. Here we select first four options. Now click on OK.



Figure 4:

Step 6: After clicking on OK, Rebuild the Setup from Build option in Menu bar like as.



Figure 5:

Step 7: After this, let's test our setup by going right clicing on the setup project in Solution Explorer and click on Install menu item. 



Figure 6:

The setup process will start, which looks like this: 



Figure 7:

Step 8: After copmpleting the installation go on desktop there you will find all files you have included in the setup on the desktop.