Friday, October 29, 2010

PDC 2010 Updates - First Look

The main updates from the PDC weren't that exciting... I haven't seen anything on Azure storage yet - hopefully soon...

The coolest thing I saw was Anders talking about c#5 - async and await - think linq for async tasks - see http://blogs.msdn.com/b/ericlippert/archive/2010/10/28/asynchrony-in-c-5-part-one.aspx and download CTP from http://msdn.microsoft.com/en-gb/vstudio/async.aspx - I liked :) Full video available on the pdcplayer... probably too much effort to find it!

The main things that might impact some of the projects that I'm working on were:
- a much better Azure management portal is on the way - at last!
- full IIS7 hosting - so you can set custom properties and so your web role can host multiple web projects :)
- Remote Desktop access to your web and worker role instances (not sure it's that useful to my projects but it made me feel happier)
- Velocity (Memcached) will be available soon
- Java will become a "first class citizen" ?!
- Extra small instances - I suspect that most of my worker roles could move to these

Azure overview is here:
SQL Azure updates are here:

Will keep an eye on some of the sessions tonight - especially Jai's deep dive into storage.

Stuart

Wednesday, October 27, 2010

How to do "TOP N" in nhibernate's query language

How to do "TOP N" in nhibernate's query language...

Seems like you can't - so instead you have to use:
- SetMaxResults(N)
on the IQuery object.

With help from:
http://stackoverflow.com/questions/555045/nhibernate-hqls-equivalent-to-t-sqls-top-keyword

Tuesday, October 26, 2010

The 5 second guide to adding WCF Client code

In case you ever have to blag some WCF client code ever...

1. Right click on your project
2. Choose "Add Service Reference"
3. In the dialog, enter the service URL wait for the "discovery" to happen, then change the namespace name and hit OK
4. In the client code just call the service - like
                var client = new MyNameSpace.MyServiceClient();
                int result = client.Method(param1, param2, param3);

Simples ;)

Tuesday, October 19, 2010

Converting datetimes to just the date (and to pretty text) in SQLServer

From http://stackoverflow.com/questions/113045/how-to-return-the-date-part-only-from-a-sql-server-datetime-datatype

DATEADD(dd, 0, DATEDIFF(dd, 0, [Datetime])) AS TheDate,

Also, this helps for text formatting

CONVERT(nvarchar, TheDate, NNN)
101 - 10/19/2010
102 - 2010.10.19
103 - 19/10/2010
104 - 19.10.2010
105 - 19-10-2010
106 - 19 Oct 2010
107 - Oct 19, 2010
108 - 00:00:00
109 - Oct 19 2010 12:00:00:000AM
110 - 10-19-2010
111 - 2010/10/19
112 - 20101019
113 - 19 Oct 2010 00:00:00:000

Monday, October 18, 2010

Parsing DateTime's into UTC

This article proved helpful - http://www.mindthe.net/devices/2008/05/23/tryparseexact-and-utc-datetime/

But actually I think the answer is to use BOTH AssumeUniversal AND AdjustToUniversal
            DateTime.TryParseExact(group.Value, this.customParseText, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal | System.Globalization.DateTimeStyles.AdjustToUniversal, out dateTime);

Friday, October 15, 2010

Datetime comparison for Azure Table Storage REST API


string.Format("{0} le datetime'{1}'", PropertyName, XmlConvert.ToString(cutoffDate, XmlDateTimeSerializationMode.RoundtripKind))

although obviously this will then need to be UrlEncoded

Wednesday, October 13, 2010

Get a RelativePath string

        public string GetRelativePath(string rootPath, string fullPath)
        {
            Uri uri1 = new Uri(rootPath);
            Uri uri2 = new Uri(fullPath);

            Uri relativeUri = uri1.MakeRelativeUri(uri2);

            return relativeUri.ToString();
        }

