Home > Software Development > Clearing Session Part 2

Clearing Session Part 2

Web.config  (Relevant portion of the web.config file.  This should get created automatically if you choose to add an ajax enabled WCF to your web site.)

<system.serviceModel>

<

behaviors>

<

endpointBehaviors>

<behavior name="SessionMethodsAspNetAjaxBehavior">

<

enableWebScript/>

<

webHttp></webHttp>

</

behavior>

</

endpointBehaviors>

</

behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

<

services>

<

service name="SessionMethods">

<endpoint address="" behaviorConfiguration="SessionMethodsAspNetAjaxBehavior" binding="webHttpBinding" contract="SessionMethods"/>

</

service>

</

services>

</

system.serviceModel>

</

configuration>

 

 

Service file (SessionMethods.svc)

<%

@ ServiceHost Language="C#" Debug="true" Service="SessionMethods" CodeBehind="~/App_Code/SessionMethods.cs" %>

Service Code Behind (SessionMethods.cs)

using

System;

using

System.Linq;

using

System.Runtime.Serialization;

using

System.ServiceModel;

using

System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Web;

[

ServiceContract(Namespace = "", SessionMode = SessionMode.Allowed)]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

public

class SessionMethods

{

// Add [WebGet] attribute to use HTTP GET

[OperationContract()]

public void ClearSession()

{

HttpContext.Current.Session.Clear();

}

}

I have an additional page so the user can go back and forth between the default.aspx page and this OtherPage.aspx and watch what happens to the Session Variable depending on whether the checkbox is checked or not.

<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="OtherPage.aspx.cs" Inherits="OtherPage" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<

head runat="server">

<title></title>

<script type="text/javascript">

function pageLoad() {

}

</script>

</

head>

<body>

<form id="form1" runat="server">

<div>

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Back to Default to check Session State</asp:HyperLink>

</div>

</form>

</

body>

</

html>

 

I apologize, but Live doesn’t appear to have an elegant way to post code.  I’m also working on a place to post the full sample code.

 

Advertisements
Categories: Software Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: