Tuesday, 22 May 2007

Using code in your blogs (blogger.com)

Not sure if everyone is aware of this, but the easiest way to blog code snippets in html on blogger.com is to put the code between <pre> tags, this applies a coding style and preserves whitespace (so you don't have to use &nbsp;). So to get this:
public void Go
{
   // TODO : Do some work    
}
Blog with the following html:
<pre>
public void Go
{
   // TODO : Do some work    
}
</pre>
Remember if you are using <tags> you need to use &lt; as the opening tag bracket otherwise the tag will be omitted (because the browser tries to parse it). Happy blogging! :)

Cannot create/shadow copy when that file already exists

The .Net Framework has a feature called Shadow Copy. Shadow copy is enabled on every appdomain created by ASP.NET by default. By default assemblies loaded will be copied to a shadow copy cache directory, and will be used from that location. Why does ASP.Net do this? So the original file is not locked and can be modified. An interesting error I have ran into intermittently when running ASP.Net apps with the debugger is 'Cannot create/shadow copy when that file already exists' I'm still not sure why this happens only on occasion, as I can often clean my solution, then reload a web app and it will work fine then. But if you are getting it frequently enough for it to affect your work, you can add the following to your app's web.config or the master .Net web.config:
<system.web>
   <hostingEnvironment shadowCopyBinAssemblies="false" />
</system.web>

Tuesday, 15 May 2007

Storing ViewState into Session

HTTP requests and responses are inherently stateless. To maintain state between HTTP requests, ASP.NET server pages can store Page state. This state, called ViewState, consists of page and control settings and data that make the page and controls appear as if they are the same ones that the user saw and interacted with on their last round trip to the page. Why would you want to store ViewState into session? 1)You have a page using lots of panels/controls resulting in a large viewstate, which is affecting perforrmace during postbacks. 2)You are serving pages to small devices that have limited client-side resources or use a markup language that does not support a hidden field element. Several mechanisms exist to store ViewState between successive requests to the same page, the abstract PageStatePersister class represents the base class for these state storage mechanisms in .Net 2. By default HiddenFieldPageStatePersister is used to keep ViewState in a hidden input element, __VIEWSTATE, which is way .Net 1/1.1 worked. To use a different mechanism, such as the bundled SessionPageStatePersister (which stores page state into session), just override the System.Web.UI.Page PageStatePersister property:
protected override PageStatePersister PageStatePersister
{
   get
   {
      return new SessionPageStatePersister(this);
   }
}
More on ViewState It is worth mentioning that you may want to look into using PageAdapters instead of overriding the System.Web.UI.Page PageStatePersister property, the MSDN PageAdapter page will get you started :) A good ViewState page that explains everything clearly is on Dave Reed's blog site. A handy ViewState decoder tool by Vasudevan Deepak Kumar. Technorati Profile

Friday, 11 May 2007

Get going with AJAX in .Net

I started to look into AJAX in 2006 and then didn't get a chance play with the AJAX technologies properly until today. I noticed that there had been some updates to the original AJAX toolkit so I decided to remove everything AJAX and start again from scratch - here's some easy steps to get you going. (Downloads can be found @ ASP.net AJAX site) Step 1) Install AJAX Extensions 1.0 This download installs the framework for developing and running AJAX-style applications with either server-centric or client-centric development models and is fully supported by Microsoft. Step 2) Install ASP.NET AJAX Control Toolkit The ASP.NET AJAX Control Toolkit is a shared-source community project consisting of samples and components that make it easier than ever to work with AJAX-enabled controls and extenders. The source download includes a great sample site. So download the source zip file and extract the contents to somewhere logical, I placed mine with the extensions files from step 1 (Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\AJAXControlToolKit). If you open the solution and view the sample web site it contains a wealth of info on the controls available with great examples. Step 3) Setup Visual Studio for easy access The best way to start playing is to open visual studio, add a new AJAX enabled web application. Because the control toolkit isn't installed with an msi (installer) but is just an assembly, the tools won't appear automatically on your toolbox like the fundamental AJAX items. So right click and add tab for the AJAX controls, call it something like AJAX Controls Toolkit. Add the controls to the tab by right clicking the tab, click choose items, then browse to the AJAX Controls Toolkit Assembly. Step 4) Go wild!!! You can now go wild adding the AJAX controls and extenders. Go to the Asp.net Ajax site for some get getting started guides.

Thursday, 10 May 2007

Using Bind Monitor (FUSLOGVW.exe)

Don't you just hate it when you've done all the development work and you are finally deploying the release onto a server and BANG! It doesn't work, you have some sort of assembly loading exception/file not found exception?

All of us (yes even you) have had assembly binding issues on our server and development machines, but not many of us know about or make use of the Bind Monitor (FUSLOGVW.exe) bundled in the .Net SDK. The tool is useful because depending on the options selected, it can log binds occurring between assemblies - including bind failures and their associated details.

The Bind monitor can be found at \Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\FUSLOGVW.exe so it is useful to add this as an external tool using Tools > External Tools. Now you can access it easily in Visual Studio at Tools > Bind Monitor OR if you are trying to find out the issue on a server machine without the developer environment installed then just copy the file from a development machine (\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\FUSLOGVW.exe) to the server.

Tips for Bind Monitor < V2

Firstly click on Log Failures. Next, if this is an ASP.NET or .NET Windows service app, select the Custom Option and using regedit, set HKLM\Software\Microsoft\Fusion\LogPath to point to a directory that exists (c:\temp\binds\?). To log all binds, not just failures, set HKLM\Software\Microsoft\Fusion\ForceLog as a DWORD value to 1.

Tips for Bind Monitor V2

You can sometimes get an issue when not using Enable custom log path (the default locations is the internet explorer Temporary Files cache). So I usually turn that on and point it at a log/temp directory.

To log bind failures for satellite assemblies, you need to do a little fix by adding a HKLM\Software\Microsoft\Fusion\LogResourceBinds registry value to 1 (the value is a DWORD) - this also seems to fix some other problems, so I strongly recommend it.

More Info?

The MSDN page for the Bind Monitor can be found here Bind Monitor - FUSLOGVW.exe.

Fusion is the technology used for loading assemblies in .Net and there is a great site with a Fusion Workshop covering all aspects of the technology.

There is a great wiki page on .NET Assemblies which is useful for anyone that has some queries on assembly basics and is too scared to ask?!