.Net ramblings
# Wednesday, 28 November 2007
Format a FAT32 partition above 32Gb
Windows has a pointless 32Gb sizelimitation on FAT32 partitions.
a very sound chap called Tom Thornill wrote a partition formatting utility that does not have this limit. full details on his site

Wednesday, 28 November 2007 15:04:31 (GMT Standard Time, UTC+00:00)  #    Comments [0]  General | Windows Server

# Tuesday, 27 November 2007
Deploy Crystal Reports for VS 2008 (RTM)
if, like me, you were running VS 2008 Beta 2 on a server and you had crystal reports working fine, and then recompiled your projects in VS 2008 RTM, you may get an error like this when you try and create a crystal report on a production server:
System.Runtime.InteropServices.COMException (0x800736B1):
Retrieving the COM class factory for component with CLSID
{5FF57840-5172-4482-9CA3-541C7878AE0F} failed due to the following error:
800736b1. at CrystalDecisions.CrystalReports.Engine.ReportDocument..cctor()
In my case, i had installed Crystal Reports Runtime for VS 2008 Beta 2, and i never installed the runtime for the RTM version.  it should be available on your VS machine, if you opted for CR in the VS installer:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5

should probably uninstall the old version of the CR runtime while you're at it too. 

Tuesday, 27 November 2007 11:32:12 (GMT Standard Time, UTC+00:00)  #    Comments [2]  .Net General | Asp.Net | Windows Server

# Monday, 19 November 2007
IFRAME causes all content beneath to disappear
how weird is this.  i upgraded my CMS editor to XHTML and i suddenly find that any pages with an IFRAME have the footer cut off, completely invisible.  i found out it was because the self-closing tag (as per XHTML) screws up the page layout in IE and FF.  you have to use </iframe>, which unfortunately breaks XHTML validation, but i can live with that.
for more info see this blog post that explains it further.

Monday, 19 November 2007 11:20:35 (GMT Standard Time, UTC+00:00)  #    Comments [0]  .Net General

# Friday, 16 November 2007
Running WSE 2 and 3 side by side

Note: this doesn't really work..

i'm just leaving it here for interest.  the approach will work for the first invocation of either v2 or v3 client, but after a v2 client has used the web service, any v3 clients will fail.  MS told me this is not supported because of the 2 soapExtensions interfere with each other in the pipeline etc

