7/24/2008
LifeCycle Solutions - Home ( the software development blog )
 

<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Subscribe to this feed:

RSS 2.0 | Atom 1.0 |CDF





Add to Technorati Favorites

Wednesday, October 17, 2007

One technique we've started using is automating our apps' configuration using Pre-Build Events and Build Configurations in Visual Studio.  It takes a little more work to set up, but once you do, publishing test and production apps is much easier.  It also helps the developer to think not just about a "works on my machine" app, but an app that works on any number of scenarios.  Without this approach, the developer still has to keep up with multiple config files, but typically does so in their head. Scott Guthrie has a great post showing how all this is done.  However, a few tips will help you fine tune your automation, so that development is even more painless:

  • Decide the different scenarios your app will run in.  In our case there are typically 3:
    • Local - App is being developed and running on the developers' PC
    • Staging - App is being tested in a copy of the production environment
    • Production - App is "live".
  • Create a folder named Configuration.
  • Create a .config file for each scenario above.
    • Name the .config file scenario.web.config or scenario.app.config (ie local.web.config).  Using this approach over Scott's means you still get intellisense in the .config files!
    • If you have an existing .config file, just copy it to the Configuration folder and rename it as above.  Remember that after this is set up, the "real" .config file will be overwritten.  If you put blank .config files in Configuration, you'll loose the "real" configuration file.
  • Create Build Configurations in Configuration Manager for each scenario above. 
    • In each configuration, add the following Pre-Build event
    • IF EXIST "$(ProjectDir)Configuration\$(ConfigurationName).web.config" xcopy "$(ProjectDir)Configuration\$(ConfigurationName).web.config" "$(ProjectDir)\web.config" /Y /R

    • This script accomodates the naming schema above, and works with source control by overwritting the read-only .config file. Note if your project is a windows forms, console, or class library app, change "web.config" to "app.config"

And that's it - before each build, the appropriate config file will be copied over and used.  Note that you'll never want to change the .config file in your project's root, as it will be overwritten.  Instead, whenever you need to change configuration, decide how the change will affect each scenario, and change each .config in your Configuration folder.

Posted by Daniel Root

Monday, April 02, 2007

In the slides from Scott Guthrie's 'ASP.NET Tips and Tricks' TechEd Presentation, I ran across this gem.  You can turn off debugging server-wide by adding the following to <system.web> section in the server's machine.config file (typically located in C:\windows\Microsoft.NET\Framework\v<version>\Config):

       <deployment retail="true"/>

This is a great backup in case developers forget to turn it off in the application using <compilation debug="false"/>.  Scott also mentions a few of the negative results of forgetting this little setting:

  • Slower performance due to debug code
  • App uses much more memory
  • Client-side scripts are not cached
Posted by Daniel Root

Tuesday, February 20, 2007

One of the most useful tools for .NET development is Lutz Roeder's .NET Reflector, now in version 5.  It uses reflection to peek inside .NET assemblies and disassembles the IL back into C# or VB code for those times that you need an in-depth look into any .NET library.  Ever wonder what the code for System.String looked like? This release adds some really nice features:

  • Formatted code comments- doubles as a documentation browser for XML code-commented members
  • 'Expand members' in disassembler lets you see code for an entire class at once- previously you had to look at each method separately.
  • New analyzer features shows where classes are instantiated and exposed.
  • Search Google or MSDN for a member

In addition to helping you debug code, this is also a great learning tool.  What better way to understand framework development than to peek inside The Framework itself?

 

 

Posted by Daniel Root

Wednesday, August 23, 2006

Ever wondered what a secure ASP.NET 2.0 application should look like?  The ASP.NET Internet Security Reference Implementation rolls all of the Patterns and Practices Security Guidance into real-world sample application, complete with full documentation about all of the security features, why and how they were implemented, and the drawbacks to doing so.

If you are doing ASP.NET 1.1 or 2.0 web design, you should download this, read through it, and check out the code.  While the code is 2.0, all of the security concepts- and some of the solutions- map to 1.1 apps as well.

One note: it installs into C:\Program Files\Microsoft\Internet Security Reference Implementation by default.  It took me forever to find it!

