Tuesday, 12 June 2007

IIS6 app pools and session state

We've intermittently been having some sessions dropping on various web sites, after about 20mins (umm sounds familiar that value...), despite having session timeout settings of say 2 hours in our web app config files (both ASP and ASP.net). After some digging, I came across this:

http://blogs.msdn.com/david.wang/archive/2005/09/19/Why_do_I_lose_ASP_Session_State_on_IIS6.aspx

If you use in-proc session state (which is the default and very popular), your application pool's "shut down worker process after being idle" setting in IIS6 is likely to be 20 mins. Thus ignoring your web app config setting if your application pool is *idle* for 20 mins or more.

Easiest solution is to increase this idle timeout value. The other option is to store state out of process.

Wednesday, 23 May 2007

ViewState improvements in ASP.Net 2

I’ve recently been upgrading a project from ASP.Net 1.1 to ASP.Net 2. I’d seen documented that ViewState sizes had been significantly reduced, quoted as up to 50% smaller in some cases. A new serialization format, incorporated in a new formatter class (ObjectStateFormatter) is behind the improvements. See http://www.nikhilk.net/ViewStateImprovements.aspx for further info on how it all works.

Anyhow, I thought it would be interesting to see how much of an improvement I would get on some 'heavy' DataGrid driven data entry intranet pages. Here’s the stats in bytes:

ViewState Size ASP.Net 1.1 (ASP.Net 2)

Page 1: 9892 (7456 75%)
Page 2: 44492 (25696 58%)
Page 3: 39900 (24800 62%)
Page 4: 46300 (23500 51%)

So my worst case was ‘only’ a 75% reduction, but on some pages my ViewState was 51% of the original. Not bad. My advice of course is still to turn off ViewState when not needed ;-)

Monday, 30 April 2007

.Net 3.5 beta 1 is here

In case you missed it, .Net Framework 3.5 Beta 1 is now available for download. Obviously not something to install on a production (!) box but it is interesting in that .Net 3.5 is now "officially" on its way to becoming a production reality (rather than in technnology preview form, Orcas downloads etc).

As expected it includes LINQ (Language Integrated Query) and ASP.Net AJAX.

Friday, 20 April 2007

WPF/E becomes Silverlight

Microsoft's project name WPF/E (Windows Presentation Foundation / Everywhere) has now been officially named Microsoft Silverlight. Although still only on February CTP, watch this space as the full version is due later in the year (July/Aug'ish).

I like the direction this vector-based technology is going in - decarative XAML markup all nice and separate from the code (I'm a big advocate of decarative UI building). I also like the way it can be embedded into existing HTML/ASPX pages, thus offering a doorway for developers to use the technology in existing ASP.Net apps (it does not have to be an all or nothing approach).

Wednesday, 11 April 2007

Usefulness of Code Reviews

When I chat with developers about code reviews, generally I get a positive response in terms of their usefulness 'in theory', although most are not exactly forthcoming when it comes to actually offering to take part in them. I think some see them as a 'threat' or an 'opportunity to receive criticism of their precious code'! This shouldn't be the case and those that believe this I think have missed the point. I see code reviews (or more often in my case, 'system reviews') as opportunities to pause for thought, take a step back, look at what you've achieved and do a critique of how 'well' it has been designed and developed. Every review I have ever done has always had positive outputs.

I've just recently completed a code review of a medium sized project written in ASP.Net/Oracle. I did it 'paired' with another developer and was a joint effort. We concentrated on the following points:



  • The Solution and how it was organised into projects and subdirectory structures.

  • Shared libraries and their usage

  • Consistency in 'style of development'

  • Source control/versions. How Visual Source Safe (VSS) is used for (e.g. source code only or some docs too?)

  • Coding style and conformance to coding standards (and are the standards we have in place up to date/suitable?)

  • Any areas where it would benefit from refactoring

  • Upgrade comments (recently undertaken) from .Net 1.1 to .Net 2/VS 2005. Where could the project benefit from refactoring/rewritten using .Net 2 features

  • Use of the N-tier approach, coupling and possible compromising of layers (e.g. SQL in code behind files?!)

  • Stored procs (Oracle) and adopted standards

  • Pass thru SQL vs Stored procs

  • General concerns and feedback on development so far


Here's the outputs from our session:



  • the project could have benefited from being broken down into more sub-projects

  • our coding standards need updating to bring in new .Net 2 features, new controls, app_themes etc, and JavaScript coding guidelines.

  • N-Tier approach used very well in most places by developers although some SQL present in 'higher layers' from earlier coding (!). 

  • There were some areas where standards were not followed but mostly affecting 'early code'. Most of latest code followed guidelines and best practise

  • Would benefit from the use of Web Services in the shared 'site list' library: 

  • Source control working well. Oracle stored procs, views etc also in VSS.. but would be better if integrated with TOAD. To investigate. 

  • Better versioning needed of shared libs. 

  • Some of the system would benefit from some .Net 2 enhancements (eg master pages, themes, new .net 2 menu tree) but decided to only bring in new .Net 2 features on either brand new modules or where a significant change is needed on an existing part of the system. Consistency important though.


All of this I think is good positive feedback and result in actions where necessary. It was a joint effort, which is important. Even if you are reviewing a new starter's code, there is still opportunity to learn from their past experiences.


If anyone else has experience of doing such reviews, do let me know.

Thursday, 29 March 2007

SPA 2007 Conference

Just got back from SPA 2007, held at Cambridge University’s Homerton College.

Held across 4 days (Sun-Wed), it consisted of a mixture of workshops, tutorials, case studies and more open ‘think tank/gold fish bowl’ type sessions.

I presented on ASP.Net / Oracle including lessons learned from a global intranet project I’m currently working on. It triggered some good discussions, both during and after the session, especially regarding the backend platform used (i.e. Oracle rather than SQL Server), state management, along with data and workflow modelling.

Here are some of the other sessions I attended too:

Strategies and Patterns for Systems Continuity
Lessons Learned from Scaling XP
Agile modelling practices on innovative projects
The whys and wherefores of Web 2.0
Effective error handling
Architecting the next generation of .Net applications

Quality of the sessions varied but most were well run and worth attending. I didn’t perhaps ‘learn’ as much as I’d hoped, but two points I did take away were:

1) Web 2.0 is not clearly defined (you don’t say!). I don’t like the term anyway. To me the web is constantly evolving, with new tools appearing and becoming established all the time. To label a ‘moment in web time’ like this does not make sense. There was no clear agreement on details at the conference but most did agree that Web 2.0 will/is having a significant impact on the Internet and business.
2) Agile development (XP, Scrum, Lean) is becoming more widespread and is being used to good effect. I’ve not yet used any of these methods ‘formally’ yet but do already use some of the techniques they encompass. Armed with the knowledge I’ve gained (and some new books!), I’m definitely going to see if I can make use of more Agile techniques in the future.

