Silverlightコントロールは、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
サービスをsilverlightアプリケーションで参照すると、.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