<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<machineKey validationKey='44DB9132AD56D581F6DBFDD8F09EDFD167B75283771DB3241804B0F03602EBACF6349C724FAC64785F60653EC9F9CA20A4D377387A1677704525FE9147CE6AC3' decryptionKey='C77E66D32F8F554C507DDEC6DA2A1A470B5EE195DA584EDD' validation='SHA1'/>
</system.web>
</configuration>
A expert on system architect with full SDLC and Agile development experience. Skills: , C# C++ Scrum OOA/OOD javascript ASP.Net MVC XML, HTML Web Service, RESTful, WCF, Android and etc. Email me if you need help on development.
Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts
Thursday, 8 December 2011
Validation of viewstate MAC failed,in hosting on a web farm
If a web application is hosted by a web farm or cluster, you would experience this exception if you didn't set up the web.config correctly. The solution for resolving this exception is to ensure the configuration specifies the same validation key and validation algorithm in every single web.config in your deployment. AutoGeneration by default cannot be used in a cluster environment.
Below is the sample machinekey config entry for your web.config
Sunday, 13 November 2011
Manage IIS Application Pool from .Net C# code
.Net framework provides a very easy-to-use API wrapper class, ServerManager, to help manage IIS system from any .Net application using either C# or VB.net. Below C# code snippet demonstrates how to restart/recycle a given AppPool in IIS.
public void RestartAppPool(string appPool)
{
try
{
logger.Info("Restarting app pool: " + appPool);
// Recycle app pool
using (var manager = new ServerManager())
{
var pool = manager.ApplicationPools[appPool];
if (pool != null)
{
Process process = null;
if (pool.WorkerProcesses.Count > 0)
{
process = Process.GetProcessById(pool.WorkerProcesses[0].ProcessId);
}
if (pool.State == ObjectState.Stopped)
pool.Start();
else
pool.Recycle();
// clean up any worker thread created by app pool
if (process != null)
{
while (!process.HasExited)
{
Thread.Sleep(10);
}
process.Dispose();
}
}
}
//logger.Info("Successfully restarted app pool: " + appPool);
}
catch (Exception ex)
{
// logger.Error("Restart app pool error: " + ex);
}
}
Tuesday, 16 August 2011
Validation of viewstate MAC failed. System.Web.HttpException (0x80004005)
There are few workarounds provided by Microsoft back to 2008. See the original posting in here.
Personally I prefer solution#3 with Alex's improvement (see below). Luckily, .Net 4.0 or above has fix it within the framework.
Personally I prefer solution#3 with Alex's improvement (see below). Luckily, .Net 4.0 or above has fix it within the framework.
public class BasePage : Page
{
private static string[] aspNetFormElements = new string[]
{
"__EVENTTARGET",
"__EVENTARGUMENT",
"__VIEWSTATE",
"__EVENTVALIDATION",
"__VIEWSTATEENCRYPTED",
};
protected override void Render(HtmlTextWriter writer)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
int formStart = html.IndexOf("<form");
int endForm = -1;
if (formStart >= 0)
endForm = html.IndexOf(">", formStart);
if (endForm >= 0)
{
StringBuilder viewStateBuilder = new StringBuilder();
foreach (string element in aspNetFormElements)
{
int startPoint = html.IndexOf("<input type=\"hidden\" name=\"" + element + "\"");
if (startPoint >= 0 && startPoint > endForm)
{
int endPoint = html.IndexOf("/>", startPoint);
if (endPoint >= 0)
{
endPoint += 2;
string viewStateInput = html.Substring(startPoint, endPoint - startPoint);
html = html.Remove(startPoint, endPoint - startPoint);
viewStateBuilder.Append(viewStateInput).Append("\r\n");
}
}
}
if (viewStateBuilder.Length > 0)
{
viewStateBuilder.Insert(0, "\r\n");
html = html.Insert(endForm + 1, viewStateBuilder.ToString());
}
}
writer.Write(html);
}
}
Thursday, 11 August 2011
Use and transform NLog config in web.config
Ever wonder how to integrate your log platform configuration into your web.config so it can be transformed nicely into different environments, such as QA, staging and production environment? Below is a example config that I worked on for a public transit website.
The original web.config looks like
The original web.config looks like
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="File"
name="FileLog"
layout="${longdate} | ${level:uppercase=true} | ${logger} | ${message}"
fileName="c:\logs\log_${shortdate}.log"/>
<rules>
<logger name="*" minlevel="Trace" writeTo="FileLog" />
</rules>
</nlog>
Your Release.config, for example, will set to below if the production has different path for log files.
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
xmlns:nlg="http://www.nlog-project.org/schemas/NLog.xsd">
...
<nlg:nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<nlg:targets >
<nlg:target xsi:type="File"
name="FileLog"
layout="${longdate} | ${level:uppercase=true} | ${logger} | ${message}"
fileName="d:\logs\prod\Log_${shortdate}.log" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</nlg:targets>
</nlg:nlog>
</configuration>
Wednesday, 10 August 2011
Enable .Net 4.0 in IIS 7.0/7.5
Go to C:\Windows\Microsoft.NET\Framework(64)\v4.0.30319 and run the command from command prompt using "Run as Administrator".
cd \
cd C:\Windows\Microsoft.NET\Framework(64)\v4.0.30319
aspnet_regiis -ir
cd \
cd C:\Windows\Microsoft.NET\Framework(64)\v4.0.30319
aspnet_regiis -ir
Subscribe to:
Posts (Atom)