Passing ASP.NET Session Variables to WCF Service
Posted by: Pravinkumar Dabade
in Category ASP.NET
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 –
Now modify the Interface ‘Iservice1’ as shown below –
First import a name – ‘using System.Data;’
Now implement this interface in our Service class ‘Service1’ as shown below. First import the following Data namespaces –
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 –
Now let’s add a App.Config file in our Console application with the configuration code as shown below –
Run your WCF host to test the service. You should get the following message-
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 –
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.
Make sure you add a reference of ‘System.ServiceModel.dll’ and import the namespace –
Let’s write the following code on our both the button click event as shown below –
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 –
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
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!