2006-12-19

Users Changing ASP.NET Theme

Man i really like codeproject, its articles are awesome.

Is DTS dead?

I was writting some scripts to move/copy databases for my releasing program for mssql 2000, but then couldnt find that many up-to-date resources. It seems there is a new tool for 2005 called SSIS.
http://msdn2.microsoft.com/en-us/library/ms141026.aspx

But i did find some links about how to create a basic DTS file, possibly that could be run from the command line.
http://www.15seconds.com/Issue/030909.htm

And then what files i would need on the running server
http://www.sqldts.com/default.aspx?225

Webservice Envelope Validation

How do you do that, this article explains.

I have got a extension that just logs soap packets, so its not too hard to plug the validator into that.

ASP.NET 1.1 and 2.0 Working Together

Is this possible on a shared provider, i dont think so, but its good for self managed.

SVN Externals No Good

After much work using svn externals for internal class libraries i have come to the realisation that they should not be used for this.

http://www.brunningonline.net/simon/blog/archives/002031.html
http://svn.haxx.se/dev/archive-2003-07/1617.shtml

A few other people tend to agree, or have found the smae problems that i have.
Its just a nightmare to try and manage them.
I'm actually starting to think that no software configuration information should be stored in the svn repository and that should be maintain by an outside program.

More Automated Software Process

I have been using cc.net for a while and its great, but then when i want to do more things, like scripted releases / packages / branching, i dont have a complete tool.
There is maven and lunt build, but both are written in java. And i think they are more similar to cc.net then the linked tool.
If i can't find one written in .net, i might have to build one. A little bit of effort here saves both time and hassel in the future thats for sure.

Server backgrounds

You know how you can have the windows build number and OS version appear on your desktop with a reg setting, what do you do if you want more details. You use this tool, it is awesome on servers where you can then just look at the desktop to see what machine you have connected to. Especially when you are connected to more than one at a time.

SQL XML Not Required

I was under the impression that you had to have the sqlxml component installed to use the feature where xml is returned from a t-sql query. I was using the connection string like this:

var objConn = new ActiveXObject("ADODB.Connection");
//Provider=SQLXMLOLEDB.3.0;Data Provider=SQLOLEDB
var objComm = new ActiveXObject("ADODB.Command")
objComm.ActiveConnection = objConn
objComm.CommandText = tSQL
var objStream = new ActiveXObject("ADODB.Stream")
objStream.Open()
objComm.Properties("Output Stream") = objStream
objComm.Execute(null, null, 0x400)
objStream.Position = 0

This turns out not to be the case, you can use this as the connection string:
//Provider=SQLOLEDB;Data Provider=SQLOLEDB
And then you dont need to install the extra component, good for shared hosting!

Handling Nulls in Webservices

I think i will start using this blog to remember what i have learn and so i can always come back to it if i forget about it.

So this was where if nulls are passed into a .net 2.0 webservice, then the elements are removed from the soap envelope that is sent.

2006-10-08

Accessing Session in HttpApplication

This is an interesting thing, when trying to access the Session object in your global class like this:

public class global : System.Web.HttpApplication {

    void Application_PreRequestHandlerExecute(object sender, EventArgs e) {

        if(User.Identity.IsAuthenticated) {
            if(Session != null) {

            }

            if(Context.Session != null) {

            }
        }
    }
}


The first access to the Session throws this exception:

Session state is not available in this context.

But the second one succeeds, i assume that they would both be accessing the same underlying object. Weird.

2006-09-27

Spring.Net Schema

Its easy to get intellisense for your spring object files, as resources, by copying the spring-objects.xsd file into %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas.

2006-09-25

Mem Usage vs VM Size

When viewing my task manager, I always make sure that the VM Size column is turned on. This shows the actual memory footprint that the process has, rather than what is currently taken. For instance, if you minimise an application, usually the Mem Usage column will drop, but the VM Size doesnt. Though it didnt matter with my memory leak program, both the numbers filled all my available ram.

Design Time Master Page

Robin provides a neat solution to design time masterpage pattern. Although Server.Transfer and Server.Redirect wont work, but that should be fixed in IIS 7.

GC.Collect not the solution

Many times i have come to situations where the memory usage in my program spirals out of control. Always i think it is a problem with anything but my program. I test the cleanups of the 3rd party libraries, and every other little thing. Then i resort to put GC.Collect statements around, thinky that it will magically free memory.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Then i go searching for how to debug these things:
http://blogs.msdn.com/tess/archive/2006/09/06/742568.aspx
http://blogs.msdn.com/tess/archive/2006/08/11/695268.aspx

Finally i get stuck into reviewing my code with a fine tooth comb, and find one thing that was causing the leak, static array on a singleton, used in temporary processing, that didnt get cleared.

2006-09-23

HttpContext Items Strongly Typed

I have just refactored my nHibernate session store in HttpContext.Items to use this as the base class. This fits perfectly with the spring wiring and means that i can also use it in other projects, for other items that i want strongly typed access to the object. I forgot the dispose method so i was having bad memory leaks. See also the default(T) keyword, interesting.

    public abstract class AbstractObjectCacher<T> {
        #region IoC
        private IObjectCache<T> cache;
        public IObjectCache<T> Cache {
            set { cache = value; }
            get { return cache; }
        }
        #endregion
        public T Get() {
            return cache.Get();
        }
        public void Set(T obj) {
            cache.Set(obj);
        }
        public void Dispose() {
            T obj = Get();
            if(obj != null) {
                Set(default(T));
                if(obj is IDisposable) {
                    ((IDisposable)obj).Dispose();
                }
                obj = default(T);
            }
            cache.Clear();
        }
    }