For me though, the session that I remember most was the plenary by the infamous Professor Tony Hoare… acknowledged widely for his Quicksort algorithm, Hoare Logic, CSP (for formally describing interacting concurrent processes) and his research leading to the Z specification language.

He’s a scientist whom I have looked up to since at University. His plenary was on ‘Assertions and Test driven design’. The talk maybe did not contain as much ‘brilliance’ as his stature and background deserves, but nevertheless none of this matters – just to see him present in person was more than enough for me. A man worthy of his Turing Award (1980) and Knighthood (2000).

Wednesday, 14 March 2007

Upgrading projects from ODP.Net 9.2 to ODP.Net 10.2

I've recently been upgrading a system from ASP.Net 1.1 to 2.0. At the same time we are upgrading the Oracle client from 9.2 to 10.2, and thus to ODP.Net (vs10.2.0.2.20 to be precise).

I've now completed the first pass of the conversion and all main parts of the system appear to be working fine - note we have left the Oracle back-end on 9.2 for now, but this is also going to be upgraded to 10.2 too... let's do it in stages ;-)

Here's a couple of issues I've come across so far regarding the upgrade:

a) -1 Result from ExecuteNonQuery

It is expected behavior that ExecuteNonQuery returns -1 when executing a stored procedure (regardless of what the stored proc does), but there was a bug in earlier 9.2.x versions where it returned 1 instead of -1. If you have any code that checks this result, it's worth a scan to see if this affects any of your logic. I was caught out in one of my calls to a stored proc that updated a number of rows, and I had incorrectly assumed it would return "number of rows UPDATEd" like a standard UPDATE statement would (and in this case I was expecting an update count of 1 for 'success' so it all worked fine thanks to the 9.2 bug!). Digging deep into the Oracle ODP.Net docs come up with this:

-------
ExecuteNonQuery returns the number of rows affected, for the following:
If the command is UPDATE, INSERT, or DELETE and the XmlCommandType property is set to OracleXmlCommandType.None.
If the XmlCommandType property is set to OracleXmlCommandType.Insert, OracleXmlCommandType.Update, OracleXmlCommandType.Delete.
For all other types of statements, the return value is -1.
-------

So I read this as meaning all stored procs return -1 for success. If you want the number of recs affected, you'll need to do this yourself by adding an output param instead. If anyone has found out something different please shout ;-)


b) Runtime data types of output parameters:

Ever had problems mapping .Net types to native database field types (and vice versa)? Umm me too. DBNULLS vs nulls, casting issues, loss of accuracy, I'm sure you've been there. Anyway, here's a couple of general issues to watch out for when moving to ODP.Net 10.2:

