Archive

Archive for February, 2009

Clearing Session Part 2

February 28, 2009 Leave a comment

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.

 

Categories: Software Development

Clearing Session on window close Part 1

February 28, 2009 Leave a comment
Dealing with an app that needs to store quite a bit of data and Session as been chosen throughout the app.  However, this makes me nervous as the traffic against the application grows.  For the pages I’m writing, I’ve been searching for a way to clear the session variables when a use goes to another page or closes IE.
 
I finally got what I was looking for using ajax WCF service in my application.
 
My default.aspx page is as follows:
 

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

<!

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>

</

head>

<

body>

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

<script type="text/javascript">

function window.onunload() {

var checked = $get(‘<%= CheckBox1.ClientID %>’).checked;

// Use window.event.clientY values to determine when you want to clear session

//alert(window.event.clientY);

if (checked) {

SessionMethods.ClearSession(ClearSuccess, ClearFail);

}

}

// These won’t fire because the window is gone

function ClearSuccess(){

alert(

‘ClearSuccess’);

}

function ClearFail(){

alert(

‘ClearFail’);

}

</script>

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

<Services>

<asp:ServiceReference Path="~/SessionMethods.svc" />

</Services>

</asp:ScriptManager>

<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<div>

<asp:Label ID="Label2" runat="server" Text="Session DateTime:"></asp:Label>

</div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<div>

<asp:CheckBox ID="CheckBox1" runat="server" Text="Clear Session on window.unload? Checked, Session state is cleared when you leave the page. Unchecked Session is left in tact."></asp:CheckBox>

</div>

<div>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</div>

</ContentTemplate>

</asp:UpdatePanel>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/OtherPage.aspx">HyperLink</asp:HyperLink>

</div>

</form>

</

body>

</

html>

 

The code behind for this page page is.

using

System;

using System.Collections.Generic;

using

System.Linq;

using

System.Web;

using

System.Web.UI;

using System.Web.UI.WebControls;

public

partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = Session[

"SessionTest"] as string;

}

protected void Button1_Click(object sender, EventArgs e)

{

Session[

"SessionTest"] = DateTime.Now.ToString();

Label1.Text = Session["SessionTest"] as string;

}

}

 

Categories: Software Development

WCF, Linq To Sql and SQL Image Type

February 4, 2009 Leave a comment
Today I’m working on an endpoint for a Northwind demo.  The endpoint is UpdateEmployee and the problem I’m running into is that the Employee Table in Northwind has a field called Photo defined as an Image.
 
Since the constructor for System.Data.Linq.Binary can take a Byte[], I created my data contract as follows.
 

[DataContract(Name = "Employee")]

public class Employee

{

private

int? _employeeId;;

private

byte[] _photo;

[

DataMember(IsRequired = false, Name = "Photo", Order = 70)]

[

DataMember(IsRequired = false, Name = "EmployeeId", Order = 0)]

public int? EmployeeId

{

get { return _employeeId; }

set { _employeeId = value; }

}

public byte[] Photo

{

get { return _photo; }

set { _photo = value; }

}

Then in my CRUD function

if (employee.Photo != null && employee.Photo.Length > 0)

{

dalEmployee.Photo =

new System.Data.Linq.Binary(employee.Photo);

}

There are some other things I’m sure I’m going to have to account for but this should be a good starting point for anybody having trouble.

Categories: Software Development