My asp.net CMS has some winforms clients running WSE2, and i'm doing an upgrade to the client (and server) to use WSE3, but i want to support both versions.  Both WSE2 and WSE3 clients are using a CustomUsernameTokenManager.  it was a bit tricky to work out, but here is what i ended up doing. thanks to brian o'keefe for his newsgroup post for most of the answer.  Your exact config might vary but hopefully it will save you some of the hassle of working all this out.
  • Leave the existing web service in place, e.g. Service_WSE2.asmx
  • Create a new web service for the WSE3 client, essentially copy/paste the ASMX file, e.g. Service_WSE3.asmx
  • Create a new CustomUsernameTokenManager for the WSE3 service which inherits from Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager.  In my case it was as easy as copy/paste from the WSE2 token manager and change all the WSE2 namespaces to WSE3.
  • then in web.config, make sure both WSE config sections are listed:
    <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3,Version=,Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  • add in references to both the assemblies for the compilation section (not 100% sure if this is necessary, but at least it means the app will not compile on the production server if you forget to deploy either of the WSE assemblies, instead of waiting till one of your clients connects).

    	<compilation defaultLanguage="c#" debug="true">
    <add assembly="Microsoft.Web.Services2, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="Microsoft.Web.Services3, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  • remove the <webServices> section from <system.web>, because each web service should be configured by location, not global as this interferes.

    	<location path="Service_WSE2.asmx">
    <add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />

    <location path="Service_WSE3.asmx">
    <soapServerProtocolFactory type="Microsoft.Web.Services3.WseProtocolFactory, Microsoft.Web.Services3, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <add type="Microsoft.Web.Services3.Description.WseExtensionImporter, Microsoft.Web.Services3, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  • my webservice policy are defined in policyCache.config files, so the following web.config sections point each version of WSE to the right file:
    <policy fileName="policyCache_WSE3.config"/>
    <add type="Whatever.CustomUsernameTokenManager3" namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" localName="UsernameToken"/>
    <securityTokenManager type="Whatever.CustomUsernameTokenManager2, MyAssemblyName" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    qname="wsse:UsernameToken" />
    <cache name="policyCache_WSE2.config" />
  • and lastly, the policy cache files themselves:
    WSE 3 version
    <?xml version="1.0"?>
    <policies xmlns="http://schemas.microsoft.com/wse/2005/06/policy">
    <extension name="usernameOverTransportSecurity" type="Microsoft.Web.Services3.Design.UsernameOverTransportAssertion, Microsoft.Web.Services3, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <extension name="requireActionHeader" type="Microsoft.Web.Services3.Design.RequireActionHeaderAssertion, Microsoft.Web.Services3, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <policy name="usernameTokenSecurity">
    <usernameOverTransportSecurity />
    <requireActionHeader />
  • WSE 2 version
    <?xml version="1.0"?>
    <policyDocument xmlns:wse="http://schemas.microsoft.com/wse/2003/06/Policy" xmlns="http://schemas.microsoft.com/wse/2003/06/Policy">
    <mappings xmlns:wse="http://schemas.microsoft.com/wse/2003/06/Policy">

    <endpoint uri="http://localhost/Service_WSE2.asmx">
    <request policy="#username-token-signed" />
    <response policy="" />
    <fault policy="" />

    <request policy="" />
    <response policy="" />
    <fault policy="" />

    <policies xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsp:Policy wsu:Id="username-token-signed" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wssp="http://schemas.xmlsoap.org/ws/2002/12/secext">
    <wsp:MessagePredicate wsp:Usage="wsp:Required" Dialect="http://schemas.xmlsoap.org/2002/12/wsse#part">
    wsp:Body() wsp:Header(wsa:To) wsp:Header(wsa:Action) wsp:Header(wsa:MessageID) wse:Timestamp()
    <wssp:Integrity wsp:Usage="wsp:Required">
    <SecurityToken xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext">
    <wssp:UsePassword wsp:Usage="wsp:Required" />
    <wssp:MessageParts Dialect="http://schemas.xmlsoap.org/2002/12/wsse#part">
    wsp:Body() wsp:Header(wsa:Action) wsp:Header(wsa:FaultTo) wsp:Header(wsa:From) wsp:Header(wsa:MessageID) wsp:Header(wsa:RelatesTo) wsp:Header(wsa:ReplyTo) wsp:Header(wsa:To) wse:Timestamp()

Friday, 16 November 2007 12:16:50 (GMT Standard Time, UTC+00:00)  #    Comments [1]  .Net General | .Net Windows Forms | Asp.Net

# Tuesday, 09 October 2007
MultiThreading In CF2 with Delegates
what a frustrating problem this was.  delegates weren't support in CF1, but now we have CF2 and according to microsoft:

The .NET Compact Framework 2.0 now supports asynchronous execution of a delegate on a user interface thread. Unlike the .NET Compact Framework 1.0 that supports only the Control.Invoke method (which synchronously executes a delegate on a control's owning thread), the .NET Compact Framework 2.0 provides Control.BeginInvoke that asynchronously executes a delegate on the control's owning thread. The Control.EndInvoke method is also provided. When called, the EndInvoke method returns the results of an asynchronous operation.

This is great but it's easily assumed that delegates are fully supported now in CF2, but no such luck.  they don't say that calling BeginInvoke on a delegate will cause your application to crash (with a NotSupportedException) even though it will compile in visual studio without warning or error.  there is an excellent (if lengthy) discussion on the matter on this newsgroup post.  the long and short of it is that you should use the ThreadPool class instead of using delegates to fire off worker threads.  You can still use delegates to send a function back to the UI thread just like in winforms.  Notice in the code below that the method signature of the function you are starting the thread on, requires a single 'object' parameter.
using System.Threading;

private void Button_Click(etc)
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomeWork));

