Passing ASP.NET Session Variables to WCF Service

Posted by: Pravinkumar Dabade , on 6/5/2011, in Category ASP.NET
Views: 81328
Abstract: In this article, we will see how to pass ASP.NET Session varaibles to WCF service using SOAP Message Header.

There are multiple ways of passing the Session variables to WCF service. We will pass the session variables to WCF service using SOAP Message header. So let’s open Visual Studio 2010 and create a Blank solution with the name ‘SessionStateSharingExample’. Once the solution is created, add a WCF Serivce library with the name ‘TestWCFService’ as shown below –

 

clip_image002

Now modify the Interface ‘Iservice1’ as shown below –

First import a name – ‘using System.Data;’

Iservice Interface

Now implement this interface in our Service class ‘Service1’ as shown below. First import the following Data namespaces  –

SQL Connection

Note: Make sure you change the connection string (Server Name, User ID and Password) according to your settings.

Now our service is ready to be accept requests. Let’s host this service in our .NET Console Application. Add a Console Application in our Solution with the name ‘WCFHost’. Now add a reference to the ‘System.ServiceModel.dll’ file. Also add a reference to our WCF Service. Then add the following code in the Main Method –

WCF Host Service

Now let’s add a App.Config file in our Console application with the configuration code as shown below –

session-share-wcf

Run your WCF host to test the service. You should get the following message-

clip_image002[6]

Now add a ASP.NET website in our solution with the name ‘TestWebSite’. Once your site is ready, add two buttons and a ‘GridView’control in ‘Default.aspx’ as shown below –

aspnet-grid-session-wcf

Once our design is ready, let’s add an interface which will act like a proxy and give a call to our WCF service by passing a database name, which in turn returns the data table Customers from the respective database.

clip_image002[8]

Make sure you add a reference of ‘System.ServiceModel.dll’ and import the namespace –

System.ServiceModel.dll

Let’s write the following code on our both the button click event as shown below –

aspnet-wcf-session-sharing

Now run the service and then your ASP.NET web site. Click the button ‘Adventure Works Database’ and then click the button ‘Northwind Database’ and you should see the output like below –

ASP.NET WCF Service Session Share

Summary – In this article we have see how easy it is to pass the ASP.NET Session Variables to WCF services using SOAP Message Header.

The entire source code of this article can be downloaded over here

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar[attherate]gmail[dot]com


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Daniel Fisher on Monday, June 6, 2011 9:20 AM
Session in ASP.NET MVC - just DON'T
Session in WCF - just DON'T
Comment posted by Tintu on Wednesday, July 20, 2011 3:28 PM
Its nice .we can refer any dbt regardng asp.net...thankz a lott..
Comment posted by Pravesh Singh on Friday, January 6, 2012 4:55 AM
This is best article so far I have read online. I would like to appreciate you for making it very simple and easy.  I have found another nice post related to this post over the internet which also explained very well. Here I am sharing that post url it might also be useful for you.

http://www.mindstick.com/Articles/701259d3-ec8d-4881-8b18-b65923e9f59c/?Session%20in%20ASP.Net

Thanks everyone for your precious post.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel