Monday, May 24, 2010

Windows Communication Foundation (WCF) - A sample application

Hello all,
We all know that the Windows Communication Foundation (WCF) is the latest technology for communication between application, just like the web service but there are some more powerful features also in the WCF. We all know that WCF is very common now a days so for here i am going discuss that how can we create a sample web service in which we can interact with the data base also.
In this WCF sample i am going to check that weather a given user name is invalid or not.We can furture implement the service for some more use..

1. So for the first step start the Visual Studio Select New Website and choose a WCF service like



Give the Site name as "CheckUser_WCF".

2. In the Next Step you will be directed to the Following page "APP_CODE/Service.cs". In that page create a function which will read the table from data base for that create a simple data base with a name "UserData" and in that create a table as "UserInfo". Insert some sample records also the table will be like..


3. In the Next Step add a function decalrtion in the service interface name "IService.cs" which you can find inside the "App_Code" folder. Simply add the following lines there.

[OperationContract]
Int32 GetUserId(string strUserName);

here [OperationContract] denotes that this function is going to perform action in the service which will intact with the user. We must declare a function as "[OperationContract]" if we are assigning it some functionality..

Now the page will be like this.


4. Now in the next step create the "GetUserId" function in the service code file which will accept the user name as input. The User name will be entered by the user in our testUser.aspx page

public Int32 GetUserId(string strUserName)
{
SqlConnection con
= new SqlConnection("Data Source = GP-000\\SQLEXPRESS; Initial Catalog = UserData; User ID = sa; Password = test;");
string cmd = "select UserID from UserInfo where UserName = '" + strUserName + "' ";
SqlDataAdapter da
= new SqlDataAdapter(cmd, con);
con
.Open();
DataSet ds
= new DataSet();
da
.Fill(ds);
con
.Close();

if(( ds.Tables[0].Rows.Count>0) && Convert.ToString(ds.Tables[0].Rows[0][0].ToString()) != "")
{
return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
}
else
{
return 0;
}}

now the page will look like this..



Now your Service is ready you can build the application and can run it. After running it you will get the following output.


5. In the next step you use the URL for the service which is appearing in the Browser as "http://localhost:4771/CheckUser_WCF/Service.svc" for any other application where you want to use that or else you can also use it in the same application. For that simply Right click on the solution and Select the "Add Service Reference" option, a new window will be opened and there you can pass the URL of the service "http://localhost:4771/CheckUser_WCF/Service.svc" in the place of address and simply click go the window will discover you service assign it a name as "UserService" The dialog box will be like this


Click on ok and the reference will be added in the application which you can see in the Solution explorer.. In this sample I am going to use it in same application only for that i am going to create a sample test page.

* Add a new ASPX page in the application give it the name as "testUser.aspx" It will look like this..


6. In the Code behind page simply create the button click functionality as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class testUser : System.Web.UI.Page
{
UserService
.ServiceClient wcfService = new UserService.ServiceClient();

protected void Page_Load(object sender, EventArgs e)
{
lblMsg
.Text = "";
}


protected void CheckUser_Click(object sender, EventArgs e)
{
string strName = "";
int intResult = 0;
strName
= textUsername.Text.Trim();

if (strName != "")
{

intResult
= wcfService.GetUserId(strName);
}

if (intResult > 0)
{
lblMsg
.Text = "User is valid";
lblMsg
.ForeColor = System.Drawing.Color.Green;
}
else
{
lblMsg
.Text = "User is invalid";
lblMsg
.ForeColor = System.Drawing.Color.Red;

}
}
}

For creation the object of the service simply use the service name and create like this..

UserService.ServiceClient wcfService = new UserService.ServiceClient();

with the help of this object we can call all the function inside the service which are available for us..

Here now application is ready which will check that weather the specified user name is present in the data base or not.. Run the application and you will get the out put will be like this if a user is INVALID


and this if the user is a valid user




This is piety simple i hope you have like this article.. Please feel free to put your comment..

Thanks
Anil Kumar Pandey
Micorsoft MVP (C#)
Navi Mumbai,
Maharashtra


Kontera