Thursday, 24 October 2013

Virtual, Portable, Portable Virtual - What to do now that the Ubuntu Edge isn't going to happen?

How do I take my machine with me wherever I go, preferably in my pocket! 

Failure to launch of the Ubuntu Edge has left me dangling. I love the idea of convergence, but the hardware is not being produced. Ubuntu is also not releasing the "Ubuntu for Android" last I heard.

So you are left with "Linux for Android", but I've had issues since the Android 4.3 upgrade with ext4 image stuffs(long story) (Update: There are new images now solving the issues I had)

So now I've gone back to an old idea, but a goodie! Ubuntu on USB Memory Stick (Pen Drive) Forget about the NASTY start-up disk option, its just a bad experience IMHO.

I'll update the post with the details of my experience, and how to overcome some of the issues, but the basics steps are                           Update: Check here for details
  1. Boot Ubuntu from a Ubuntu Install DVD, 32 bit will give best portability, as you don't know what hardware you'll end up on
  2. Select the try Ubuntu option 
  3. Insert a USB memory stick (I'd advise minimum of 8Gb, but can be done with as little as 4Gb
  4. Install Ubuntu to the memory stick, making sure to target the memory stick for where the boot loader is installed
  5. Wait +- 6 hours for install (From DVD to HDD takes about 30 min, not sure why to USB takes forever! I've worked out a 20 min cloning option I'll blog about)
  6. Restart after install and boot from USB!
  7. Enjoy Ubuntu on the move. 
I've been running this for a month off a USB2 stick with excellent performance, especially on my old Dell 640M laptop, it flies now. I've gotten a high performance USB3 now which I'll moved to ASP.

Best thing is, where ever I go, "my computer" is right there on my keyring!

Other Options: Not ready to give up Windows, +Scott Hanselman has some good ideas on getting decent performance. See the Less Virtual Windows7


Monday, 30 September 2013

Clean Code Cheat Sheet

Assuming you've read Uncle Bob's (Robert Martin) Clean Code if not, it is a must for all developers.
Check this nice summary/reference sheet.

Friday, 16 August 2013

Quick replacement for Thread.Sleep

Ever needed to replace Thread.Sleep?

 Since you know you should be using it!

Or you haven't worked that out yet, or not been in that situation, but you've wanted to wake from a sleep call (Then you're in a situation where you shouldn't be using Thread.Sleep, and just not realized it yet)

Or your lazy and used Thread.Sleep knowing better because most of the code you could nick for the web has been too overbearing.

I've been in all three camps, and got tired of it and wrote this concise class to add to my toolbelt (someone else has probably done the same, I just didn't find it).

It will allow you the effect of sleep while still allowing callbacks to fire in the thread! Which is rather important when for

Example


You have a windows service which has to execute a task every x minutes.

As a minimum you'd have something to this affect

        public void DoWorkEveryXMinutes()
        {
            while (!stopWork)
            {
                Work();
                Thread.Sleep(TimeSpan.FromMinutes(5));
            }
        }

        public void Stop()
        {
            stopWork = true;
        }


So what happens when you want to stop working? You have to wait at least 5 minutes for the Thread.Sleep to complete, never mind the "work" task.

