Weird issue in Site Performance - asp.net

We have very weird problem with our web site. When we run our site for the first time in the day (it is not first time after deployment) it runs very slow and take 5 minutes to load the page (any browser) even if no body is connected to that site at that moment. But, once we start using it, opens various pages, it runs like a charm - very fast (not more than 4 seconds) I mean to say and even if single or multiple users are connected to it. In clear terms, if site remains idle, site performs badly for the first time but once we start using it, it runs in usual. We are using following to build the site:
MVC 4
Dot net framework 4.5
Database: SQLAnywhere and SQL Server 2008 (we have tried to use both the database to resolve the issue but no success)
Entity Framework 5.0 using Web API model and we are using jQuery call to have data on page and showing them on DataTable grid (http://www.datatables.net/)
Hosted on IIS 7.5
Note: This site was working correctly few days back but we don't know what went wrong (in code or hosting settings etc.) during last few deployments but it works very slow. We have tried with everything but now we are in need of your expert guidance.
Many thanks in advance.
My code in global.asax.cs is:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//ConfigureApi(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Filters.Add(new ModelValidationFilterAttribute());
FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);
BundleTable.EnableOptimizations = true;
}
protected void Session_Start(object src, EventArgs e)
{
SessionHelper.EnterPriceID = 1;
SessionHelper.CompanyID = 1;
SessionHelper.RoomID = 1;
SessionHelper.UserID = 1;
SessionHelper.RoomName = "Room1";
SessionHelper.UserName = "Admin";
SessionHelper.CompanyResourceFolder = SessionHelper.EnterPriceID.ToString() + "_" + SessionHelper.CompanyID.ToString();
eTurns.DTO.Resources.ResourceHelper.ResourceDirectoryPath = HttpContext.Current.Server.MapPath(#"\Resources\" + SessionHelper.CompanyResourceFolder) + #"\";
eTurns.DTO.Resources.ResourceHelper.ResourceBaseClassPath = eTurns.DTO.Resources.ResourceHelper.ResourceDirectoryPath.Replace(#"\", ".");
System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = c;
Thread.CurrentThread.CurrentCulture = c;
Session["CurrentCult"] = c;
}
public void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null && Session["CurrentCult"] != null)
{
string currentCulture = Convert.ToString(Session["CurrentCult"]);
if (String.Compare(currentCulture, System.Threading.Thread.CurrentThread.CurrentCulture.ToString(), StringComparison.OrdinalIgnoreCase) != 0)
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
try
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(currentCulture);
}
catch
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
}
}
}
}

Increase your IDEAL TIMEOUT 20 M to 900 M (as per your convenience)....
I hope this works.....

Related

session time out before specefied time in asp.net application

I have a web application hosted on IIS 7 running on windows server 2008 R2.Users of my application where able to login the application with default session timeout of 20 minutes which is configured in web.config file and session settings on IIS 7.
Users authentication is done by active directory domain services running on another server, that i implemented using this code.
code:
public bool CheckUserInActiveDirectory(string userName)
{
try
{
string filter = "(&(objectCategory=person)(objectClass=user)(SAMAccountName=" + userName + "))";
string[] propertiesToLoad = new string[2] { "name", "PwdLastSet" };
DirectoryEntry root = new DirectoryEntry(activeDirectoryAddress);
root.AuthenticationType = AuthenticationTypes.None;
root.Username = activeDirectoryName + activeDirectoryDefaultUser;
root.Password = activeDirectoryDefaultPass;
DirectorySearcher searcher = new DirectorySearcher(root, filter, propertiesToLoad);
int count = searcher.FindAll().Count;
if (count >= 1)
return true;
else
return false;
}
catch (Exception ex)
{
throw;
}
}
since my application server (with windows server 2008 R2) is joined to Domain Group, Users session is null, less than 20 minutes and the session time is not equal for other users, and is changed every time for each user.
Finally the user is redirected to login page.Is there any body who knows the reason and guide me how to resolve the issue.
code :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserSession"] == null)
Response.Redirect("LoginPage.aspx");
}

how convert web browser control into web application, and some events

