Tip: Clearing QueryString when using RadControls and ASP.NET WebForms

I had to google myself silly and the page I ended up finding was so hard to find I couldn’t actually find it again to link to it properly. The below code is not my idea, so if you feel wronged, submit your link and I shall attribute properly.

The problem is: I want to be able to navigate, or find my way back if you will, to a certain MultiPage and TabStrip that is nested inside a Telerik RadGrid. As it is a view and not a state change, I would like the querystring to handle this. However, to be nice, or obnoxious, depending on how you see it, Telerik will resubmit the same querystring when you navigate the tabs yourself, which will make it so that after changing tabs via querystring once, you cannot navigate to a different page by clicking anymore.

Anyway: People on the Internet, especially Telerik forums tell you to do Request.QueryString.Clear(), but that just doesn’t work because it is a readonly Collection and although it compiles, you will get a runtime error. So: What to do?

The post that I found simply used violence and Reflection to force the collection into being Read/Write, and then when Telerik reads from Request.Querystring to needlessly resubmit the exact previous query, it actually, accidentally, posts the correct, cleaned querystring. This enables my expected scenario where you by using GET can change tabs in a TabStrip/MultiPage.

        private void ClearQueryStringParam(string paramName)
        { Continue reading Tip: Clearing QueryString when using RadControls and ASP.NET WebForms 

Early Bird campaign ends in two weeks, Aug 15

Early Bird campaign ends in two weeks, Aug 15

We are proud of the program we have put together for Øredev 2013 and we are quite confident you will find the content interesting enough to at least consider coming to Malmö in November.

The thing is, our very lucrative early bird campaign is ending in two weeks, so if you are on the fence, this is the time to pull the trigger and make that reservation. It is easier to beg accounting for forgiveness than to ask for permission. It can’t hurt to at least ask.

Castle Windsor + NHibernate + MVC revisited

I have noticed some people come to this blog for reference on using Castle Windsor together with NHibernate and ASP.NET MVC, so I have to mention a few things that have happened since I last posted about it several years ago. First of all ,there is an excellent sample application that shows the simplicity with which you can hook these things up.

The sample shows you how to register Castle in an MVC application. how to add logging and how to hook up NHibernate 3.2 . This is far, far easier than what I described years ago thanks to improvements in all involved frameworks, really.

The important things to remember are:

  1. Use NuGet to install your libs
  2. Use MVC:s way of installing an IoC container (as demonstrated)
  3. Use logging, you might as well.
  4. Hook up the NHibernate Session Factory as a singleton and hook up ISession to request a new transient session from your singleton SessionFactory. There you go, the magic is taken care of.
  5. This is not mentioned in the demo, but if you are going building production code consider buying a subscription for NHProf if you don’t have one already. It wlll tell you where you mess up and need to do better.
  6. As above, look into hooking up Glimpse, see Scott Hanselman’s blog about it below.

Castle Windsor + NHibernate + log4net + ASP.NET MVC Sample

Windsor tutorial ASP.NET MVC 3-application To-be-Seen

Scott Hanselman on Glimpse:
If you’re not using Glimpse…

F# puzzle

One of my colleagues at Jayway launched a functional puzzle and solicited responses via Github gists. The challenge was as follows:

  1. Write a function that, given a sequence of either positive or negative integers, returns true if any subset of the list sums up to zero. Eg:
    find-solutions ([-7 4 3 8 -5]) => true
    find-solutions ([-6 2 3]) => false
  2. Make it work with any number, not just zero. Eg:
    find-solutions (0, [-7 4 3 8 -5]) => true
    find-solutions (13, [-7 4 3 8 -5]) => false
  3. Make it return all matching sequences, instead of a boolean. Eg:
    find-solutions (0, [-7 4 3 8 -5]) => [ [-7 4 3] [-7 4 8 -5] ]
    find-solutions (13, [-7 4 3 8 -5]) => []
  4. Make it take any predicate for the sum, not just equal to a number. Eg:
    find-solutions (isZero, [-7 4 3 8 -5]) => [ [-7 4 3] [-7 4 8 -5] ]
    find-solutions (isOdd, [-7 4 3]) => [ [-7] [3] [-7 4] [4 3] ]

Of course, me being an F# n00b I ended up at StackOverflow within hours of trying my hand at this, and I also asked among code monkeys on Facebook,  so I will not submit an answer is it wouldn’t be my own. My colleagues went ahead and figured out solutions on their own rather than use Google engineering like I did.

Anyway, as you can imagine, the first bit is the hard one. With higher order functions, delegating the actual filtering is done easily thereafter by supplying  the comparer function as a parameter.

So what do you do? I looked at many solutions, but couldn’t manage to use the comme il faut solution with a permutation tree, but went with a list of permutations instead which I then could trivially recurse through to see if I ever find a match.

If you are using Google engineering to solve F# problems, be aware that the old function Seq.map_concat has been renamed Seq.collect, which is a clear improvement.

In my case I found my permutation function on StackOverflow, provided by the excellent Tomáš Petříček, see blogroll, and just added my c0dez to find matches. This is from a console app, so hence the EntryPoint business.

let rec permutations list taken = 
  seq { if Set.count taken = List.length list then yield [] else
        for l in list do
          if not (Set.contains l taken) then 
            for perm in permutations list (Set.add l taken)  do
              yield l::perm }

let rec isMakingZero acc lst =
    match lst with
     | [] -> false
     | a :: tail -> (acc + a = 0) || isMakingZero (acc + a) tail 
    

let rec testpaths pathlist =
    if Seq.length pathlist = 0 then
        false
    else
        isMakingZero 0 (Seq.head pathlist) || testpaths (Seq.skip 1 pathlist)

[<EntryPoint>]
let main argv = 
    printfn "First %A"  (testpaths (permutations [1; -2; 3; -5] Set.empty))
    printfn "Second %A" (testpaths (permutations [1; -3; 2; -5] Set.empty))
    0 // return an integer exit code

    

As you can imagine, you just supply a function to testpaths that then gets passed down to isMakingZero, which should be renamed, in order to solve the remaining tasks.

F# Actor: An Actor Library

F# Actor: An Actor Library.

This is a follow-up on my post on the Actor Model, as I was very generous in my definition of F# Agents as a full-fledged Actor framework. F#:er s have been wanting something like the Akka framework for Scala. Colin Bull from whom I also reblogged the F# and Raven post below, posted about a framework on his blog that will provide this, and it also explains what the difference is and what the requirements are for an Actor framework.

 

Azure Websites with automatic deployment

Automatic deployment when you commit and push changes to your Github/Bitbucket repo is truly awesome and like running water or electricity that it is so convenient that you incorporate it into the fabric of your life and eventually only really notice it when it’s broken.The amount of reduced headache and increased productivity is amazing.

Overview

However, bringing continuous deployment to legacy web projects and solutions can be a bit challenging until it all works. The moving parts involved favor convention over configuration so in the most cases you don’t need to really do anything, but then there are cases when you all of a sudden notice that you need to do something to make things roll smoothly again.

Windows Azure Websites use a few tricks for the Deploy from Source Repository-feature to work. They put a git repository in your allotted folder on the IIS where your website resides which you can easily see via FTP.  You can access this repo directly if you do a straight git deployment where an “azure” remote is added to your local git repo, otherwise it is used by WAWS to pull your changes from TFS, Codeplex, GitHub or Bitbucket and it is from there the deployment happens. Allegedly, even when you deploy from Dropbox, your changes are first applied to the git repo in your directory in Azure and then the deployment begins from there.

The deployment system is open sourced at https://github.com/projectkudu/and you find further documentation there.

Gotchas

Multiple deployable projects in one repo

Let’s say you have a solution to deploy that has multiple projects that are potentially deployable. Kudu will not be able to guess which one to deploy and the deployment will fail with an error. What to do?

The solution is a deployment configuration file, a .deployments file. Yes, Windows won’t let you right click and create a file called “.deployment”, nor rename an existing file to that name, so you need to save it directly to that name from Visual Studio or just type the following in cmd.exe: (yes, the command means to copy from the Console, as in what you type, so just type the config file and finish with Ctrl+Z which means End of File and press enter again).

C:\Users\H4xx0rD00d\teh_c0dez\awesomerepo>copy con: .deployment
[config]
project = theActualFolderWhereTheProjectThatShouldBeDeployedResides
^Z
        1 file(s) copied.
C:\Users\H4xx0rD00d\teh_c0dez\awesomerepo>

After you add, commit and push that file, Azure will know which project to deploy. Further documentation on the .deployment file is available here.

Compilation fails with missing references

If you are using Nuget to manage your dependencies, you can choose either of two routes, Nuget Package Restore where Kudu will download the relevant libraries for you, or just committing your entire .nuget folder to your Git repository so that all your reference libraries are available for compilation on Azure.

The first option makes your repository more light-weight, but on the other hand you would be hoping that whomever manages the nuget packages on which you depend aren’t cheeky, like Microsoft have been in the past and withdraw the packages from under you, or that the maintainer misses a breaking change thus violating SemVer versioning causing Kudu to supply your app with an incompatible library.

With the second option you get a bulkier Repo, but you are in control of new versions of dependent libraries so that you can find out that they truly work with your system before you deploy. It works almost the same if you manually maintain your dependencies in the form of a Lib folder containing the DLLs that you need. Just ensure your Libs folder has been comitted to your Git repo and Kudu will be able to find them.

Øredev

For the last year and a half I have been a part of the Øredev Conference Program Committee. I should at some point write a post about it, and yes, I have had a draft laying around for a while, so here it goes:

I have always felt at home at Øredev as the conference was founded by Jayway and it is very much held close to our hearts. Of course, having played a part in the creation of the program makes it even more special for me to attend and last year I had the chance to attend Wednesday and also be at the Speakers’ dinner the evening before. This was a great opportunity to meet and greet, and I could have been more efficient there, but I did get to talk to a whole heap of interesting people.

The unrelenting search for quality speakers my colleagues pursued over the course of the year made Øredev 2012 angst-ridden in a good way. Whatever you chose to go see, there was always another talk you wanted to see going on at the same time. This is a good thing, when you are one of the ones putting together the program.

Last year

Last year we brought Mads Torgersen, Program Manager for the C# Language at Microsoft, which is pretty huge for a .NET guy like myself. He had a secret talk he wanted to do, and weeks before the conference, TypeScript was announced and we got to be the first ones to really bring that out there to our attendees.

TypeScript – JavaScript development at Scale
Async in C#5.0 – No More Callbacks!

Scott Barnes is a former Product Manager for Silverlight at Microsoft, now with Riagenic in Brisbane. He used his experience and laid out a plausible explanation to the twists and turns involving UI frameworks at Microsoft over the last year, as well as doing a very popular talk on a Design eye for the Developer Person. A very entertaining speaker that underplays his smarts.

Sebastien Lambla is another favourite of mine who took last year’s meme theme one step farther to combat session narcolepsy by leading the attendees in a Gangnam Style dance. He did an extremely initiated talk on HTTP Caching.

Jim McCarthy’s closing keynote on the Wednesday was mesmerising. I had to leave for the airport but I had a hard time breaking the spell to get up and leave.

Glenn Block is a good friend to the Øredev and a dream to work with. He is a very busy man but has the admirable quality of always wanting to help. In 2012 he talked about Hypermedia and WebApi as well as Node.js on Azure.

I have been a fan of Oren Eini’s since I saw him deliver a talk and demo on NHibernate back in 2009, and he is a very skilled and formidable speaker with a huge following on his blog. This time he delivered Raven DB training and two talks on Advanced RavenDB and an architecture talk entitled Hard Coding.

So what about 2013?

As experience tells us, you never know everything until November, but the program is out  and is looking rather complete. I went for .NET Open Source and F# this year, no point in trying to hide it. Maybe that’s a bit preachy, but so be it. We have great content on ASP.NET for IIS 8.5 and VS 2013 (“blue”) as well, of course and so on, but I really wanted to feature F# and functional in general as well as give props to some really mature OSS that isn’t getting enough praise.

In DDD with Value Objects, with ES and with the concept of Inversion of Control (well, OK, bear with me here), immutability and pure functions have already been shown to be the awesomesauce when trying to reduce complexity and increase parallelizability. Julie Lerman will talk DDD and Entity Framework, by the way.

Jessica Kerr will talk about Functional Principles for Object Oriented developers which explores how you can write better code in OO without going full Functional and Phil Trelford will talk about  F# for C# developers, when you do decide to go full Functional. Once you have decided to bridge the chasm, you go see Rachel Reese talk about the Actor model in F#, as in F# Agents, and about F# Type Providers. If you are ready to put the pieces together, there is Phil Trelford’s talk on F# for Trading, which is an example of why the City of London is crawling with F# developers  or at least recruiters looking for them.

NancyFx and ServiceStack are among the most popular Open Source frameworks out there for .NET and they have large communities, rich featuresets and proven track records and their respective fathers Andreas Håkansson and Demis Bellot will talk about them in greater detail and of course they will be available to be accosted for further questions you may have, which is the awesomest thing with Øredev.

A long time goal of mine was to bring Michele Leroux Bustamante to Malmö and Øredev. I became a fan back with her book Learning WCF which is the best programming book I have ever read, in tough competition with W Richard Stevens’ book Advanced Programming in the UNIX Environment. Since then Michele has created and sold a startup in the Identity field and is a Windows Azure MVP so what she doesn’t know about cloud and identity in the .NET space is probably not worth knowing.

Another important thing that you cannot miss is the excellent Anders Janmyr who with his talk on Habits of a Responsible Programmer managed to overcome the impossible standards you need to keep if you are both a part of Jayway and the Program Committee and still want to speak at the conference. You may think we would hand out speaking positions like candy to “our own”, but it’s rather the other way around as the last thing we would want to be accused of is playing favors and not striving for quality first.

This is just for starters, explore the program and book your tickets now. The conference will be the best ever.