So for instance, the thread store i use for unit testing will look like this:

    public class ThreadObjectCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set {
                key = value;
                ThreadStore.Initialise(key);
            }
            get { return key; }
        }
        #endregion
        public T Get() {
            return (T)ThreadStore.Get(key);
        }
        public void Set(T obj) {
            ThreadStore.Set(key, obj);
        }
        public void Clear() {
            ThreadStore.Set(key, null);
        }
        public void Dispose() {
            ThreadStore.Dispose(key);
        }
    }

And the HttpContext store will look like this:

    public class HttpContextCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set { key = value; }
            get { return key; }
        }
        #endregion
public T Get() {
            return (T)HttpContext.Current.Items[key];
        }
        public void Set(T obj) {
            HttpContext.Current.Items[key] = obj;
        }
        public void Clear() {
            HttpContext.Current.Items[key] = null;
        }
        public void Dispose() {
            HttpContext.Current.Items.Remove(key);
        }
    }

2006-09-20

Changes are not allowed while code is running

How often have you got this message when you try to edit your source while debugging? "Changes are not allowed while code is running"


Notice the lock icon next to the filename, showing its read only.

This is a feature of VS that allows you to change the code and continue running with the modified code.
It seems to be only useful for desktop applications, not web apps that we develop.
We know that if we change code in the binary then we will need to recompile anyway, so how do we turn off this annoying message.
To remove the message, turn off Edit and Continue by:
Going to options

Go to Debugging, Edit and Continue and uncheck the top box



By turning off Edit and Continue, you still can create and remove debug points why the process is paused.
And you can still edit aspx’s, ascx’s, and their code besides (in the web application) without problems.

db4o 5.7

A new developement release of db4o has been released, which is supposed to be much faster than the previous versions. 5.7 is a couple of releases ahead of my current version 5.4.8 and the object browser has been updated too. This weekend if i get a chance i will work on intergrating this into the repository wrapper.

2006-09-19

Updating Atlas

