Part 3 Using ASP NET Session State in a Web Service
#############################
Video Source: www.youtube.com/watch?v=gq1Yt11brps
Link for code samples used in the demo • http://csharp-video-tutorials.blogspo... • Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. • / @aarvikitchen5572 • Link for all dot net and sql server video tutorial playlists • / kudvenkat • In this video, we will discuss using asp.net session variables in a web service. This is continuation to Part 2. Please watch Part 2 from the ASP.NET tutorial before proceeding. • To use asp.net Session object in a web service, the web service class must inherit from System.Web.Services.WebService class and EnableSession property of WebMethod attribute must be set to true. • Let us now use Session object with the Calculator Service that we have built in Part 2. We want to display all the calculations that a user has performed as shown below. Here are the steps to achieve this. • Step 1: Copy and paste the following code in CalculatorWebServices.asmx file • using System.Web.Services; • using System.Collections.Generic; • namespace WebServicesDemo • { • [WebService(Namespace = http://pragimtech.com/webservices )] • [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] • [System.ComponentModel.ToolboxItem(false)] • public class CalculatorWebServices : System.Web.Services.WebService • { • [WebMethod(EnableSession = true)] • public int Add(int firstNumber, int secondNumber) • { • List[string] calculations; • if (Session[ CALCULATIONS ] == null) • { • calculations = new List[string](); • } • else • { • calculations = (List[string])Session[ CALCULATIONS ]; • } • • string strTransaction = firstNumber.ToString() + + • secondNumber.ToString() • = + (firstNumber + secondNumber).ToString(); • calculations.Add(strTransaction); • Session[ CALCULATIONS ] = calculations; • return firstNumber + secondNumber; • } • [WebMethod(EnableSession = true)] • public List[string] GetCalculations() • { • if (Session[ CALCULATIONS ] == null) • { • List[string] calculations = new List[string](); • calculations.Add( You have not performed any calculations ); • return calculations; • } • else • { • return (List[string])Session[ CALCULATIONS ]; • } • } • } • } • Step 2: The next step is to update the proxy class in the client application, that is in the CalculatorWebApplication. To achieve this, right click on CalculatorService in Service References folder and select Update Service Reference option. • Step 3: Copy and paste the HTML from by blog, as youtube doesnot allow HTML to be pasted in the description. • Step 4: Copy and paste the following code in WebForm1.aspx.cs • using System; • namespace CalculatorWebApplication • { • public partial class WebForm1 : System.Web.UI.Page • { • protected void Page_Load(object sender, EventArgs e) • { • } • protected void btnAdd_Click(object sender, EventArgs e) • { • CalculatorService.CalculatorWebServicesSoapClient client = • new CalculatorService.CalculatorWebServicesSoapClient(); • int result = client.Add(Convert.ToInt32(txtFirstNumber.Text), • Convert.ToInt32(txtSecondNumber.Text)); • lblResult.Text = result.ToString(); • gvCalculations.DataSource = client.GetCalculations(); • gvCalculations.DataBind(); • gvCalculations.HeaderRow.Cells[0].Text = Recent Calculations ; • } • } • } • Step 5: In web.config file of CalculatorWebApplication, set allowCookies attribute to true. • When allowCookies attribute is set to true, the client application accepts the cookie returned from the ASMX web service, and copies it into all future requests that are made to the web service. This ensures that the same session is maintained between the client and the web service.
#############################