Hail the "Sleeper" class

    public class Sleeper:IDisposable
    {
        private readonly AutoResetEvent wake;
        private readonly Timer timer;

        public Sleeper()
        {
            wake = new AutoResetEvent(false);
            timer = new Timer(x => wake.Set());
        }

        public void Sleep(TimeSpan timeSpan)
        {
            timer.Change(timeSpan, TimeSpan.FromMilliseconds(-1));
            wake.WaitOne();
            timer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        public void Wake()
        {
            wake.Set();
        }

        public void Dispose()
        {
            timer.Dispose();
        }
    }


All you need to do now is create a instance of Sleeper to use (I'm still thinking about adapting it to work as Static) Replace your "Thread.Sleep" with "mySleeper.Sleep" Optionally add a Wake call "mySleeper.Wake"
        
        public void DoWorkEveryXMinutes()
        {
            while (!stopWork)
            {
                Work();
                //Thread.Sleep(TimeSpan.FromMinutes(5));
                mySleeper.Sleep(TimeSpan.FromMinutes(5));
            }
        }

        public void Stop()
        {
            stopWork = true;
            mySleeper.Wake();
        }
        

For simplicity and bravity I left out the manual reset event I've used in my sample code, which ensure tasks are completed before Stop returns giving the impression everything has stopped, which is not necessarily the case.

Check out the sample code here for complete but brief code. It makes use of Nuget package restorer, but you'll need to rename the file \.nuget\nuget.xex to .exe.
This also makes a useful basic sample of a Windows service using Topshelf. It sets you up with self installation and VS debugging, all things I used to set up myself, but now get in a supported package :D  ... so if your not using it, get on it. (Don't understand why VS services projects don't set up this way and requires 3rd party package)

Monday, 25 March 2013

Querying Oracle from .Net

So you need to connect to Oracle directly because the Linked Server in MS Sql Server is causing locks that refuse to release till you bounce SQL Server. THATS BAD! now consider it happened on our shared production clustered instance. Well ... you just have to wait till everyone ... globally has agreed you may. Fairly it does impact them, but your production system is down till then.

As you can imagine, major architecture shift we are on, has now been put into hyper drive. So less MS Sql Server, especially shared instances. Role in the new fad, SOA, Mongo etc. Granted SOA has been around, but were working at doing it right.

This is just a short how to, as I have to connect my new micro service to get the info from Oracle we were getting through the Linked Server. I found the documentation on getting going rather ropey.

So for a basic connection to Oracle. Assuming Windows x64, .Net 4

  1. Download "Instant Client Package - Basic Lite" and install it.
  2. Download "Oracle Data Access Components (ODAC) Downloads" and extract that to a useful location
  3. Reference the Oracle.DataAccess.dll from your project (useful location\ODAC1120320Xcopy_x64\odp.net4\odp.net\bin\4\Oracle.DataAccess.dll)
  4. Forget all that tnsnames.ora stuff you keep reading about all over.
  5. Construct your connection string like this e.g.
    string oradb = "user id=myUserName;password=MyPassword;data source=" +
                               "(DESCRIPTION=" +
                               "(ADDRESS=(PROTOCOL=tcp)(HOST=name.of.server.com)(PORT=1521))" +
                               //Or other port number if not default 
                               "(CONNECT_DATA=(SERVICE_NAME=myDatabase))" +
                               ");";
    
  6.  Open a new connection e.g. using (var conn = new OracleConnection(oradb))
  7. Query away.
  8. Beware the memory leaks. Close the and dispose the connection for each query, long running connections with lots of commands executed against it tend to leak like a sift.

References:
  1. http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
  2. http://docs.oracle.com/cd/B19306_01/win.102/b14307/featConnecting.htm#sthref114
When I work out how to include an attachment I'll update with a sample code file.

Updated 17/06/2013
Code Sample has dummy .dll's as I can't redistribute Oracles Dll's that you'll need to do for yourself.

Look out for my next blog with instructions for creating a NuGet package to host yourself for easy oracle project creation.

Thursday, 21 February 2013

Integrating MVC 4 into an old ASP.Net application

I thought now would be a good time to get my new dev blog going.

I used +Scott Hanselman  post before to get MVC3 into an existing ASP.Net project I wanted to port in stages. So this time when I wanted to do the same with MVC4 I went digging for the same article and thought, about time I added it to a blog to find it easier in future.

So using Scotts well documented approach, I was able to get going again quickly. There were a couple of bumps to smooth out though.

Remove the superfluous rubbish a standard VS project adds (Basic Template)
  1. Global.asax.cs
    • using System.Web.Optimization;
    • WebApiConfig.Register(GlobalConfiguration.Configuration);
    • BundleConfig.RegisterBundles(BundleTable.Bundles);
  2. Views/Shared/WEb.Config under system.web.webPages.razor/pages/namespaces
    • <add namespace="System.Web.Optimization"/>
You'll most likely find System.Web.Optimization strangely missing  (causing compilation or run-time errors), dependent on when and how you installed MVC4, anyhow in my opinion - YAGNI.
So in case you end up here instead of on +Scott Hanselman blog site as you were looking for a MVC4 option, head over there and get busy!