Silverlight 컨트롤은 클라이언트 측 컨트롤이므로 직접 세션 변수에 액세스 할 수 없습니다. 그러나 Silverlight에서 세션을 관리하기 위해 WCF 서비스를 호출 할 수 있습니다 .
우리는 다음과 같이 wcf 서비스에서 세션 변수를 설정해야합니다.
<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements
(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class PersonService
<OperationContract()> _
Public Sub DoWork()
' Add your operation implementation here
End Sub
' Add more operations here and mark them with <OperationContract()>
<OperationContract()> _
Public Sub SetSessionVariable(ByVal Sessionkey As String)
System.Web.HttpContext.Current.Session("Key") = Sessionkey
System.Web.HttpContext.Current.Session.Timeout = 20
End Sub
<OperationContract()> _
Public Function GetSessionVariable() As String
Return System.Web.HttpContext.Current.Session("Key")
End Function
End Class
실버 라이트 애플리케이션에 서비스를 참조하면 .xaml 페이지에서 세션 변수를 다음과 같이 설정할 수 있습니다.
Dim client As Service.PersonServiceClient = New Service.PersonServiceClient()
'Calls the SetSessionVariable() and store values in the session.
client.SetSessionVariableAsync("Soumya")
We will get the session variable in the .xaml page by calling GetSessionVariable() where we want to check the session
Dim client As Service.PersonServiceClient = New Service.PersonServiceClient()
AddHandler client.GetSessionVariableCompleted, AddressOf client_GetSessionVariableCompleted
client.GetSessionVariableAsync()
Private Sub client_GetSessionVariableCompleted(ByVal sender As Object, ByVal e As GetSessionVariableCompletedEventArgs)
Try
If Not String.IsNullOrEmpty(e.Result) Then
MessageBox.Show(e.Result)
Else
MessageBox.Show("Your session has been expired")
End If
Catch ex As FaultException
End Try
End Sub