Posted by Daniel Root

Thursday, August 17, 2006

I recently data bound to a DateTime property in ASP.NET 2.0, and couldn't figure out why the formatting wasn't working properly.  I set the DataFormatString property to {0:d}, which should change the column to short date format.  But it wasn't formatting at all.  After a little digging, I found that it was because HtmlEncode wasn't set to false.  Setting HtmlEncode="false" on the column fixed the problem. 

Seems a bit odd at first, but the purpose of HtmlEncode is to prevent cross-site-script attacks.  Here's what's happening:

  • The property's value is retrieved.
  • ASP.NET converts the value to a string and formats it to remove any HTML.  For example a '<' gets changed to '&lt;'.  So, if someone were to somehow add the value <script>doSomethingTricksy()</script> to your database, it wouldn't get run here...
  • ASP.NET applies the DataFormatString, but by now the date is already a string. 
  • Formatting the string "1/1/2005 12:00:00 PM" doesn't do anything.

Since we know the type is a date, and it would be difficult to insert HTML into the date, then it's pretty safe to turn off this feature for this column.  Microsoft has a little note on this issue here.

I say pretty safe, because it's actually still possible that script or other HTML could be inserted into this column.  A recent post by Scott Hanselman may give you a clue.  Suppose you let users define their own DateTime formatting preferences, and did something really evil, like this:

customCulture.DateTimeFormat.ShortDatePattern = Request("format").ToString()
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture

And then suppose some crafty hacker sent a link to  yourpage.aspx?format=<script>doSomethingTricksy()</script>MM/dd/yyyy.  Any time you databound to a DateTime, if HtmlEncode was false, then the script would be run! Once somebody gets that far, they can do any number of bad things.

Granted, it's a pretty low threat, but it's worth knowing about.

Posted by Daniel Root

Tuesday, August 15, 2006

The Portable Network Graphics (PNG) format is a great image format.  It's file sizes are typically much smaller than GIF or JPG, and it supports alpha transparency.  So, you can have a transparent background on your image, and it will look correct on any page.  With GIF or JPG, a designer would need to carefully adjust the image background to match the page background.  The only problem with PNG, is that IE doesn't support it natively!  Instead of rendering the transparent areas correctly, they are displayed as opaque grey areas.   Fortunately, IE 7 supposedly fixes that, and there are workarounds for IE 5.5 and 6.  Bob Osola has a nice explanation and a javascript fix.

Suppose we wanted to package this solution in a way that makes it easy for ASP.NET developers to use, or we have custom controls that will rely on this fix.  Currently, they would have to keep up with the client script, and reference it from each page in each application they wrote.  Using Web Resources and Client Script Management in ASP.NET 2.0, we can greatly simplify reuse of this script.

Solution Overview
The fix itself is a little javascript that must be run on every page that requires PNG support.  It works by looping through all the elements on a page and, if it's an <img> element with a .png image, applying the CSS filter to fix PNG in IE.  We've modified it slightly to do the same for <input type="image"> elements, such as those rendered by the DataGridView's command button columns.  As is, the developer must add the script to their application, and add this line to the <head> element of each page that needs PNG transparency:

<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

But, we want the solution to be easy for developers to use.  From the application developer's perspective, we want the expirience to be:
  • Add a reference to MyCompany.Web.dll
  • Add the line MyCompany.Web.UI.ClientScriptManager.EnablePNGSupport(...) to a control, page, or master page's OnLoad event.
Setting it up
This tells us that we'll need a MyCompany.Web Class Library Project, and a class called 'ClientScriptManager'.  So in VS05, create a project named MyCompany.Web.  For various reasons, it's also a good idea to set the default namespace to empty here.  This will let you set the namespace inside your class, and will make using Web Resources easier.  To do this:
  • Rt Click the project
  • Choose Properties
  • Choose the Application Tab
  • Empty the 'Default Namespace' field.