Example 1:

OracleCommand cmd = new OracleCommand();
cmd.CommandText = "EPR_Month_PKG.Get_Month_YTD";
cmd.Parameters.Add("p_isd_id", OracleDbType.Int32,ParameterDirection.Input).Value = isdID;
cmd.Parameters.Add("p_mp_id", OracleDbType.Int32,ParameterDirection.Input).Value = mpID;
cmd.Parameters.Add("p_month_ytd", OracleDbType.Double).Direction = ParameterDirection.Output;
DBTool.ExecuteStoredProc(cmd);
// if (cmd.Parameters["p_month_ytd"].Value != System.DBNull.Value) // fine in 9.2 but condition never fires in 10.2
if (!((Oracle.DataAccess.Types.OracleDecimal)(cmd.Parameters["p_month_ytd"].Value)).IsNull) // works fine in 10.2
{
// not null... rest of code here
}
cmd.Dispose();

(Note DBTool is simply a DAL helper class that sits as part of a larger DAL library we have).

The commented out if statement worked fine in ODP.Net 9.2. but in 10.2 the output param (specified as OracleDbType.Double) now comes back as an OracleDecimal (hence my cast in the if statement). In fact I've also got cases where the output param type is specified as OracleDbType.Int32 and this still comes back as OracleDecimal. Oracle really needs to be consistent on all this, especially across versions.

Example 2:

Old working code in .Net 1.1/ODP.net 9.2

OracleCommand cmd = new OracleCommand();
cmd.CommandText = "EPR_Month_PKG.Get_Max_MP_ID";
cmd.Parameters.Add("p_max_mp_id", OracleDbType.Int32).Direction = ParameterDirection.Output;
DBTool.ExecuteStoredProc(cmd);
MaxMPID = (int) cmd.Parameters["p_max_mp_id"].Value; // this bit no longer works, you get System.InvalidCastException
cmd.Dispose();

Again, this now fails in 10.2 due to output param data type being returned as OracleDecimal (!).

This new code works fine though in 10.2:

OracleCommand cmd = new OracleCommand();
cmd.CommandText = "EPR_Month_PKG.Get_Max_MP_ID";
OracleParameter ParMaxMPID = new OracleParameter();
ParMaxMPID.Direction = ParameterDirection.Output;
ParMaxMPID.DbType = DbType.Int32;
ParMaxMPID.ParameterName = "p_max_mp_id";
cmd.Parameters.Add(ParMaxMPID);
DBTool.ExecuteStoredProc(cmd);
MaxMPID = (int) cmd.Parameters["p_max_mp_id"].Value;
cmd.Dispose();

The key bit is the specifying of DbType (instead of using OracleDbType). Doing this infers an OracleDbType (you can see a table of mappings in ODP.Net docs) and also ensures a correct mapping to the .Net int type. We could use this "DbType approach" to solve the issue in example 1 above too.

Strings as output params can be handled this way too. If you don't do it this way, I found nulls and empty strings were handled inconsistently in 10.2.


LATEST UPDATE (16 Mar 2007)

Having converted a number of calls to cater for the issues above I have added a static member to DAL class, e.g. DBTool.GetOracleParameterByDbType(...) so typical code can now look as follows:

cmd.CommandText = "EPR_MonthlyReport_PKG.Get_Target";
cmd.Parameters.Add("p_year", OracleDbType.Int32,ParameterDirection.Input).Value = year;
cmd.Parameters.Add("p_year_offset", OracleDbType.Int32,ParameterDirection.Input).Value = (int) Target.YearOffSet.TargetPlus1;
cmd.Parameters.Add("p_mp_id", OracleDbType.Int32,ParameterDirection.Input).Value = mpID;
cmd.Parameters.Add("p_person_id", OracleDbType.Int32,ParameterDirection.Input).Value = personId;
cmd.Parameters.Add(DBTool.GetOracleParameterByDbType("p_target", DbType.Double, ParameterDirection.Output));
DBTool.ExecuteStoredProc(cmd);
if (cmd.Parameters["p_target"].Value != System.DBNull.Value)
{
target = Convert.ToDouble(cmd.Parameters["p_target"].Value);
}

This is nice and tidy. Whether we should also be passing Input params by DbType is another matter but not wanting to interfere with existing code too much maybe go with this for now. Note passing a DbType also ensures that System.DBNull.Value works as expected too.

The helper method by the way is simply:

public static OracleParameter GetOracleParameterByDbType(string paramName, DbType dbType, ParameterDirection paramDirection)
{
OracleParameter NewParam = new OracleParameter();
NewParam.Direction = paramDirection;
NewParam.DbType = dbType;
NewParam.ParameterName = paramName;
return NewParam;
}