private void DoSomeWork(object o)

Tuesday, 09 October 2007 15:00:03 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net Compact

# Monday, 08 October 2007
Look After Your Eyes!
i went to the optician yesterday for an annual check up, i'm always a bit worried because i write code for most of the day and that's not very healthy for eyes etc etc.  i know you're supposed to take breaks every 15 mins or so but that's difficult to do in practice.
the test showed one of my eyes with a very slight deterioration which makes no difference really but still i found it quite alarming that i'm doing damage to my eyes, specially since you only get 2 for your whole life.  i dug out a program i wrote a few years ago to go 'ding' in the background every 10 minutes to remind you to take an eye break.  apparently relaxing your eyes on a distant object for a minute or two is a good idea etc.  i was going to post up my little exe file but then i went looking and found "TakeYourBreak" on download.com, it is a much better program!

Monday, 08 October 2007 15:44:00 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  General

# Tuesday, 02 October 2007
Bug in ComboBox SelectedIndex
big bug in ComboBox control here: http://support.microsoft.com/default.aspx?scid=kb;en-us;327244
you have to set the SelectedIndex to -1 twice in a row if you want it to take effect!
i came across this in a compact framework app, but it looks like it applies to win-forms as well.

Tuesday, 02 October 2007 17:17:18 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net Compact | .Net Windows Forms

# Tuesday, 18 September 2007
Profiler For SQL 2005 Express
wow, i never knew this existed: http://sqlprofiler.googlepages.com/
thanks to nikolay.zhebrun
it works great.

Tuesday, 18 September 2007 13:43:58 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net General | Database

LINQ Sql Using Skip And Take
Here is the sql that is generated by a DataContext when you use Skip() and Take() to efficiently select records for the grid:
SELECT TOP 10 [t1].[Name], [t1].[Address], [t1].[Tel1], [t1].[Tel2], [t1].[Email], [t1].[DateCommenced], [t1].[Comments], [t1].[Active], [t1].[Fax]
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[Name], [t0].[Address], [t0].[Tel1], [t0].[Tel2], [t0].[Email], [t0].[DateCommenced], [t0].[Comments], [t0].[Active], [t0].[Fax]) AS [ROW_NUMBER], [t0].[Name], [t0].[Address], [t0].[Tel1], [t0].[Tel2], [t0].[Email], [t0].[DateCommenced], [t0].[Comments], [t0].[Active], [t0].[Fax]
FROM [dbo].[Table1] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] > @p0
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [50]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

it's not immediately obvious why they use the subquery like this, but i'm sure they have been very thorough in optimising LINQ. 

Tuesday, 18 September 2007 13:22:32 (GMT Daylight Time, UTC+01:00)  #    Comments [1]  .Net General | Asp.Net | Database

# Monday, 17 September 2007
LINQ with strings
Today was the first time i used LINQ with non-database data.  I always knew you could do it but didn't realise how satisfying it would actually be to use LINQ to crunch out some results that i would otherwise have to code up in some gnarly routine involving on-the-fly DataTables and DataView filters etc.  Also, I'm mainly posting this because i can never remember the LINQ group by syntax...

i have a plain list of reference numbers, e.g. 1.a, 2.b, 3.c, 4.a, 10.d, 11.c etc.  they contain duplicates and i was asked to figure out the top 10 most popular references.  A simple regex separates out the numbers, i could also have used string.split() but regex is better in my case.  Copy each one into an array and then a simple LINQ query on the array gives me the top 10 references:
List<string> lst = new List<string>();
foreach(Match m in Regex.Matches(this.txtInput.Text, @"\w+\.\w+", RegexOptions.IgnoreCase))
var results = (from l in lst group l by l into g select new {Ref = g.Key, Total = g.Count()}).OrderByDescending(z => z.Total).Take(10);
foreach(var v in results)
this.txtOutput.Text += v.Ref + "\t" + v.Total + "\r\n";

Monday, 17 September 2007 15:01:26 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net General