Embedding Resources
Next, create a folder named Resources, and add the pngfix.js file to it.  To use this javascript file from web applications, it needs to be embedded in the assembly and marked as a web resource.  Follow these steps to do this:
  • Right click the script file and choose 'Properties'
  • Set Build Action to 'Embedded Resource'.
  • Go to the Solution Explorer and click 'Show All Files'
  • Expand the 'My Project' item
  • Open AssemblyInfo.vb
  • Add this line to the bottom: <Assembly: WebResource("pngfix.js", "text/javascript")>
  • Note: If you didn't set the default namespace to empty as above, you'll need to include it here  (ie: "MyCompany.Web.pngfix.js")
To recap, this just told the compiler to take the pngfix.js file and stuff it into the .dll when it's compiled, and mark it as usable by web applications.  This fix also requires a blank image file to work correctly.  Do the same as above for the file blank.gif.

Getting Resources at the Client
The last step is to create a method that will allow developers to easily use this javascript.  This method needs to add a script to the page to set the path to blank.gif, and a script to run the fix in pngfix.js.  But remember that file has been embedded in a dll, so that our developers don't have to keep up with it. This is where Web Resources come in.  To get the content that was embedded into the dll, ASP.NET 2.0 gives us a method named GetWebResourceUrl(...).  When called, this method returns a URL to a special path that looks something like this:

/WebResource.axd?d=rN9s8cQhdsLqV...

This location actually maps to an HttpHandler built in to ASP.NET 2.0, which will do the work of pulling the embedded resource out of our assembly, and returning it to the client.  So, to get the embedded resource- be it a script, image, or any other file, all we need to do is browse to the url provided by GetWebResourceUrl.  But, your developers shouldn't have to worry about all that.

The code below creates a class with a shared method for registering the two scripts needed for this fix to work.  It first checks to see if the browser is IE 5.5 or 6.  Next, it checks to be sure the script isn't already registered, since adding it twice would cause errors. Finally, it registers the scripts.

Imports System.Web.UI.WebControls

Imports System.Web.UI

Imports System.Web

 

 

