using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationRuntime;
using System.IO;
using Microsoft.SharePoint.Security;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Web.SessionState;
namespace Demo {
public class MyCustomApplication : IHttpModule {
#region IHttpModule Members
private SyncedInfo _synced = null;
private GCHandle? _allocatedMem = null;
private GCHandle? AllocatedMemory {
get {
return _allocatedMem;
}
set {
_allocatedMem = value;
}
}
private SyncedInfo Singleton {
get {
return _synced;
}
set {
_synced = value;
}
}
public void Dispose() {
if (AllocatedMemory != null) {
AllocatedMemory.Value.Free();
}
}
public void Init(HttpApplication context) {
if (Singleton == null) {
Singleton = new SyncedInfo(context.Modules["Session"]);
AllocatedMemory = GCHandle.Alloc(Singleton);
}
}
#endregion
}
[Serializable]
public class SyncedInfo {
protected SessionStateModule _sessionModule;
protected HttpContext _currentContext = HttpContext.Current;
public SyncedInfo(IHttpModule context) {
_sessionModule = (SessionStateModule)context;
_currentContext.Application["UserData"] = new Dictionary<string, string>();
_sessionModule.Start += Session_Start;
_sessionModule.End += Session_End;
}
public bool Push(string customer, string selectedData) {
bool retval = false;
object userData = _currentContext.Application["UserData"];
try {
lock (userData) {
if (!((Dictionary<string, string>)userData).ContainsKey(customer)) {
((Dictionary<string, string>)userData).Add(customer, selectedData);
} else {
((Dictionary<string, string>)userData)[customer] = selectedData;
}
retval = true;
}
} catch { } // Swallow the exception
return retval;
}
public KeyValuePair<string, string>? Pop(string customer) {
KeyValuePair<string, string>? retval = null;
object userData = _currentContext.Application["UserData"];
try {
lock (userData) {
if (((Dictionary<string, string>)userData).ContainsKey(customer)) {
retval = new KeyValuePair<string, string>(customer, ((Dictionary<string, string>)userData)[customer]);
((Dictionary<string, string>)userData).Remove(customer);
}
}
} catch { retval = null; } // Swallow the exception
return retval;
}
private void Session_End(object sender, EventArgs e) {
// Serialize data here (Pop)
SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate() {
WriteActivity(false);
}));
}
private void Session_Start(object sender, EventArgs e) {
// Push should be call from the App (UI)
SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate() {
WriteActivity(true);
}));
}
private void WriteActivity(bool state) {
string fileName = @"c:\log.txt";
try {
using (StreamWriter logWriter = (!File.Exists(fileName) ? File.CreateText(fileName) : File.AppendText(fileName))) {
logWriter.WriteLine(string.Format("Session {0} at {1}", new object[] { (state ? "Started" : "Ended"), DateTime.Now }));
logWriter.Flush();
logWriter.Close();
}
} catch { } // Swallow exception
}
}
}