Session Management Using WCF - Silverlight

Posted on 2011-10-02
Last Modified: 2012-06-27

I want to handle Session Management using WCF in Silverlight. Here is the sample code:


Imports System.ServiceModel

Public Interface IService1

    Sub SetSessionVariable(ByVal SessionKey As String, ByVal SessionValue As String)

    Function GetSessionVariable(ByVal SessionKey As String) As String

End Interface


Imports System.ServiceModel.Activation

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class Service1
    Implements IService1

    Public Sub SetSessionVariable(ByVal SessionKey As String, ByVal SessionValue As String) Implements IService1.SetSessionVariable
        System.Web.HttpContext.Current.Session(SessionKey) = SessionValue
        System.Web.HttpContext.Current.Session.Timeout = 20
    End Sub

    Public Function GetSessionVariable(ByVal SessionKey As String) As String Implements IService1.GetSessionVariable
        Return System.Web.HttpContext.Current.Session(SessionKey)
    End Function

End Class


Private Sub MainPage_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim svc As New ServiceReference1.Service1Client()
        svc.SetSessionVariableAsync("Key", "Hello World !")
End Sub


I am getting "Object reference not set to an instance of an object." at:

System.Web.HttpContext.Current.Session(SessionKey) = SessionValue

Can you please change my code and make it working ? My WCF Service is in .WEB project. I am accessing that by using proxy in Silverlight project.


Question by:milani_lucie
    LVL 8

    Accepted Solution

    You should be using a binding that support sessions, and enable PerSession service instances.
    LVL 83

    Assisted Solution


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now