You people might be think this question is pretty simple but really I am structed here. Could you please give me a proper solution for this
I have a Windows sample like the following in button click event
AuthURI= "http://.......";
**Webbrowser1.Navigate(AuthURI);**
private void **Webbrowser1_DocumentCompleted**(object sender, WebBrowserDocumentCompletedEventArgs **e**)
{
try
{
if (e.Url.ToString().Contains("code="))
{
string[] responseOauth = Regex.Split(e.Url.ToString(), "&");
for (int i = 0; i < responseOauth.Count(); i++)
{
string[] nvPair = Regex.Split(responseOauth[i], "=");
drive.AccessCode = nvPair[1];
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is windows application code but now I want to change this application into web application
but I strucked WebBrowserDocumentCompletedEventArgs e how to pass this event like e.Url in my web application
what is replacement for this event I tried in google but exact result I didn't get. Please give me a proper solution for this

Application_BeginRequest not firing (NOT URL rewrite)

I would like to write out incoming requests for a SOAP web service. Turns out that my Application_BeginRequest event is not firing. It is running on IIS6 & Windows 2003, so the config change recommended elsewhere doesn't have any effect.
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
byte[] inputBytes = new byte[HttpContext.Current.Request.ContentLength];
HttpContext.Current.Request.InputStream.Position = 0;
HttpContext.Current.Request.InputStream.Read(inputBytes, 0, inputBytes.Length);
string requestString = ASCIIEncoding.ASCII.GetString(inputBytes);
Logger.Write("input: " + Convert.ToString(inputBytes.Length), Global.LOGGER_EVENT_SOURCE, 0, 0, TraceEventType.Warning);
using (var writer = new StreamWriter(#"c:\inetpub\wwwroot\requests\testfile - " + DateTime.Now.ToString("MMddYYYYhhmmssffff") + ".xml"))
{
writer.Write(requestString);
writer.Close();
}
// Reset stream pointer to beginning.
HttpContext.Current.Request.InputStream.Position = 0;
}
catch (Exception ex)
{
// Error handling; omitted here.
}
}
The code 'almost' works if I just put it inside of the web service method being called; the file is created but the request has a content length of '0'.
Thanks for any advice.
The most likely reason for this issue is that the site is not set as an Application.

Output cache is not working .net4.0

We are working on a asp.net site with .net framework 4.0. and we tried to incorporate output cache for it.
But unfortunately it is not worked. Later we found removing Microsoft security update KB2656351 will solve the problem.
I want to know whethere is there any other way to do this without removing the update.
This issue is there only when you install the above mention update and there is a cookies on the response. No matter if cookies contains in request. Found a workaround to fix this problem. I have created a custom HTTPModule and copy all available cookies from the response(including newly added cookies) to the Context.Items. then clear all the cookies available in the response.
In the next step, read the object stored in the Context.items and add back to the response. So when output cache provider is trying to cache the page there is no cookies in the response. so it works as usual. and then adding the cookies back.
public void Init(HttpApplication context)
{
context.PostReleaseRequestState += new EventHandler(OnPostReleaseRequestState);
context.PostUpdateRequestCache += new EventHandler(OnPostUpdateRequestCache);
}
public void OnPostReleaseRequestState(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
HttpCookieCollection cookieCollection = new HttpCookieCollection();
foreach (string item in context.Response.Cookies)
{
HttpCookie tempCookie = context.Response.Cookies[item];
HttpCookie cookie = new HttpCookie(tempCookie.Name) { Value = tempCookie.Value, Expires = tempCookie.Expires, Domain = tempCookie.Domain, Path = tempCookie.Path };
cookieCollection.Add(cookie);
}
context.Items["cookieCollection"] = cookieCollection;
context.Response.Cookies.Clear();
}
public void OnPostUpdateRequestCache(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
HttpCookieCollection cookieCollection = (HttpCookieCollection)context.Items["cookieCollection"];
if (cookieCollection != null)
{
foreach (string item in cookieCollection)
{
context.Response.Cookies.Add(cookieCollection[item]);
}
}
}
There was some problem reported here for this update, and repairing .net Framework 4 worked. It might be because of the corruption of .net Framework or the order in which framework and IIS is installed, which de-registers ASP.Net, so we need to register ASP.Net specifically, which sometimes causes these issue.
I would suggest to repair .Net framework, and the registering ASP.Net separately to see if that works.

ASP.NET MetaTags from database

I'm developing a web site in which the site manager can change dinamically the MetaTags for the web site in a cms, storing those MetaTags in a MySql database.
In the public master.page, in the Page_Load event, I get those MetaTags from the database
sTitle = get_from_data_base();
using (HtmlMeta mTag = new HtmlMeta())
{
mTag .Name = "Title";
mTag .Content = sTitle;
Page.Header.Controls.Add(mTag);
}
The problem is everytime a page loads, the load event of the master.page is loading from database those MetaTags.
How can I keep in cache or something similar the metatags once they have been loaded so the site doesn't access the database on every request?
How can the site knows when those MetaTags have changed to load them again?
Please, could you provide an example.
Thanks so much.
One of the solutions to do is to load the data once in the global.asax application start event and add the result in application object then in each page you need it, load it from the application object.
When the data is changed in the database, access the application object and update it as well.
If you don't know how to deal with global.asax or application object, i will post code.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
string sTitle = get_from_data_base();
Application.Add("sTitle", sTitle);
}
public string get_from_data_base()
{
//update this to call the database and get the data
return "Your data";
}
In your pages write the following:
string sTitle = Application["sTitle"].ToString();
using (HtmlMeta mTag = new HtmlMeta())
{
mTag.Name = "Title";
mTag.Content = sTitle;
Page.Header.Controls.Add(mTag);
}
While your manage is updating the metatag append the following line
Application["sTitle"] = "You manager entered string";
Finally, after your answers and reading on the Internet I went for the Cache object instead Application object. I have read is better, so my final aproach for this would be to add this on the global class and access this method from my master.pages
public static MetaTagsDN get_meta_tags()
{
MetaTagsDN oTags;
if (HttpRuntime.Cache["MetaTags"] == null)
{
MetaTagsLN ln = new MetaTagsLN();
oTags = ln.get_metatags();
HttpRuntime.Cache["MetaTags"] = oTags;
}
else
{
oTags = (MetaTagsDN)HttpRuntime.Cache["MetaTags"];
}
return oTags;
}
What do you think?
Thanks for helping me...

Resources