ASP.NET MVC3 : Connected clients count - asp.net

In my ASP.NET MVC3 web application I'd like to add a small message displaying the number of users currently browsing the site.
I'm currently using Session_Start and Session_End application events to increment or decrement a static property inside Global.asax.
This works, but it isn't precise at all. Since my session timeout is configured to 20mn there's a huge delay between updates.
Is there a more elegant, precise way of doing this?
I've thought of calling an action via AJAX which simply does Session.Abandon() on the window.onbeforeunload javascript event, but this would be called each time the user changed pages. Is there a way to determine when the user closes his browser or leaves the domain?
Any hints, comments or code examples would be welcome!
Here is the relevant part of current code:
public class MvcApplication : System.Web.HttpApplication
{
public static int UsersConnected { get; set; }
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
UsersConnected++;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
UsersConnected--;
Application.UnLock();
}
...
}

Okay this was a bit tricky but I've come with an 'okay' solution.
First, Ive created a static dictionary in Global.asax which will store the IP address of the clients and their last poll date.
public class MvcApplication : System.Web.HttpApplication
{
public static Dictionary<string, DateTime> ConnectedtUsers { get; set; }
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ConnectedtUsers = new Dictionary<string, DateTime>();
}
...
}
Then, in my CommonsController, I've created this action which will add new clients, remove clients which haven't been polled in the last 30 seconds and update the poll date of already registered clients :
public class CommonsController : Controller
{
...
public JsonResult UserConnected()
{
string ip = Request.UserHostAddress;
if (MvcApplication.ConnectedtUsers.ContainsKey(ip))
{
MvcApplication.ConnectedtUsers[ip] = DateTime.Now;
}
else
{
MvcApplication.ConnectedtUsers.Add(ip, DateTime.Now);
}
int connected = MvcApplication.ConnectedtUsers.Where(c => c.Value.AddSeconds(30d) > DateTime.Now).Count();
foreach (string key in MvcApplication.ConnectedtUsers.Where(c => c.Value.AddSeconds(30d) < DateTime.Now).Select(c => c.Key))
{
MvcApplication.ConnectedtUsers.Remove(key);
}
return Json(new { count = connected }, JsonRequestBehavior.AllowGet);
}
}
Finally, on my layout page, added this code which will call my action every 30 seconds and output the result in a span :
<span id="connectedUsers"></span>
<script type="text/javascript">
function PollUsers()
{
$(function() {
$.getJSON("/Commons/UserConnected", function(json){ $('#connectedUsers').text(json.count + " user(s) connected")});
});
}
setInterval(PollUsers, 30000);
</script>
May not be that precise, maybe not that elegant either, but it works. Of course, multiple users from the same IP would count for one user. But it's the best solution I've experimented so far.

you can call a webmethod from javascript on unload body event.
http://www.codeproject.com/Tips/89661/Execute-server-side-code-on-close-of-a-browser
Best regards

Related

Server.TransferRequest does not get correct page

we are using HttpModule for switching aspx pages..
But if large number of client try to hit same time then people get wrong page on screen... I am not sure if something wrong in my code due to Server.TransferRequest.
Can any one give any suggestion?
public class SwitchMasterModule : IHttpModule, IRequiresSessionState
{
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
void Context_BeginRequest(object sender, EventArgs e)
{
var AppId = SiteSettings.ApplicationId().ToString();
if (HttpContext.Current.Request.CurrentExecutionFilePath.Equals("/default.aspx", StringComparison.InvariantCultureIgnoreCase))
{
HttpContext.Current.Server.TransferRequest(string.Format("~/Templates/{0}/default.aspx", AppId), true);
}
}
}
Moved from comment to answer.
It might be that SiteSettings.ApplicationId() returns incorrect value.
I don't know what the origin of the SiteSettings class is, but if it's not absolutely thread-safe, you could easily wind up with one user accessing the ApplicationId value appropriate for another user.

What are ways to update ASP.NET page from HttpModule?