Namespace MyCompany.Web.UI

    ''' <summary>

    ''' Class for managing client side scripts frequently used by MyCompany web applications.

    ''' </summary>

    ''' <remarks></remarks>

    Public Class ClientScriptManager

 

        ''' <summary>

        ''' Adds client script to add PNG transparency support to Internet Explorer 6.0 and lower browsers,

        ''' if it has not already been added by a previous call to this method.

        ''' </summary>

        ''' <param name="page">The page.</param>

        ''' <remarks>

        ''' <para>IE 6 and lower do not properly display PNGs with transparent backgrounds.

        ''' This method puts a javascript include tag in the page's &lt;head&gt; declaration

        ''' if the browser is IE 6 or 5.5. This fix is based on the one

        ''' found here: http://homepage.ntlworld.com/bobosola

        ''' </para>

        ''' <para>

        ''' Typically this would be done using Page.ClientScript, however that method does

        ''' not support the 'defer' attribute, which is required in this case.

        ''' </para>

        ''' <para>

        ''' To use, add the following to your Page or MasterPage:

        ''' <code>       

        '''   Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        '''    MyCompany.Web.UI.ClientScriptManager.AddPNGSupport(Me.Page)

        '''   End Sub

        ''' </code>

        ''' </para>

        ''' </remarks>

        Public Shared Sub EnsurePNGSupport(ByVal page As System.Web.UI.Page)

            If HttpContext.Current.Request.Browser.Browser = "IE" Then

                Dim Version As Double = CDbl(HttpContext.Current.Request.Browser.Version)

                If Version < 7.0 And Version > 5.5 Then

                    Dim pageHeader As System.Web.UI.HtmlControls.HtmlHead = page.Header

 

                    If Not pageHeader.Page.ClientScript.IsClientScriptBlockRegistered( "SetBlankImageUrl") Then

                        Dim blankImageScript As String = String.Format("<script type=""text/javascript"">var blankImageUrl = '{0}'</script>", pageHeader.Page.ClientScript.GetWebResourceUrl( GetType(MyCompany.Web.UI.ClientScriptManager), "blank.gif"))

                        pageHeader.Page.ClientScript.RegisterClientScriptBlock( pageHeader.Page.GetType(), "SetBlankImageUrl", blankImageScript)

                    End If

 

                    If Not ContainsControlWithId(pageHeader.Controls, "pngFixBlock") Then

                        Dim scriptBlock As New LiteralControl(String.Format("<script defer type=""text/javascript"" src=""{0}""></script>", pageHeader.Page.ClientScript.GetWebResourceUrl( GetType(MyCompany.Web.UI.ClientScriptManager), "pngfix.js")))

                        scriptBlock.ID = "pngFixBlock"

 

                        pageHeader.Controls.Add(scriptBlock)

                    End If

 

                End If

            End If

        End Sub

 

        ''' <summary>

        ''' Gets a value indicating whether a control with the given id already exists in a collection.

        ''' </summary>

        ''' <param name="controls"></param>

        ''' <param name="id"></param>

        ''' <returns></returns>

        ''' <remarks></remarks>

        Private Shared Function ContainsControlWithId(ByVal controls As ControlCollection, ByVal id As String) As Boolean

            For Each control As Control In controls

                If control.ID = id Then Return True

            Next

            Return False

        End Function

    End Class

End Namespace


Copy the above, and put in a class called 'ClientScriptManager.vb.'

Using the Fix
Build, and if everything worked correctly, your developers can reference the assembly from their projects, and fix PNG transparency with a single line of code in their page's OnLoad event:

MyCompany.Web.UI.ClientScriptManager.EnsurePNGSupport(Me)

An even better option would be to put this in a master page OnLoad event:

MyCompany.Web.UI.ClientScriptManager.EnsurePNGSupport(Me.Page)

Finally, it's worth noting that this would also work for custom controls.  You can use the same technique to embed a png to be used by, say, the ImageUrl property of your control.   The only gotcha is that the script looks for the .png extension to determine if it needs to fix an element, but the URL returned by GetWebResourceUrl doesn't end in '.png'.  To fix this, simply add the extension to the query string:

If Not String.IsNullOrEmpty(Me.ImageUrl) Then

   printLink.ImageUrl = ImageUrl
ElseIf Me.DesignMode Then

   printLink.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "printer48.png")

Else

   printLink.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "printer48.png") + "&.png"

End If

Files for this post:
pngfix.js (2.19 KB)
blank.gif (.04 KB)


Posted by Daniel Root

Sunday, August 06, 2006

Microsoft is cranking out ASP.NET 2.0 how-to videos at a record pace.  Brian Goldfarb lists several new ones here. Scott Guthrie lists them also, and includes a few others here, including one on Microsoft's AJAXy toolkit, Atlas.

Posted by Daniel Root

Friday, August 04, 2006

Online labs are Microsoft's newest way to learn about developer tools and techniques.  These guided tutorials give you a real, live virtual PC pre-installed with everything you need- Visual Studio 05, .NET 2.0, etc.  Just sign up and use the web-based remote desktop client to connect.  There's a list of good .NET 2.0 Labs over at WWWCoder.

Posted by Daniel Root

Thursday, August 03, 2006

Here is a nice short video on setting up security for a folder in ASP.NET 2.0.  This was possible in 1.1, but 2.0 makes it even easier- no code required to secure your site by roles!

Posted by Daniel Root

Tuesday, July 25, 2006

The introduction of the DataSource model in .NET 2.0 has made it much easier to perform data binding in ASP.NET.  In most cases, it's trivial to use the SqlDataSource and ObjectDataSource for purposes of binding and updating the backing data store when a single row is changed.  However, there's no out-of-the-box way to use a DataSet itself as the backing data store and later persist its entire batch of data to the database.  Consider a shopping cart scenario in which you are adding multiple rows to a temporary data store...you don't necessarily want to go to the DB each time a row is added; rather, you'd prefer to perform a batch update at the end of the process.

Andres Aguiar presents a good solution to the problem by creating a custom data source for this purpose -- the DataSetDataSource.

Posted by Brian Parks

Thursday, July 20, 2006

There is an excellent collection of tutorial videos on the Official ASP.NET 2.0 web site.  While they are probably most beneficial for developers seeking to learn about the new features in 2.0, they should also be useful for those moving to ASP.NET 2.0 from other environments, like traditional ASP or PHP.

Posted by Brian Parks

© 2006 LifeCycle Solutions, LLC | All Rights Reserved