WCF Application and Instance Management by using 3 different application domains (Asp.net, C # Windows Application and WPF application as clients)

In this article we are going to see Instance management provided by WCF and how we can use single WCF service in three different clients- ASP.net, C sharp Windows application and WPF application.

WCF provides three different instance management techniques per call, Per Session and Single.

Part 1-Creating Database and C# windows application client.

We have designed a small application to demonstrate the whole scenario. In this application, we have a database table with some records and we will be accessing the data at the client end through WCF service and have used WCF instance management to understand what impact they have.

So we will have to create a table in database, and populated it with some records, below is the code for creating table.

CREATE TABLE [dbo].[blog_PersonalInfo](

    [FirstName] [nvarchar](50) NOT NULL,

    [LastName] [nvarchar](50) NOT NULL,

    [Country] [nvarchar](50) NOT NULL,

    [City] [nvarchar](max) NULL,

    [ID] [int] IDENTITY(1,1) NOT NULL,

CONSTRAINT [PK_blog_PersonalInfo] PRIMARY KEY CLUSTERED

(

    [ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

 

Stored procedure is as follows

Create PROCEDURE [dbo].[usp_blog_PersonalInfo]

(

@Name nvarchar(100),

@Id int=0,

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

 

 

select * from blog_PersonalInfo where firstname like @Name +'%' or ID=@Id

--select 'successfull'

return 0

END try

begin catch

select @msg='Error!!!Problem Occured During Process'

return 1

end catch

 

Now we will start making our WCF service and this service will be consumed by our three clients ASP.net,C sharp Windows application and WPF .

In order to make our WCF service we will have to select WCF service from templates present in Visual Studio( I have already discussed how to select WCF service from templates in my previous article, below is the link).

http://devtechie.com/post/2012/01/28/Simple-Application-Using-WCF-and-Database.aspx

Once we have selected WCF service we will be provided with two files in App_Code Directory

Iservice.cs file is where we are going to define our interface and methods which they contain and Service.cs is where we are going to implement our methods which are in our interface.

Code for Iservice.cs file

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data;

 

// NOTE: If you chan[ServiceContract]

[ServiceContract]

public interface IService

{

 

[OperationContract]

string GetData(int value);

 

[OperationContract]

DataTable GetPersonalInfo(PersonalInfo Pi);

 

[OperationContract]

int ReturnVal();

 

 

    // TODO: Add your service operations here

}

 

// Use a data contract as illustrated in the sample below to add composite types to service operations.

[DataContract]

public class PersonalInfo

{

string name1;

[DataMember]

public string p_name

{

get{return name1;}

set{name1=value;}

}

string Country1;

[DataMember]

public string p_country

{

get{return Country1;}

set{Country1=value;}

}

int id;

[DataMember]

public int p_ID

{

get { return id; }

set { id = value; }

}

int objVal=0;

[DataMember]

public int p_objVal

{

get { return objVal; }

set { objVal = value; }

}

}

 

In above code we have int objVal variable in class PersonalInfo and we are using this variable so that we can count how many times the method is being called also, we have defined int ReturnVal() method which will increment the value of objVal according to the instance management technique.In other words objVal will count the service object created by the different instance management technique used in WCF Service.

 

 

Code for Service.cs file

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.

 

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]

public class Service : IService

{

PersonalInfo pi = new PersonalInfo();

 

    public string GetData(int value)

    {

        return string.Format("You entered: {0}", value);

    }

 

public int ReturnVal()

{

pi.p_objVal = pi.p_objVal + 1;

return pi.p_objVal;

}

 

public DataTable GetPersonalInfo(PersonalInfo pi)

{

 

string constr = ConfigurationManager.ConnectionStrings["blogDevtechie"].ToString();

SqlConnection con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

 

SqlCommand cmd = new SqlCommand("usp_blog_PersonalInfo", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = pi.p_name;// name;

cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = pi.p_ID;// name;

 

//***************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//**************************************************************

SqlDataAdapter adpt = new SqlDataAdapter(cmd);

DataTable dt = new DataTable("PersonalInfo");

adpt.Fill(dt);

 

return dt;

 

 

}

 

}

In the above code

public int ReturnVal() method will count and return objVal.

ie. How many service object have been created.

 

public DataTable GetPersonalInfo(PersonalInfo pi) will return data from database table.

 

Now we will design our 3 clients :

C sharp windows Application

In order to design C sharp windows Application we need select "Windows Forms Application" from templates.

Code for Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1___Client

{

public partial class Form1 : Form

{

ServiceReference1.IService PersonalInfo = new ServiceReference1.ServiceClient();

ServiceReference1.PersonalInfo Pi = new ServiceReference1.PersonalInfo();

DataTable dt = new DataTable();

public Form1()

{

InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

dt = new DataTable();

Pi.p_name = "Ankit";

dt = PersonalInfo.GetPersonalInfo(Pi);

 

dataGridView1.DataSource = dt;

textBox2.Text = Convert.ToString(PersonalInfo.ReturnVal());

 

}

 

private void button1_Click(object sender, EventArgs e)

{

dt = new DataTable();

Pi.p_name = textBox1.Text;

dt = PersonalInfo.GetPersonalInfo(Pi);

 

dataGridView1.DataSource = dt;

textBox2.Text = Convert.ToString(PersonalInfo.ReturnVal());

 

}

}

}

 

Below is the Form Design for C sharp windows Application

 

Tags:

ASP.NET | WCF | Windows Form