Test if a path is a Directory?

        public bool IsDirectory(string path)
        {
            try
            {
                FileAttributes attr = System.IO.File.GetAttributes(path);
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    return true;
                }
                return false;
            }
            catch (FileNotFoundException)
            {
                return false;
            }
            catch (DirectoryNotFoundException)
            {
                return false;
            }
        }

Monday, October 11, 2010

If you see Error 32 Internal Compiler Error: stage 'COMPILE' ....

If you see Error 32 Internal Compiler Error: stage 'COMPILE' ....

then what caused it for me...

was accessing a {get; private set;} variable

Removing that allowed the compile to proceed again

If you're looking for quick and effective free training on M$oft tools

This resource looks excellent:
http://channel9.msdn.com/Learn/Courses/

Then you can also play with code from:

There's so much resource out there!

Sunday, October 10, 2010

If you are using AutoFac and you see... "are you missing a using directive or an assembly reference" when using AutoFac

e.g if you see:

'Autofac.ContainerBuilder' does not contain a definition for 'RegisterAssemblyTypes' and no extension method 'RegisterAssemblyTypes' accepting a first argument of type 'Autofac.ContainerBuilder' could be found (are you missing a using directive or an assembly reference?)

or 
'Autofac.ContainerBuilder' does not contain a definition for 'RegisterInterface' and no extension method 'RegisterInterface' accepting a first argument of type 'Autofac.ContainerBuilder' could be found (are you missing a using directive or an assembly reference?)

or....

then check that you are using "using Autofac" at the top of the file

if you don't include this then you won't get the necessary extension methods inside your file's visible namespaces.

Friday, October 01, 2010

Back to basics - simple Azure table test results part 2

Another test - what happens if you try concurrent updates.... breaking the optimistic concurrency locking

        static TableServiceContext GetContext()
        {
            CloudTableClient client = new CloudTableClient(
                "https://***.table.core.windows.net",
                new StorageCredentialsAccountAndKey("***", "****"));

            //client.CreateTableIfNotExist("TestData");
            var context1 = client.GetDataServiceContext();

            return context1;
        }

        static Thing GetFredFromContext(TableServiceContext context)
        {
            var query = context.CreateQuery<Thing>("TestData");
            var subquery = from t in query
                           where t.PartitionKey == "Fred"
                           && t.RowKey == "Bloggs"
                           select t;

            var element = subquery.First();
            return element;
        }

        static void Main(string[] args)
        {
            var context1 = GetContext();
            var context2 = GetContext();

            var one = GetFredFromContext(context1);
            var two = GetFredFromContext(context2);

            one.TestField = "test2";
            two.TestField = "test3";

            context1.UpdateObject(one);
            context2.UpdateObject(two);

            context1.SaveChanges();

            try
            {
                context2.SaveChanges();
            }
            catch (Exception e)
            {
// exception is caught here - 
            }
        }


The exception is System.Data.Services.Client.DataServiceRequestException
The InnerException is [System.Data.Services.Client.DataServiceClientException] with StatusCode of 412 - from http://msdn.microsoft.com/en-us/library/dd179438.aspx

UpdateConditionNotSatisfied

Precondition Failed (412)

Back to basics - simple Azure table test results

What happens if you try to create an object twice:

            CloudTableClient client = new CloudTableClient(
                "https://thing.table.core.windows.net",
                new StorageCredentialsAccountAndKey("thing", "****blurb******"));

            client.CreateTableIfNotExist("TestData");
            var context1 = client.GetDataServiceContext();

            Thing t = new Thing();
            t.PartitionKey = "Fred";
            t.RowKey = "Bloggs";
            t.TestField = "test";

            context1.AddObject("TestData", t);
            var response = context1.SaveChanges();

            context1.Detach(t);

            context1.AddObject("TestData", t);
            response = context1.SaveChanges();

the answer... is that you get an exception thrown in the second "SaveChanges()" - the exception is System.Data.Services.Client.DataServiceRequestException - with an InnerException of type DataServiceClientException -with StatusCode 409 - EntityAlreadyExists