Thursday, June 25, 2009

A good article on the changes from the 32 bit address space to 64 bits.

Related to the previous article on hacking visual studio to get rid of the 2Gb limit on user process virtual memory there is a good article from Mark Russinovich that explains what this setting (in the bootloader) does and how the memory looks like when you have a processor and OS that supports a 64bit address space:

http://blogs.technet.com/markrussinovich/archive/2008/11/17/3155406.aspx

Reducing the out of memory errors in Visual Studio

I was getting a lot of OutOfMemory or NotEnoughStorage errors in visual studio when developing with large projects. It seems that this is a known memory problem. I follow the instructions on this post and with the 3Gb I have on my laptop it improved a lot. If you are experiencing the same here is the link:

http://stevenharman.net/blog/archive/2008/04/29/hacking-visual-studio-to-use-more-than-2gigabytes-of-memory.aspx

Happy coding,

Tuesday, June 16, 2009

Primavera School Reporting - First product on the platform we have been developing in Primavera

After a couple of years of work on a modeling platform where I have been contributing in the areas of architecture, business patterns and service orientation the first product is out:

http://www.primaverabss.com/pt/PortalRender.aspx?PageID={8f4a9168-49ed-407d-9b60-8656e1ec2256}

Here is a quick look on it.

The announcement is only available in Portuguese as this is a product that applies only to Portuguese legislation.

This is a state of the art product with all the latest .NET 3.5 technologies in and is based on MDD principles.

Friday, June 5, 2009

Windows Azure Configuration for Microsoft Sample Storage Client

Another quick note on Windows Azure that made me loose some time.

The urls of the three storages, Table, Blob and Queue must be given without the account name:

   1: <ConfigurationSettings>
   2:   <Setting name="AccountName"
   3:      value="youraccounthere"/>
   4:  
   5:   <Setting name="AccountSharedKey"
   6:            value="yourprimarykeyhere"/>
   7:  
   8:   <Setting name="TableStorageEndpoint"
   9:            value="http://table.core.windows.net/"/>
  10:  
  11:   <Setting name="BlobStorageEndpoint"
  12:            value="http://blob.core.windows.net/"/>
  13:  
  14:   <Setting name="QueueStorageEndpoint"
  15:            value="http://queue.core.windows.net/"/>
  16: </ConfigurationSettings>

What you will see on the portal is something like http://youraccount.table.core.windows.net and copying that string will not work with the code as is.

Thursday, June 4, 2009

Windows Azure - TableNotFound

I got this error while trying out Windows Azure after deploying to the cloud fabrik. When testing locally we use the option shown on the picture bellow but on the cloud that is not required.

AzureCreateTestTablesMenu

My problem was that I was not calling the initialization code that creates the tables in the storage. This is not required locally. The recommend initialization for the cloud is demonstrated in this video:

Store Data in Windows Azure Tables

The initialization is done in the Global.axax in the BeginRequest event handler. My looks like this:

   1: protected void Application_BeginRequest(object sender, EventArgs e)
   2: {
   3:     HttpApplication app = (HttpApplication)sender;
   4:     HttpContext context = app.Context;
   5:     FirstRequestInitialization.Initialize(context);
   6: }

And the FirstRequestInitialization class looks like this:

   1: class FirstRequestInitialization
   2: {
   3:     private static bool s_InitializedAlready = false;
   4:     private static Object s_lock = new Object();
   5:  
   6:     public static void Initialize(HttpContext context)
   7:     {
   8:         if (s_InitializedAlready)
   9:         {
  10:             return;
  11:         }
  12:         lock (s_lock)
  13:         {
  14:             if (s_InitializedAlready)
  15:             {
  16:                 return;
  17:             }
  18:  
  19:             ApplicationStartUponFirstRequest(context);
  20:             s_InitializedAlready = true;
  21:         }
  22:     }
  23:  
  24:     private static void ApplicationStartUponFirstRequest(HttpContext context)
  25:     {
  26:         StorageAccountInfo accountInfo =
  27:             StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
  28:         
  29:         TableStorage.CreateTablesFromModel(typeof(BrandTable), accountInfo);
  30:         // other tables here...
  31:     }
  32: }

Basically what this code does is to read the configuration from the configuration file and create each required table. If you get this error and you are using this method you forgot to add a call to the CreateTablesFromModel with the missing table.

Reposition a console window in C#

Motivation

When writing demo code I think a very nice interface is a console application. It is quick way of getting an application to input and output data without having to think on all those complex UI problems.

When you have to console projects, eg. client and server, the windows will most likely start cascaded. You then have to move one of them to be able to read stuff in both at the same time. This is really annoying.

How to

First you will need to get some native APIs:

   1: public struct RECT
   2: {
   3:    public int left;
   4:    public int top;
   5:    public int right;
   6:    public int bottom;
   7: }
   8:  
   9: public static class NativeMethods
  10: {
  11:    public const UInt32 WM_MOVE = 0x0003;
  12:  
  13:    [DllImport("kernel32.dll")]
  14:    public static extern IntPtr GetConsoleWindow();
  15:  
  16:    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  17:    public static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
  18:  
  19:    /// <summary>
  20:    /// The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area.
  21:    /// </summary>
  22:    /// <param name="hWnd">Handle to the window.</param>
  23:    /// <param name="X">Specifies the new position of the left side of the window.</param>
  24:    /// <param name="Y">Specifies the new position of the top of the window.</param>
  25:    /// <param name="nWidth">Specifies the new width of the window.</param>
  26:    /// <param name="nHeight">Specifies the new height of the window.</param>
  27:    /// <param name="bRepaint">Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.</param>
  28:    /// <returns>If the function succeeds, the return value is nonzero.
  29:    /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para></returns>
  30:    [DllImport("user32.dll", SetLastError = true)]
  31:    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  32:  
  33:    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
  34:    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
  35: }

First you use the GetConsoleWindow API to get a handle to the window where the console buffers are being outputted.

The second step is to get the RECT structure populated with the window rectangle coordinates. We need this to compute the width and height of the window because we don’t want to change that and those are needed for the next API call.

The third and final step is calling MoveWindow API passing in the new origin and the same dimensions. Where goes a method that does just that:

   1: public static void RePositionWindow(int x, int y)
   2: {
   3:     IntPtr windowHandle = NativeMethods.GetConsoleWindow();
   4:     RECT rect = new RECT();
   5:     NativeMethods.GetWindowRect(windowHandle, ref rect);
   6:     NativeMethods.MoveWindow(windowHandle, x, y, rect.right - rect.left, rect.bottom - rect.top, true);
   7: }

I call this in the Main method before doing anything:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         ConsoleUtils.RePositionWindow(800, 100);
   6:  
   7:         // add code bellow
   8:     }
   9: }

I give each window a hardcoded position but you can try a more elaborate algorithm like dividing the available desktop space.