I need to update an ASP.NET page from the HttpModule permanently, or from time to time.
Here is the code of IUpdatablePage interface for our page to be updated:
interface IUpdatablePage
{
void Update( string value );
}
Here is the code of HttpModule, I imagine, can be:
void IHttpModule.Init( HttpApplication application )
{
application.PreRequestHandlerExecute += new EventHandler( application_PreRequestHandlerExecute );
}
void application_PreRequestHandlerExecute( object sender, EventArgs e )
{
this._Page = ( Page )HttpContext.Current.Handler;
}
void HttpModuleProcessing()
{
//... doing smth
IUpdatablePage page = this._Page as IUpdatablePage;
page.Update( currentVaue );
//... continue doing smth
}
Here we:
save the current request page in _Page,
get access to IUpdatablePage interface while processing in HttpModule
call Update function passing some currentValue.
Now the page gets the value in Update function.
public partial class MyPage: System.Web.Page, IUpdatablePage
{
void IUpdatablePage.Update( string value )
{
// Here we need to update the page with new value
Label1.Text = value;
}
}
The question is what are the ways to transmit this value to the webform controls so that they would immediately show it in browser?
I suppose any way of refreshing the page: using UpdatePanel, Timer, iframe block, javascript etc.
NOTE, that the request from the page is being processed in HttpModule while refresh.
Please, help with code samples (I'm a web-beginner).
The way to transfer data between Page and HttpModule is to use Application named static objects, that are identified by session id.
The page is update via UpdatePanel triggered by timer.
The code of HttpModule (simplified):
public class UploadProcessModule : IHttpModule
{
public void Init( HttpApplication context )
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest( object sender, EventArgs e )
{
HttpContext context = ( ( HttpApplication )sender ).Context;
if ( context.Request.Cookies["ASP.NET_SessionId"] != null )
{
string sessionId = context.Request.Cookies["ASP.NET_SessionId"].Value;
context.Application["Data_" + sessionId] = new MyClass();
}
}
}

Partial caching of custom WebControls

I need to cache the generated content of custom WebControls. Since build up of control collection hierarchy is very expensive, simple caching of database results is not sufficient. Caching the whole page is not feasible, because there are other dynamic parts inside the page.
My Question: Is there a best practice approach for this problem? I found a lot of solutions caching whole pages or static UserControls, but nothing appropriate for me. I ended up with my own solution, but im quite doubtful if this is a feasible approach.
A custom WebControl which should be cached could look like this:
public class ReportControl : WebControl
{
public string ReportViewModel { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Fake expensive control hierarchy build up
System.Threading.Thread.Sleep(10000);
this.Controls.Add(new LiteralControl(ReportViewModel));
}
}
The aspx page which includes the content control(s) could look as follows:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Fake authenticated UserID
int userID = 1;
// Parse ReportID
int reportID = int.Parse(Request.QueryString["ReportID"]);
// Validate if current user is allowed to view report
if (!UserCanAccessReport(userID, reportID))
{
form1.Controls.Add(new LiteralControl("You're not allowed to view this report."));
return;
}
// Get ReportContent from Repository
string reportContent = GetReport(reportID);
// This controls needs to be cached
form1.Controls.Add(new ReportControl() { ReportViewModel = reportContent });
}
private bool UserCanAccessReport(int userID, int reportID)
{
return true;
}
protected string GetReport(int reportID)
{
return "This is Report #" + reportID;
}
}
I ended up writing two wrapper controls, one for capturing generated html and a second one for caching the content - Quite a lot of code for simple caching functionality (see below).
The wrapper control for capturing the output overwrites the function Render and looks like this:
public class CaptureOutputControlWrapper : Control
{
public event EventHandler OutputGenerated = (sender, e) => { };
public string CapturedOutput { get; set; }
public Control ControlToWrap { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Controls.Add(ControlToWrap);
}
protected override void Render(HtmlTextWriter writer)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
base.RenderChildren(htmlTextWriter);
CapturedOutput = stringWriter.ToString();
OutputGenerated(this, EventArgs.Empty);
writer.Write(CapturedOutput);
}
}
The wrapper control to cache this generated output looks as follows:
public class CachingControlWrapper : WebControl
{
public CreateControlDelegate CreateControl;
public string CachingKey { get; set; }
public delegate Control CreateControlDelegate();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string content = HttpRuntime.Cache.Get(CachingKey) as string;
if (content != null)
{
// Content is cached, display
this.Controls.Add(new LiteralControl(content));
}
else
{
// Content is not cached, create specified content control and store output in cache
CaptureOutputControlWrapper wrapper = new CaptureOutputControlWrapper();
wrapper.ControlToWrap = CreateControl();
wrapper.OutputGenerated += new EventHandler(WrapperOutputGenerated);
this.Controls.Add(wrapper);
}
}
protected void WrapperOutputGenerated(object sender, EventArgs e)
{
CaptureOutputControlWrapper wrapper = (CaptureOutputControlWrapper)sender;
HttpRuntime.Cache.Insert(CachingKey, wrapper.CapturedOutput);
}
}
In my aspx page i replaced
// This controls needs to be cached
form1.Controls.Add(new ReportControl() { ReportViewModel = reportContent });
with
CachingControlWrapper cachingControlWrapper = new CachingControlWrapper();
// CachingKey - Each Report must be cached independently
cachingControlWrapper.CachingKey = "ReportControl_" + reportID;
// Create Control Delegate - Control to cache, generated only if control does not exist in cache
cachingControlWrapper.CreateControl = () => { return new ReportControl() { ReportViewModel = reportContent }; };
form1.Controls.Add(cachingControlWrapper);
Seems like a good idea, maybe you should pay attention to :
the ClientIdMode of the child controls of your custom control to prevent conflicts if these controls are to be displayed in another context
the LiteralMode of your Literal : it should be PassThrough
the expiration mode of your cached item (absoluteExpiration/slidingExpiration)
disable ViewState of your CustomControl
Recently, I tend to have another approach : my wrapper controls only holds some javascript that performs an AJAX GET request on a page containing only my custom control.
Caching is performed client side through http headers and serverside through OutputCache directive (unless HTTPS, content has to be public though)

