Tuesday, October 12, 2010

Common issues when migrating from 3.5 to 4.0

I recently had to migrate one of our enterprise level applications to .net 4.0 and it was a challenge to say the least in terms of isolating fixes for some of the obscure issues. Here is a list of some of the issues that I face while migrating and how I overcame them:

Problem) There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined

Solution) In .net 4.0 the config sections scriptResourceHandler, jsonSerialization, profileService, authenticationService, roleService amongst others are defined in the machine.config. So there is no longer a need to explicitly define them. Of course if you need to change any of these settings then you will need to remove the config sections http://msdn.microsoft.com/en-us/library/ms228258.aspx and then readd the config section and modify the settings as desired.

Problem) ‘Schema specified is not valid’ error message gets thrown while trying to upgrade your Entity Model to 4.0.

Solution) Regenerate the connection string of your model. It should look like some below. As you can see, the upgraded version of EF required much more info about where to get its metadata resource.

add name="TestEntities" connectionString="metadata=res://*/BusinessRuleEntity.csdl|res://*/BusinessRuleEntity.ssdl|res://*/BusinessRuleEntity.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(local)\sqldefault;Initial Catalog=TestDB;User ID=TestUser;Password=*****"" providerName="System.Data.EntityClient"

Problem) A potentially dangerous Request.Form value was detected from the client when loading an html page.

Solution) This issue can be fixed by adding the directive to the section of the web.config, and .NET then honours the directive that's in the same root web.config.

Tuesday, September 21, 2010

Windows Azure Performance Monitoring and Logging

What is managed code?

I have been out for the last year researching and developing on Azure. Now that my Azure project is in a stable state I have more time to start posting on articles I find useful again. Recently I have been on a hiring spree, and have been looking into questions to ask to gauge the true understanding and depth of a potential candidate, and one fundamental question I find that most folks lack an understanding for is what is managed code. So here I have posted a indepth answer to that question.

Managed code is code that has its execution managed by the .NET Framework Common Language Runtime. It refers to a contract of cooperation between natively executing code and the runtime. This contract specifies that at any point of execution, the runtime may stop an executing CPU and retrieve information specific to the current CPU instruction address. Information that must be query-able generally pertains to runtime state, such as register or stack memory contents.

The necessary information is encoded in an Intermediate Language (IL) and associated metadata, or symbolic information that describes all of the entry points and the constructs exposed in the IL (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard (which the CLR is the primary commercial implementation) describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding. All a developer has to know is that any of the languages that target the runtime produce managed code emitted as PE files that contain IL and metadata. And there are many such languages to choose from, since there are nearly 20 different languages provided by third parties – everything from COBOL to Camel – in addition to C#, J#, VB .Net, Jscript .Net, and C++ from Microsoft.

Before the code is run, the IL is compiled into native executable code. And, since this compilation happens by the managed execution environment (or, more correctly, by a runtime-aware compiler that knows how to target the managed execution environment), the managed execution environment can make guarantees about what the code is going to do. It can insert traps and appropriate garbage collection hooks, exception handling, type safety, array bounds and index checking, and so forth. For example, such a compiler makes sure to lay out stack frames and everything just right so that the garbage collector can run in the background on a separate thread, constantly walking the active call stack, finding all the roots, chasing down all the live objects. In addition because the IL has a notion of type safety the execution engine will maintain the guarantee of type safety eliminating a whole class of programming mistakes that often lead to security holes.

Contrast this to the unmanaged world: Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that’s the last the OS knows. There are protections in place around memory management and port I/O and so forth, but the system doesn’t actually know what the application is doing. Therefore, it can’t make any guarantees about what happens when the application runs.

Wednesday, July 1, 2009

How to reverse contents of a file in windows

I recently needed to mimic the linux functionality of being able to reverse the contents of any file, on windows. After playing with a few options, I found vb-scripting to be the least painful way to do this. See below for details:

Step 1:
Save the script below as a .vbs file for example ReverseStream.vbs

' ------- BEGIN CALLOUT A -------Dim Stack: Set Stack = CreateObject("System.Collections.Stack")' ------- END CALLOUT A -------
' ------- BEGIN CALLOUT B -------Do While Not WScript.StdIn.AtEndofStream Stack.Push WScript.StdIn.ReadLineLoop' ------- END CALLOUT B -------
' ------- BEGIN CALLOUT C -------WScript.StdOut.WriteLine Join(Stack.ToArray, vbCrLf)' ------- END CALLOUT C -------

Step 2:
To run the script use the following command on the cmd shell

c:\>cscript c:\ReverseStream.vbs c:\Reversefile.xml

Wednesday, June 10, 2009

Download from Datatable to EXCEL/CSV

Code snippet to download from a datatable to Excel/CSV, for a given number of rows from a machine where Excel may not necessarily be installed.(I.E if you dont have office the file loads from any other editor that will load a CSV)


protected void ExportToExcel()
{
string query = @"AS
(
SELECT *, RANK() OVER(ORDER BY [column_name]) AS RankNumber FROM [table_name]
)
SELECT * FROM CTE WHERE RankNumber >= @StartRow AND RankNumber <= @EndRow";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = new SqlConnection("");
cmd.Parameters.AddWithValue("@StartRow", Page.Request.QueryString["StartRow"].ToString());
cmd.Parameters.AddWithValue("@EndRow", Page.Request.QueryString["EndRow"].ToString());
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
string name = "CSVExport";
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dt.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}
}

Tuesday, June 9, 2009

How to retain scrool position in tree view after postback

I have been looking for an elegant solution to this problem for a while here is the cleanest one I have found so far:

///
/// Handles the click event when a tree node is selected
///

/// treeview reference
///
protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
{
// Cast the sender to a treeview
TreeView T = sender as TreeView;
//Execute required code
//Finally register the start of JScript to load to selected treeview
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "selectNode", "var elem = document.getElementById('" + T.ClientID + "_SelectedNode');var node = document.getElementById(elem.value);node.scrollIntoView(true);elem.scrollLeft=0;", true);
}