ASP.NET initialization of array - asp.net

I have website with array (list) of 1000 objects, these objects are loading from json to array every website refresh. I would like to load these objects from json to array only once and keep it in RAM for others users. Because everytime read file is much slower than read it from RAM.
I am using ASP.NET Web Forms
How is it posssible?

I would recommend to define the array as an static member of a class and then initialize it with help of Global.asax, use the Application_Start event handler.
to add Global.asax to you project in Visual Studio:
File -> New -> File -> Global Application Class
Here is a sample C# code for Global.asax.cs:
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// ... Your initialization of the array done here ...
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}

Are these values static, i.e., do they stay constant while your application is running? In that case, the easiest way is to cache those values.
You can use static variables for that, but the recommended way is to use the thread-safe Cache object provided by ASP.NET. It can be accessed with the Cache property of the Page or of the HttpContext.
Example:
var myList = (MyListType)Cache["MyList"];
if (myList == null)
{
myList = ...; // Load the list
Cache["MyList"] = myList; // Store it, so we don't need to load it again next time.
}
Further reading:
Caching Application Data

Related

Base Page/Derived Page event model in ASP.NET

I have written the following code in ASP.NET
I have a base page:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
I have a derived page which have following code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
Now while I am calling the Derived page it doesn't call Base Page's Page_Load. It displays
"Derived Page Called".
Now if I change the Derived page Load event handler name to "Page1_Load" and the implementation as following, the Base page is called.
protected void Page1_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
"Base Page Called".
What is the reason for this kind of behaviour?
Page_Load is automatically wired up if there exists a method with the Page_Load name, so if you define one in the derived class it will hide the one from the base. However, it has to match by name, so by giving the one in the derived class a suffix, it no longer hides the base implementation, so it will pick up the base one and use it.
If you put Page1_Load in the base as well, you will get no output
Base:
protected virtual void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
Derived:
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load();
Response.Write("Derived Page Called");
}
Try the above if you want both to be called

Create specific LoginError asp.net

I'm using the LoginPage by .net 4.5.
When the fields are empty I get an error in red which say I can't connect.
What I want to do, is when I enter username and passwords, I try to log into DB, and if I get false, I want a custom error which says I can't log into DB.
Here is the code:
protected void LoginButton_Click(object sender, EventArgs e)
{
if (!DBHandle.DBConnect(UserLogin.UserName, UserLogin.Password))
{
}
}
protected void UserName_TextChanged(object sender, EventArgs e)
{
}
protected void UserLogin_LoginError(object sender, EventArgs e)
{
UserLogin.FailureText = "asdad";
}
What I have to put in the condition, that cause to get to LoginError?
Thanks!

Registered HttpApplication events in ASP.NET

I have the following query:
In ASP.NET Global.ascx file following HttpApplication events are defined:
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
As I can understand, these are HttpApplication event handlers. But there are lots of other events also. Are the Global.ascx events are only registered events? If not then, What are the other events those had been registered?
Also, say I have implemented a HttpModule on implementing Application_BeginRequest eventhandler. Now, the ASP.NET Framework also implemented the same. Then would my implementation overrides the Framework ones?
You can attach as many as handlers to an event. For more information read MSDN pages on - Handling events.
Read MSDN - Life Cycle Events and the Global.asax file and ASP.NET Application Life Cycle Overview for IIS 7.0

How does Global.asax PostAuthenticateRequest event binding happen?

How can I use the PostAuthenticateRequest event of Global.asax? I'm following this tutorial and it mentions that I have to use the PostAuthenticateRequest event. When I added the Global.asax event it created two files, the markup and the code-behind file. Here is the content of the code-behind file
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace authentication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Now when I type the
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
It is successfully called. Now I want to know how is the PostAuthenticateRequest bound to this Application_OnPostAuthenticateRequest method? How can I change the method to some other?
Magic..., a mechanism called Auto Event Wireup, the same reason you can write
Page_Load(object sender, EventArgs e)
{
}
in your code-behind and the method will automatically be called when the page loads.
MSDN description for System.Web.Configuration.PagesSection.AutoEventWireup property:
Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.
When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname(), such as Page_Load() or Page_Init(). ASP.NET first looks for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). If an event handler with this signature is not found, ASP.NET looks for an overload that has no parameters. More details in this answer.
If you wanted to do it explicitly you would write the following instead
public override void Init()
{
this.PostAuthenticateRequest +=
new EventHandler(MyOnPostAuthenticateRequestHandler);
base.Init();
}
private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}

Getting number of instances of asp.net website

I have an asp.net website and i want to get the number of users currently viewing my site. I am aware that there are some third party softwares available, that would give me the list of the users online but i don't want to do that.
Does anyone know how this can be achieved in asp.net? May be if there are any server variables that would keep a track of the website instances that gives the number of users currently visiting the site. Please help me.
if you want to count users which are using your website at the moment you can use the following code in your global.asax file:
private int activeUsers = 0;
protected void Session_Start(Object sender, EventArgs e)
{
activeUsers++;
Context.Items["activeUsers"] = activeUsers;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Context.Items.Add("activeUsers", activeUsers);
}
protected void Session_End(Object sender, EventArgs e)
{
if(activeUsers > 0)
activeUsers--;
}
protected void Application_End(Object sender, EventArgs e)
{
activeUsers = 0;
}
I would use performance counters instead.
Look under
ASP.NET Application Performance Counters
http://msdn.microsoft.com/en-us/library/fxk122b4.aspx

Resources