Using HttpModules to modify the response sent to the client

I have two production websites that have similar content. One of these websites needs to be indexed by search engines and the other shouldn't. Is there a way of adding content to the response given to the client using the HttpModule?
In my case, I need the HttpModule to add to the response sent to the when the module is active on that particular web.
You'd probably want to handle the PreRequestHandlerExecute event of the application as it is run just before the IHttpHandler processes the page itself:
public class NoIndexHttpModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += AttachNoIndexMeta;
}
private void AttachNoIndexMeta(object sender, EventArgs e)
{
var page = HttpContext.Current.CurrentHandler as Page;
if (page != null && page.Header != null)
{
page.Header.Controls.Add(new LiteralControl("<meta name=\"robots\" value=\"noindex, follow\" />"));
}
}
}
The other way of doing it, is to create your own Stream implementation and apply it through Response.Filters, but that's certainly trickier.

Auto wiring of Property does not work for me

In my Asp.Net project I wanna use Property Auto-wiring, e.g. for my ILogger. Basically I placed it as Property into class where I need to use it. Like below.
public class UserControlBase : UserControl
{
public ILogger CLogger { get; set; }
....
}
public partial class IPTracking : UserControlBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//it works
ILogger logger = ObjectFactory.GetInstance<ILogger>();
logger.LogInfo(string.Format("Client IP: {0}", ip));
//it does not work
CLogger.LogInfo(string.Format("Client IP: {0}", ip));
}
}
}
However when calling in inherited control, logger is null. I checked the container and it'a definitely set as above implementation shows. Below is setting which is called from Global.asax.
public static void SetupForIoC()
{
Debug.WriteLine("SetupForIoC");
ObjectFactory.Initialize(x =>
{
x.FillAllPropertiesOfType<ILogger>().Use<EntLibLogger>();
});
Debug.WriteLine(ObjectFactory.WhatDoIHave());
}
Thanks for any advice, tip? X.
Update:
- I didnt mentioned before, but its Asp.Net webforms 3.5.
- I can't see what I am missing. I guess it could be because the injection gets involved later in process and didnt get set in requested class.
Link to desc. of usage: http://structuremap.github.com/structuremap/ConstructorAndSetterInjection.htm#section7
Give something like this a shot.
FillAllPropertiesOfType<ILogger>().AlwaysUnique().Use(s => s.ParentType == null ? new Log4NetLogger(s.BuildStack.Current.ConcreteType) : new Log4NetLogger((s.ParentType)));
Check out another StackOverflow answer I have which discusses using StructureMap to auto wire loggers.
Where do you actually set the CLogger property on the user control? Also, if you wanted to use one logger within the page, you could have the User cotnrol do:
public ILogger CLogger
{
get
{
if (this.Page is PageBase)
return ((PageBase)this.Page).Logger;
else
return null;
}
}

Resources