After updating the AtlasControlToolkit to the latest version 1.0.60914.0, i ran into a bunch of problems. The first was the removal of the third AtlasControlExtenderer binary, whichwas easy to fix, just had to update references to the AtlasControlToolkit and namespaces.
The other issue i had was i was gettting a debugger alert client side about
PageRequestManagerID not being an attribute on draggablelistitem. This is a custom drag item i have implemented, so i just went digging through AtlasControlToolkit and found something similar to what i wanted to do DragPanelProperties and it has go this code:

        [EditorBrowsable(EditorBrowsableState.Never), 
  Browsable(false), 
  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PageRequestManagerID {
            get { return null; }
            set { throw new NotImplementedException(); }
        }


So i just copied that into mine, and things worked. Its weird that there is no need to have the "new" keyword on this method. And why didnt it work in the first place.

ASX Partition Table

Once upon a time, i had a application that graphed asx data (australian stock exchange). It got imported from some csv files in sql server. For performance reasons i had each code as a seperate table. But that was before i knew about indexes.
Now there may be a better way, by using Partitioning. I will be able to have the same tables, and then a view on top of them that i can query. The partitioning will happen on the stock code. With sql server 2005 this will happen automatically. Nice.

http://weblogs.sqlteam.com/dmauri/archive/2005/07/18/7147.aspx
http://searchtechtarget.techtarget.com/originalContent/0,289142,sid91_gci1207038,00.html
http://www.google.com/search?hl=en&lr=&q=partition+mssql+2005

Unicode Kanji Characters

This table is awesome, if i need to match a character i do a copy on the page, then find on this one. Then get the number and convert it to &#123

Javascript Triple Equals (===)

I download Script# today, and had a browse through the script files, and in the first couple of lines was this triple equal operation (===). Now i have been writing javascript for nearly 5 years and hadn't ever seen this.

http://www.flashinsider.com/2005/07/25/null-equals-undefined-but-null-doesn-t-strictly-equal-undefined/
http://blogs.msdn.com/ericlippert/archive/2004/07/26/197302.aspx
http://www.ejball.com/EdAtWork/PermaLink.aspx?guid=1a946f91-d37d-4104-a1d3-63e633afafba

So it does a comparison of the value, including the datatype. I don't know if i will ever use i, considering i have never used it, but it might come in handy later.

Output Stream Filtering

I was debating today whether to use an output filter to replace special macros in the my site templates at runtime. Apart from the issues outlined in this article, where the filters dont run if End() is called and in some transfer and redirect methods, the process wouldn't work if i was using the visual studio design editor. Say for example that i was replacing the application root path with a macro like [[AppRoot]].
My prefered method would be to have a helper static class that references HttpContext.Current in runtime and something else a design time. Then use <%= HtmlHelper.AppRoot %> in templates instead.
Remember to wrapper the script tags in an asp:PlaceHolder if you get the can't add to controls collection error.

2006-09-18

Programming Index

So how does your programming language rank in terms of this index...

Delete All Rows

Many a time i have wanted to delete all the rows from all the tables in the database. It usually involves a script getting all the names of the tables and deleting one at a time. This script is awesome where you can do it in one line:

EXEC sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"

How many other things will this make easier.

2006-09-14

Random Selects

The simple things are often the best. Like this way of selecting a random row from a table.

SELECT *
FROM MyTable
ORDER BY NEWID()

2006-09-11

.Net 2.0 RSA Encryption

Even though the new framework makes some tasks much easily, like openning a file in one line, other tasks, like encrypting data still require helper functions.
The classes all exist in the System.Security.Cryptography namespace:
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.aspx
I have choosen to use RSA asymmetric encryption:
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

Many examples helped over the basic msdn documentation:
http://www.codeproject.com/dotnet/RSACryptoPad.asp
http://www.codeproject.com/dotnet/SimpleEncryption.asp
http://www.eggheadcafe.com/articles/20020630.asp
http://www.codeproject.com/dotnet/ComboEncryption.asp
http://blogs.msdn.com/lcris/archive/2006/05/08/592868.aspx

One of the annoying things to remember is that on a shared hosting server, you will most likely not have access to the underlying COM object directories. I thought this would be a show stopper, but this only restricts you from decrypting the values, you can still encrypt items fine. So if your requirement is to collect data and store it securely with a public key, then download it remotely and process it with the private key, this should work.

Some issues that may arise:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q322/3/71.asp&NoWebContent=1

.Net Serializers

I had originally implemented a serialization using BinaryFormatter because i needed the result as a byte array. It all work well when running in nUnit, but when i moved it to my medium trust web application it failed. This was due to apparently, the use of reflection in the BinaryFormatter when it serializes all of the object graph properties, including private fields. The easy way to make it work was to change to using the XmlSerializer.

Interestingly though, the XmlSerializer does not implement the same interface as the BinaryFormatter, so i can't just sub in the constructor. The serialise method has the same parameters which is strange.

Heres the msdn doc on the XmlSerializer:
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

And this explains the trust issue:
http://haacked.com/archive/2006/07/09/ConfiguringLog4NetWithASP.NET2.0InMediumTrust.aspx

I really thought there might be a way to set the properties that get serialized, but it looks like a LinkDemand and Security assert is performed right at the start of the internal serialization class, so theres no hope of ever getting this to work.

2006-09-06

Inline Server Script in Header

Have you ever wanted to have inline script tags in your header control and add controls programatically. You will propably be getting an error about not being able to do this. This is especially true when you have a control that writes scripts into the header and then you also wish to have inline style sheets with the proper application path. The simple way to get around this problem is to wrap the inline scripts in an asp:PlaceHolder.

<html>
  <head runat="server">
    <title></title>
    <asp:PlaceHolder runat="server">
      <link href="<"%= SomeVariable %>/style.css" rel="stylesheet" type="text/css" />
    </asp:PlaceHolder>
  </head>
</html>


This way you can still add controls to the header by doing:
Page.Header.Controls.Add();

2006-08-31

c# Anonymous Method Syntax

I always keep forgetting the sytnax for these whenever i start a new project and have nothing to copy and paste from. These two links are the most helpful i have found. They provide examples over and above the documentation in msdn2.
http://www.theserverside.net/tt/articles/showarticle.tss?id=AnonymousMethods
http://msdn.microsoft.com/msdnmag/issues/06/00/C20/default.aspx

Working Days in T-SQL

These two posts helped greatly in getting a function to work out the working days between two working days:
http://aaronjohals.blogspot.com/2005/10/number-of-working-days-between-2-dates.html
http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-count-the-number-of-business-days-between-two-dates.html


drop function dbo.GetWorkingDaysDiff
go
create function dbo.GetWorkingDaysDiff(@start datetime, @end datetime)
    returns int
as 
begin
    declare @countdays int
    set @countdays = 0
    while @start < @end
    begin
        if (select datepart(dw, @start)) < 6
        begin
            set @countdays = @countdays + 1
        end
        set @start = dateadd(d,1,@start)
    end
    return @countdays
end 
GO
print(dbo.GetWorkingDaysDiff('2006/08/21', '2006/08/27'))
go

2006-08-30

NHibernate 1.2.0.Alpha1

I finally got around to implementing this into my code base, but as you would expect not everything worked first up. The saving worked fine, i could see the rows in the database. It was just the loading. At initial start NHibernate complains about virtuals on all you proxys for lazy loading, but i fixed them. A gut feeling i had was that this was to do with all to do with lazy loading.

As it turns out all collections and associations are lazy loaded in this new version:
http://forum.hibernate.org/viewtopic.php?t=960004&start=15&sid=ba44698acc40eaae77d6d73d7561ba83
http://dotnet.org.za/kuate/archive/2006/05/29/52751.aspx
So just put that attribute into my mapping file and everything went back to normal.

The real underlying cause of all this is that when things get lazy loaded they are a proxy type, not the actual type in the domain model. So when i debugged it like this:
Console.WriteLine(n.GetType());
I was getting a type name like this with these names all joined together with under scores
ProxyInterfaceSystemObject
INHibernateProxy
ISerializable
My tests for type (x is MyType) all fail silently, they really should be converted to proxy interfaces, but i don't need to atm, and i get 20% extra speed.

2006-08-29

ASP.NET 2.0 Medium Trust

Here are some links for configuring some library binaries:

nHibernate
http://forum.hibernate.org/viewtopic.php?t=963084&view=previous

IMPORTENT: Turn off the relection optimizer as you guessed it you dont have ReflectionPermission on shared hosting in medium trust.

<configuration>
  <configSections>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler" requirePermission="false"/>
  </configSections>
  <nhibernate>
    <add key="hibernate.use_reflection_optimizer" value="false" />
  </nhibernate>
</configuration>
I found also that the .NET 2.0 namespace declaration in the app.config/web.config caused exceptions, so just remove it, the application will still run in 2.0 no worries.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

Working Around Shared Hosting

I have been investigating the shared hosting issues with Spring and found that it is not as bad as it initially seems. There are a few steps that must be taken, very similar to those taken from other java ported libraries (eg nhibernate).
Firstly, all calls to GetCompiledPageInstance MUST be replaced with CreateInstanceFromVirtualPath. I have provided the fix for this in a previous thread (http://forum.springframework.net/showthread.php?t=407). This is a must because the GetCompiledPageInstance method has an attribute on it, pretty much killing calls to it unless you are running in full trust mode. On shared servers this will be highly unlikely.

[SecurityPermission(SecurityAction.Demand, Unrestricted = true)]

Now i had thought this had been commited into the main branch of SPring.Web, but the nightly build and the cvs repository on sourceforge both do not have the fix

The second step to take modifing the spring source, is to find the file src\Spring\Spring.Core\Core\IO\FileSystemResource.cs. This file has two calls like this

fileHandle.Directory.Root.ToString();

In the hosting trust mode, this bombs out with a FileIOPermission violation. The reason being, you don't have access to the root directory when you are on the shared host. The Root property of DirectoryInfo has this line in it causing the exception.

new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[] { text1 }, false, false).Demand();

My solution is a little hacky at the moment but you get the idea. All it is doing is getting the root folder (ie c: or d:). This propably wont work on a network drive remember.

fileHandle.Directory.ToString().Substring(0, 3);

Third thing to do is to add requirePermission="false" to the configSections section in web.config. If you dont do this you will receive a Configuration security permission exception.

<configSections>
  <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" 
 requirePermission="false"/>
    <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" 
 requirePermission="false"/>
  </sectionGroup>
</configSections>

The last item on the list is probably the most obscure, and i will try to explain and give some background:

dll's can be signed, which gives them a strong name and a hash, which stops them being modified and re-distributed.
strong named dlls can be registered in the GAC.
From the GAC, dlls can be configured to run in full trust, depending on your .net framework configuration. (see somewhere in control panel -> administration tools).

When you run a web application in anything but full trust, all the dlls in the bin directory run in a partial trust mode.
Strong named assemblies that are NOT in the GAC but in you bin directory, can not by default be called from a partial trusted assembly

So in this case i have my project dll, spring, nhibernate and log4net in the bin directory, plus any dependant dlls. nhibernate and log4net are strong named, but i can only call log4net from my app, not nhibernate. Why is this? its because log4net has an assembly level attribute on it.

[assembly: System.Security.AllowPartiallyTrustedCallers()]

This attribute allows my partially trusted application to call into it without throwing an exception. Details can be found on msdn, its something to do with LinkDemand.

Now it gets tricky, that would mean all the dll's that i have in the bin directory need this attribute. How would i do this?, I could get all the sources for them and recompile, but what happens if i can't get the source, or i have a 3rd party app that is proprietary. I do the following instead:

1. get all the dll's and disassemble them
2. add the AllowPartiallyTrustedCallers attribute to them in the MSIL file
3. remove all signing code from the assembly
4. remove all public keys and hashes from the assembly
5. update any extern assembly declarations, by removing the public hash requirement
6. reassemble the dll's again.

What does this do? It removes all strong names from the assemblies and also updates the references to not require the strong named versions.
As we dont have the keys that sign the dll, we can't resign them, but we can remove the signing altogether.
We could resign them all with new keys that we created and update the references, but that sounds like a lot of work for no gain.
We are going to be running in partial trust mode so we just don't need strong named assemblies.
This process probably wont work in all circumstances mind you.

2006-08-28

NHibernateUtil.IsInitialized()

I have a nhibernate lazy loaded collection, well a bunch of them in a tree of parent-child relations. When i save some nodes, i have to loop over the active nodes and add a bit of data to them. Trouble is when i access the parents/children it initilises the collections. This is bad as it then loads the whole tree into memory on the saved.
I thought there must be a way to check whether a collection had been loaded lazyily. First i thought about checking the type to see if it was the dynamic proxy. But then ran into this gem at the nhibernate reference guide:
http://www.hibernate.org/hib_docs/nhibernate/html/performance.html

I could just pass the collection to this method, and it would check if it had been initialised. Just waht i was after. The only other place i found a reference to this function then was at the forums:
http://forums.hibernate.org/viewtopic.php?p=2282177&sid=b5989f2c3f2387add4188a6e13802019

So why dao function ended up as, because i dont want the nhibernate logic permeating my service layer:

        public bool HasBeenInitialised(ICollection collection) {
            return NHibernateUtil.IsInitialized(collection);
        }

2006-08-27

bindingRedirect

I ran into a wall today where i was getting an exception akin to this:

Could not load file or assembly 'dommons, Version=0.0.33.0, Culture=neutral, PublicKeyToken=873d6a369a01a6bd' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Now im fine with the error, its a shared strong named assembly, that NOT in the GAC. So how do i fix it. It didn't take long to find the solution was to add a bindingRedirect. I was going to map the 0.0.33.0 version to the lastest 0.0.36.0 one. Easier said than done i'm afraid.
Here is a link explaining the redirect:
http://www.diranieh.com/NETAssemblies/Assemblies.htm

All i would have to do would be to put this in my web.config:


  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="dommons" 
  publicKeyToken="873d6a369a01a6bd" culture="neutral" />
        <bindingRedirect oldVersion="0.0.33.0" newVersion="0.0.36.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>


Though this did not fix the problem. A lot of material seems to be geared towards the assemblies being in the GAC or running as a client application. Not my case of build versions mismateching. To a newer version as well.

So i then spent the next half a day figuring out what was going on.
First checking i was doing it correctly:
http://blogs.msdn.com/suzcook/archive/2004/05/14/132022.aspx
http://plans.thefrankes.com/tutorials/Assemblies/
http://blogs.msdn.com/junfeng/archive/2004/11/16/258081.aspx

I was sure i had the syntax right, time to start using the assembly loading debugger, a tool called fuslogvw. In the start menu, open up the .net framework command prompt and type that. Also make sure you restart IIS to register it, otherwise nothing happens. Here are some more links for debugging:
http://www.grimes.demon.co.uk/workshops/fusWSFive.htm
http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx
http://www.grimes.demon.co.uk/workshops/fusWSFive.htm

So what was my problem, it seems that the redirect was not working. This happens first in the process (msdn). But why was it failing.
The error from fusion log viewer was this:

  *** Assembly Binder Log Entry  (27/08/2006 @ 3:29:10 PM) ***
  The operation failed.
  Bind result: hr = 0x80131040. No description available.
  Assembly manager loaded from:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
  Running under executable  c:\windows\system32\inetsrv\w3wp.exe
  --- A detailed error log follows.
  === Pre-bind state information ===
  LOG: User = NT AUTHORITY\NETWORK SERVICE
  LOG: DisplayName = dommons, Version=0.0.33.0, Culture=neutral, PublicKeyToken=873d6a369a01a6bd
  (Fully-specified)
  LOG: Appbase = file:///D:/projects/svn/project/trunk/project-web/
  LOG: Initial PrivatePath = D:\projects\svn\project\trunk\project-web\bin
  LOG: Dynamic Base = C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\project\91bd8e69
  LOG: Cache Base = C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\project\91bd8e69
  LOG: AppName = fec991e1
  Calling assembly : repository, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: D:\projects\svn\project\trunk\project-web\web.config
  LOG: Using host configuration file: file:////?\c:\windows\microsoft.net\framework\v2.0.50727\aspnet.config
  LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
  LOG: Post-policy reference: dommons, Version=0.0.33.0, Culture=neutral, PublicKeyToken=873d6a369a01a6bd
  LOG: GAC Lookup was unsuccessful.
  LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files
 /project/91bd8e69/fec991e1/dommons.DLL.
  LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files
 /project/91bd8e69/fec991e1/dommons/dommons.DLL.
  LOG: Attempting download of new URL file:///D:/projects/svn/project/trunk/project-web/bin/dommons.DLL.
  LOG: Assembly download was successful. Attempting setup of file: D:\projects\svn\project\trunk\project-web\
 bin\dommons.dll
  LOG: Entering download cache setup phase.
  LOG: Assembly Name is: dommons, Version=0.0.36.0, Culture=neutral, PublicKeyToken=873d6a369a01a6bd
  WRN: Comparing the assembly name resulted in the mismatch: Build Number
  ERR: The assembly reference did not match the assembly definition found.
  ERR: Setup failed with hr = 0x80131040.
  ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Note the two red lines. The redirect was not working, and as the 33 version could not be found, the application start failed. In frustration i tried a suggestion to put the redirect in the machine.config file, and what do you know, the whole thing started to work. Now at least i know why it isn't working it might be a little easier to figure out the cause. I think i read a permission on a security namespace item may need to be set.

2006-08-25

Collection was modified; enumeration operation may not execute.

For this exception i had always kept a cache array of elements to remove, but this guy creates a temperary array enumerator in the foreach loop. Nice work. But this doesn't work with generics, so he uses a delegate to do the same job. Someone also says that there is a powercollections framework that offers .net 2.0 some advanced features.

Extension Methods

Recently at work we had a requirement to have enum values displayed as proper words rather than camle cased. But you can't override the ToString method. But what about using the Extension Methods feature of c# 3.0. Now it seems like agood idead to do what upon future investigate you can't override methods like you would a virtual, you can only add new methods. So then i begs the point, what is the point of Extension Methods. The only case that i can see is if the class is sealed in a 3rd party library that you can't change, that you want to add a method, rather than having a Utils class to do the work for you. Now this is good, becuase then you can see all the methods in the visual studio intellisense. But if you own the class, then there really is no point, you would just add the method yourself.

The could be a way to use numerous different methods with the same name, but in different namespaces, but i haven't fully thought about that yet, and propably wont until 3.0 has gone into release.

Here are some links, though they are kind of old:
http://blah.winsmarts.com/2006/05/18/demystifying-c-30--part-3-extension-methods.aspx
http://mtaulty.com/communityserver/blogs/mike_taultys_blog/archive/2006/03/20/5798.aspx
http://www.cincomsmalltalk.com/userblogs/malby/blogView?showComments=true&entry=3304188302
http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx

2006-08-24

yield

So i found this keyword that i did not know about in c#. Intriguing. So what it does is creates an enumeration on the fly, without having to code a class to do it. Sounds good, but what could i use it in. A few examples suggest that it be used in tree traveral:

        public IEnumerator<Node> GetDepthFirstTraversalEnumerator() {
            yield return this.value;
            foreach(Node child in this.children) {
                IEnumerator<Node> enum_ = kidchildGetDepthFirstTraversalEnumerator();
                while(enum_.MoveNext()) {
                    yield return enum_.Current;
                }
            }
        }
 
and also found the map function from other laungauges:

        public delegate object MapFunction(object o);
        public static IEnumerable Map(MapFunction mapFunction, IEnumerable enumerable) {
            foreach(object o in enumerable) {
                yield return mapFunction(o);
            }
        }
        public static IEnumerable PositiveIntegers() {
            int a = 1;
            while(true) {
                yield return a;
                a = a + 1;
            }
        }
        public static IEnumerable SubList(int start, int end, IEnumerable enumerable) {
            int count = 0;
            foreach(object o in enumerable) {
                count++;
                if(count < start) {
                    continue;
                } else if(count <= end) {
                    yield return o;
                } else {
                    yield break;
                }
            }
        }
        IEnumerable set = I.SubList(5, 15, I.PositiveNumbers());
        foreach(int value in Map(delegate(object o) { return ((int)o) * 2; }, )) {
            //do something
        }
 
heres the link to a Fibonacci generator 
http://www.cerkit.com/cerkitBlog/2004/06/30/Using+The+Yield++Statement+In+Whidbey+C.aspx
 
a lazy paged collection:
http://haacked.com/archive/2006/08/14/FunIteratingPagedCollectionsWithGenericsAndIterators.aspx
 
and a textreader:
http://blogs.msdn.com/jmstall/archive/2005/08/08/textreader_yield.aspx

2006-08-23

WeakReference

Have you ever wanted to store a time consuming to construct object in a cache, but then it hangs around forever. Try storing a WeakReference to the object instead.

            object cached = new TimeConsumingToConstructObject();
            WeakReference wr = new WeakReference(cached);
            cached = null;
            if(wr.IsAlive) {
                cached = wr.Target;
            } else {
                cached = new TimeConsumingToConstructObject();
            }

To Ruby or not to Ruby

I was wondering if i should start tinkering with ruby to see what sort of framework it was. Then i read some items that suggested that it was still in its infancy as a technonolgy. Poor speed, not enough documentation.
http://discuss.joelonsoftware.com/default.asp?joel.3.309321.3
http://www.pankaj-k.net/archives/2005/11/ruby_or_java_a.html

I mean its sounds good to write less code, and have db mappping handled for you, but at what cost. I have spent years writting jscript asp with variable types, but the amount of errors that you miss without compile time and type checking support is painful. I prefer to let Visual Studio do the meanal tasks like remembering method names and type checking.

I think i saw somewhere that threading and internationalisation were issues, so it doesn't sound like an enterprise framework.

2006-08-21

COLLATE and Coalesce

I came across both of these items a while ago, but keep forgetting about them when i need to use them. The first is the COLLATE statement in T-SQL. Sometime different databases have different collations and when you try to query between them a collation error occurs. Now i used to always use one of the collations, ie collate Latin1_General_CS_AS. But i knew there was a default one, and i finally found it again (COLLATE database_default).
The other function Coalesce is a C# 2.0 item that does a thing similar to T-SQL's isnull()'s function. So:
string x = null;
string y = null;
string z = "123";
string value = x ?? y ?? z;
(value == "123") == true
So instead of using the :? statement checking nulls, it makes it alot cleaner.

2006-08-19

Javascript Includes not Included

Another 2 hours wasted this morning. It all started when a few atlas extender controls started throwing javascript debug alerts. I knew it used to work, and i couldn't see any recent changes that would cause it to break, like extra braces or colons. Then started the debuggin by removing the includes. This lead me back to the webresources, then to inline scripts. Finally i commented out the whole files text and had an alert, but no alert was firing.

Finally i stumbled accross a similar problem here. But then i tried it in firefox, and i got no errors. Doh, this was that IE bug where the cache gets filled an nothing seems to work anymore. I know that happens with view source, but it must also happen with this. As soon as i cleared the temporary internet files, everything worked again. Lucky with subversion i can just revert, not too much damage done.

Static Code Analysis

Intergrating FxCop into the cruisecontrol .net build process is not too hard. Also finding the SuppressMessage attribute was by pure luck. This attrbiute allows you to flag exceptions to FxCop rules in your source, rather than in the FxCop project file. But that doesn't phase me too much as i would still like the project file for message historys etc. I just created another file in the properties folder of the assembly for Analysis.cs.

The trouble with fxcop is that it only checks the compiled source, it doesn't check the actuall source code. I need a tool to intergrate with cruise control to check coding standards. The is a tool called PREfast in team studio, but i think it only does c/c++ for device drivers. And from the look of this article, thats all that microsoft offers. There is a java open source project called checkstyle that looks promising, but i will have to see that it doesn't throw huge amounts of errors for c#. I am surprised there is not a port yet. The last tool that i had a look at is SSW Code Auditor but it seems there server is having troubles at the moment and i can't download the trial. But it says it overs a large amount of source code checking, from cs, to html, to templates and sql.

2006-08-16

A MulitView with dynamic Views inside an UpdatePanel Exception

After having a hard day coding just before i hit the sack i got this exception that i couldn't find any google results on:

ActiveViewIndex is being set to '0'. It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.
Parameter name: value

I have an multiview in an updatepanel which has the individual view dynamically generated in OnLoad. So how was i supposed to register them in PreInit if the multiview hadn't even be initialised. Then i stumbled on a solution.

I added some empty dummy views in the ascx file. Then i just cleared them before loading my own dynamic ones. I only added about 10 as i dont think ill ever need more than that, but if i do ill just add some more.

<atlas:UpdatePanel ID="MultiView1UpdatePanel" Mode="Conditional" runat="server">
    <ContentTemplate>
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
            <asp:View ID="View0" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View1" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View2" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View3" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View4" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View5" runat="server">
                Empty
            </asp:View>
            <asp:View ID="View6" runat="server">
                Empty
            </asp:View>
            <%-- add more if needed --%>
        </asp:MultiView>
    </ContentTemplate>
    <Triggers>
        <atlas:ControlEventTrigger ControlID="Menu1" EventName="MenuItemClick" />
    </Triggers>
</atlas:UpdatePanel>
 
I tried adding this in code in both PreInit and Init before and after the call to base.x() but that didn't work. Ill see later if i can cleanup this solution any better

2006-08-10

WebServiceClientFactory

I found something with this Spring.Net webservice class.
When you define the context object:
  <object id="authServerStub" 
 type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
    <property name="ServiceUrl" value="http://localhost:81/auth/MembershipWS.asmx?wsdl"/>;
    <property name="ServiceInterface" 
 value="security.web.ISpringMembershipProviderAuthServer, dsecurity"/>
  </object>
and an interface like this:
    public interface ISpringMembershipProviderAuthServer {
        bool ValidateUser(string name, string password);
    }
when implementing this interface on the server end, you have to make sure that the parameter names match.
normally in c# you dont have to make the names match, only the types, but in this instance i expect that the client proxy generator uses the interface to pass arguements to the server. If you have them named different then you seems to only get null's on the server end.

Spring.Net WebApplicationContext

I was tring to use Spring.Net to configure an authentication application that i have been working on. The idea was to have a single-sign on webservice exposed by Spring and also have the client dynamically proxy the service via the same interface. This was all going well until the application would hang at load time with a timeout connection trying to download the webservice wsdl.

First i narrowed the problem to when spring started, which was when the application initialised a custom membership provider. I could also get to the webservice okay via the browser, so it must have been a context issue. I dropped the dependancy injection and created the webservice client in code and things worked fine. Next i ditched the AOP advice around the service.

Finally i removed a piece of code i had custom loading the webcontext and replaced it with:

            IApplicationContext ctx = WebApplicationContext.Current;

This did the trick, but how much time did i waste because of some legacy code i had for a work around for an older version on Spring.NET. Note to self, always work with the latest nightly build.

2006-08-09

Authenticate on LoginControl

I just spent 2 hours trying to get my custom membership provider to authenticate. No matter what i did i couldn't get the ValidateUser method to call. I know the provider was getting initialised because the Name and ApplicationName properties were being accessed.
Then i released that i had put this:

    protected void Page_Load(object sender, EventArgs e) {

        Login1.Authenticate += new AuthenticateEventHandler(Login1_Authenticate);

        Login1.LoggingIn += new LoginCancelEventHandler(Login1_LoggingIn);

        Login1.LoggedIn += new EventHandler(Login1_LoggedIn);

        Login1.LoginError += new EventHandler(Login1_LoginError);

    }

this was left over from some other debbuing and the OnAuthenticate was the problem. It was overriding the default provider login. Doh. So either remove the event or but my custom authentication in here. Kind of simple.

Security.dll

Now heres an interesting problem. I can run a vs2005 site in IIS no problems, but when i try to open it with the debugger in vs2005, i get the exception Unable to start Debugging System.Net.DigestClient. Plug the message into google and the first answer is it.
I have a dll called security and this apparently conflicts with a system dll of the same name.
What do i do, i rename the output of the dll to descurity.dll, clear out the bin directory, restart vstudio and no more problems. Unfortunately i now have to change the names of all the type declarations in web.config and spring files.
forums.microsoft.com
forums.asp.net

2006-08-08

Sandcastle CTP

At last i will be able to intergrate an api documentor into my cruisecontrol.net build process. Since i have migrated to .net 2.0 i have been without this feature. There are a few people building scripts and vs2005 plugins, so time to build the nant task.
Whats this, someone has already done it at http://blog.jkowalski.net/?p=52. Thanks Jaroslaw.

CodeFileBaseClass to the rescue of Page Inheritance

So i had a page that i needed to drop back into a compiled assembly, rather than just having it as a partial class next to the aspx file. This is always good, as you get compile support in the assembly. The only hard part is decalring all the controls you reference in the file as protected. Then i created a skeleton class that just extended the one from the assembly.

Trouble was that all the control definitions in the base class didn't get wired up. You know how it goes with the protected variables. So i spent a whilte trying public, didn't work. Reading blogs suggested using master pages.. Then Scott Allen on Rick Strahl's blog came to the rescue with the CodeFileBaseClass attribute on the page directive.

Nice. Rick's blog

Heres an example: jotek's sample

2006-03-07

String Function

Its always a pain when you are can't remember how to do something and then it takes you a few minutes to search for the syntax as google aint smart enough to turn what i think into effective queries.

Anyway i was looking for String.Format
builderau

And also i found a string truncate function:
4guysfromrolla

Its always nice.

Update: found these examples too:
http://blog.stevex.net/index.php/string-formatting-in-csharp/

2006-03-06

.Net 2.0 Code Profilers

When I went to try to profile my application in c# 2.0 I wasn't surprise to see that the old application that I used in 1.1 did not work anymore. These seem to be the current tools for profiling:

Free:
CLR Profiler
nprof

Cost:
ANTS Profiler
aqtime
jetbrains
and last but not least, the most expensive version of VS2005
vsts

I will try the CLR Profiler first and if the results are no good/hard to decipher, which I except, I will download the trial of ANTS, as that seems to be the recommended one.

2006-03-05

ASP.NET 2.0 EventValidation

When building an ajax enabled folder list view control, ran into a problem where i was clicking on the folder node, which caused a postback to the datagrid control. Trouble was then when i tried to operate on the datagrid control (sort, select) i was getting an error:

The state information is invalid for this page and might be corrupted.

After doing a lot of reading about it, this problem is caused by the event validation feature of ASP.NET 2.0. This is where the items in the datagrid are added to an event validation thingy, so that incorrect values can not be used to postback. Now i can solve it two ways, either by disabling eventvalidation it in web.config:
forums.asp.net
channel9
forums.asp.net

Or by registering each of the new items that i add to the datagrid for the callback so that event validation works correct:
alexthissen.nl

I will see how easy it is added the validation, and hope that magicajax will correctly handle the updates. Otherwise i will just turn off the security feature as suggested here:
forums.asp.net

2006-03-03

Format Source Code in Blog

I suppose this is the must have tool for posting source code into blogs. A real time saver.

MSDN VirtualPathProvider

So i copied all the code, make the files, make a web application in IIS. Doesn't work, so i have to make a few fixes to the code (ending brackets, change GetData on VirtualFile to use this.VirtualPath, rather than this.Name). Still doesn't work. Finally have to add httphandlers.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true"/>
    <customErrors mode="Off"/>
    <trace enabled="true" requestLimit="100" pageOutput="true" traceMode="SortByTime" localOnly="false"/>
    <httpHandlers>
      <add verb="*" path="*.vrf" type="System.Web.StaticFileHandler" />
    </httpHandlers>  
  </system.web>
</configuration>

And still cannot access by "~/", must access by "~/default.aspx".

VirtualPathProvider IIS vs ASP.NET Development Server

One thing that I have found when working in VS2005, is that I can be testing my VirtualPathProviders, and they will all work correctly for unmapped extensions. That is images (jpg, gif), styles sheets (css) and script files (js). Then when I move the application to run in IIS 6.0 the behaviour is different. This is because it seems that IIS does not even dispatch requests for such files to the ASP.NET runtime. So my VirtualPathProviders are not even used. Its strange how the two servers run differently.

The solution for IIS, to this problem is within one of Mark DÂ’Urso's presentations, http://blogs.msdn.com/mdurso/ [VSLive]. It involves adding a Wildcard Application Map in IIS's snapin (application configuration->mappings). With this mapping in place though, the ability to have default documents in a folder is lost. When normally requesting an aspx page, the ISAPI extension takes control from IIS. When you request a file that does not have an ISAPI mapping, IIS just servers the file itself. With the wildcard, all files are palmed off to the ASP.NET application extension. Don't know how this affects load/caching, having all files go though ASP.NET.

Heres what msdn says about wildcard application mappings in IIS 6.0:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx

Some info on httphandlers when wildcard application mappings are used:
http://support.microsoft.com/?kbid=909641

Static mappings for content served by ASP.NET compared to IIS
http://www.agileprogrammer.com/dotnetguy/archive/2003/05/04/4550.aspx

ASP.NET 2.0 VirtualPathProvider

Wouldn't it be nice in web applications to have all scripts and images embedded in an assembly so that you only had to release one file. Now if you used this common library across more than one project, you may not want to rely upon VS 2005's web deployment project. Each project would then have to be compiled separately.

One method of achieving this would be to use ASP.NET 2.0's VirtualPathProvider. Apparently you should be able to set up certain path's that map to what you want, rather than the filesystem. This could be a database, xml file or in this instance, an assembly.

If I wanted to create an assembly for a client script application, for example a html content editor, that consisted of style sheets, images and script files, the idea would be to map the path, ~/scripts/htmleditor, to a custom VirtualPathProvider. This provider would then read the file from the assembly and return it to the client.

Here are a few links to how to implement:
http://blogs.msdn.com/davidebb/archive/2005/11/27/497339.aspx
http://dotnetjunkies.com/WebLog/teund/archive/2005/02/15/54446.aspx