Wednesday, October 13, 2010

Azure Tables Error Messages

Azure Table Storage (ATS) is a REST-basead data access mechanism, and REST is based on HTTP. So, makes sense that ATS data access errors are returned as HTTP errors. But some translation is needed to understand those errors.

1. DataServiceRequestException and DataServiceClientException exceptions

When a data access error occurs in ATS, the exception returned usually is from DataServiceRequestException class, being the error message “An error ocurred while processing this request”. This exception encapsulates a more specific DataServiceClientException exception, that contains a XML document on the Message property describing the error:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <code>EntityAlreadyExists</code>
    <message xml:lang="pt-BR">The specified entity already exists.</message>
</error>

The value on the <code> element needs to be interpreted considering what the application was doing when the error ocurred. For example, the code InvalidInput is generated if you are trying to save an entity that has a property of an unsupported data type; OR if you are trying to update an entity that has no concurrency control information. Almost the same, right? ;-)

2. Error codes an possible causes

Following are the HTTP error code that we’ve bumped into, and what the application was trying do do when the error ocurred.

Error Code
(value on <code> element)
Possible Causes
ConditionNotMet - Concurrency error. The app read some entity; when trying to modify or delete the entity, it had already been modified by a command from another data context.
EntityAlreadyExists - Primary key violation. The app is trying to insert an entity, but there’s an entity on the table with the same values for PartitionKey and RowKey properties on the entity being inserted.
InternalError - Use of unsupported Linq operators on ATS. (We found this one trying to recover rows that had a string property starting with some value. There’s no string support for like, >, <, and neither string.StartsWith() – the one that generated the error. You have to use string.CompareTo() – :-P)
InvalidInput - Storing an entity that has properties of types not supported by ATS.
- Updating an entity that was attached to the context (TableServiceContext.AttachTo()), but has null on it’s
ETag property.
- Linq queries on a table with no structure defined. We received this error when trying to query a table that had just been created; if you insert some entities, even if you erase all the entities on table later, it seems that the table holds the entity structure and you can use it on Linq queries.
- Linq queries using
anonymous types. The workaround was to make the query return the “whole” entity, and build a second query using anonmymous types on the data returned by ATS.
- Batch save operation (SaveChanges(SaveOptions.Batch)) with 2 or more entities having the same Partion and Row Keys.
ResourceNotFound - Linq query that returns no entitites using equality conditions on PartitionKey and RowKey (where entity.PartitionKey == “…” && entity.RowKey == “…”). (Queries with conditions using other attributes return a 0-sized list)
- Concurrency error. The app read some entity; when trying to modify or delete the entity, it had already been deleted by a command from another data context.
- Linq query on a table that does not exist on ATS.
TableAlreadyExists Found on calls to CloudTableClient.CreateTablesFromModel(). It seems that concurrent calls to this method may end up generating this exception. We placed a call to CreateTablesFromModel() on the data access context static constructor. When we raised the number of role instances to 2, one of them started ok, and the other one generated the exception.
TableBeingDeleted Same as TableAlreadyExists above.
OutOfRangeInput Generated when saving an entity with an uninitialized DateTime property. The minimum allowed value for a DateTime property on ATS is Jan 01, 1601, and an uninitialized DateTime in C# holds Jan 01, year 1. We didn’t detected it on tests against DevStorage because it changes dates before Jan 01, 1753 to Jan 01, 1753, but ATS does not.

3. How to extract the HTTP error code from DataServiceClientException objects

The code is based on an enum that has members named exactly as the error codes returned by ATS:


public enum ATSError
{
    EntityAlreadyExists,
    ConditionNotMet,
    InvalidInput,
    ResourceNotFound, 
    Unknown  // This one is the "generic" error code 
}

The following method extracts the error code from the XML returned by ATS, and converts it on the correspondent ATSError enum value:

/// <summary>
/// Translates an ATS exception to one of the ATSErrors values.
/// </summary>
/// <param name="error">Exception returned by ATS.</param>
/// <returns></returns>
public static ATSError ExtractATSError(Exception e)
{
    // Searches for a DataServiceClientException exception
    while (!(e is System.Data.Services.Client.DataServiceClientException) && (e != null)) 
        e = e.InnerException;
    if (e == null) return ATSError.Unknown;

    // At this point "e" contains a DataServiceClientException object,  
    // and the Message property contains a valid XML document describing the error
    ATSError code;
    try
    {
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(e.Message);
        // The XML document uses namespace http://schemas.microsoft.com/ado/2007/08/dataservices/metadata, so 
        // we have to use a XmlNamespaceManager to represent this namespace
        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xml.NameTable);
        namespaceManager.AddNamespace("n", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        // Retrieves the "<code>" element 
        string s = xml.SelectSingleNode("/n:error/n:code", namespaceManager).InnerText;
        // Converts the element value to the corresponding ATSError value
        code = (ATSError)Enum.Parse(typeof(ATSError), s);
    }
    catch
    { 
        code = ATSError.Unknown;
    }
    return code;
}

And that’s it. If you bump into another error code, just add the corresponding member on the ATSError enum, so that the ExtractATSError() method will recognize it.

Tuesday, August 24, 2010

Error on Azure Development Storage startup

Right after you install Azure Tools for Visual Studio, if you try to start the Development Storage service, a command prompt window pops up, then shows a error message in red, then closes so fast you can't even start to read the error message. I've tried to start the Development Storage from a command prompt to see what was the error. But the .lnk file to Dev Storage does not show what is the command to start it.
Tip 1: To start Azure Development Storage from a command prompt, open an Azure Command Prompt (Start > All Programs > Windows Azure SDK > Windows Azure SDK Command Prompt), and then run csrun /devstore.
Ok. Then I could see the error message:
Encountered an unexpected error from the devstore: Unable to start Development Storage.
Error Details: Failed to start Development Storage: the SQL Server instance 'localhost\SQLExpress' could not be found. Please configure the SQL Server instance for Development Storage using the 'DSInit' utility in the Windows Azure SDK.
Nothing like being able to read the error message when you're trying to fix some error. That leads to
Tip 2: After you install Azure Tools for Visual Studio, run dsinit /sqlinstance:sql_instance_name to create the database needed to run Development Storage.
Since I already have a non-named default SQL Server instance, I ran dsinit /sqlinstance:. (dsinit /sqlinstance "colon" "dot"), as "dot" is the name for a default SQL Server instance. Then Dev Storage started up ok.

Thursday, June 17, 2010

Test machine using boot from VHD

Another option to my last post on SharePoint test machine setup is to boot from a VHD file. A VHD is a Virtual Hard Disk, a file that is used as a hard disk on a Virtual PC or Hyper-V Virtual Machine.

I followed the instructions on Marin Frankovic's blog about it, and it was very easy to create an easy-to-rollback, boot-from-vhd test environment. That's what I did:
  1. I used Marin's post to install a fresh Windows 2008 boot.
  2. I then created a differencing vhd based on the new W2K8 boot, using diskpart and running the diskpart command create vdisk file=c:\testdiff.vhd parent=c:\test.vhd maximum=size_in_mb type=expandable (or fixed). This is nice because you can create a initial differencing VHD from a base VHD, put aside a copy of this differencing VHD, and "destroy" your new boot with tests. When you want to rollback the "destruction", just throw away the differencig file your were using, and start with a fresh new copy from the VHD you put aside before.
  3. Next I added the boot entry to this differencing vhd. On a command prompt:
    a. bcdedit /copy generates boot entry copies. There's plenty of references on this, google for some.
    b. Run the following to point the new boot entry to the differencing VHD:
    bcdedit /set {boot_identifier} device vhd=[c:]\path_to_vhd\vdhfile.vdh
    bcdedit /set {boot_identifier} osdevice vhd=[c:]\path_to_vhd\vhdfile.vhd
    bcdedit /set {boot_identifier} detecthal yes
    (the boot identifier is shown when you generate the boot copy with bcdedit /copy. It can also be seen f you run bcdedit with no params)
And that's it. Your tests boots now can use the full power of your machine, RAM & CPU, instead of sharing the (precious) hardware with the host OS, as VM's do. Just remember: boot from VHD is supported only if your OS is W7 Ultimate or Enterprise, or W2K8 R2.

Thursday, March 25, 2010

SharePoint 2010 Test Machine Setup

Some facts on my SharePoint 2010 test environment setup:

1. Some useful links
2. The SharePoint Demonstration Virtual Machine
  • It's a complete test environment for SharePoint 2010, comes pre-loaded with SharePoint Server 2010, Office 2010, SharePoint Designer 2010, an Active Directory domain, Visual Studio 2010, SQL Server 2008 + Analysis & Reporting Services, Exchange Server 2010 (Exchange on a second VM), and several sample sites with data on it. It saves a lot of work.
  • SharePoint 2010 is a *64-bit only* software, so you can't create its VM's on Virtual Server or Virtual PC, since they don't support 64-bits VM's.
    • Since the VM guest operationg system has to be 64-bit, you will have to user Hyper-V, wich only runs on Windows 2008 machines.
    • If you have a desktop that has been more off than on since you got that notebook you are using now, a "lighter" option is to download (free) and intall Hyper-V Server 2008 R2. It's a stripped down version of Windows 2008 designed solely to run VM's and with support for remote VM management (so you will work from the new note ;-). My 3GHz Pentium D (yes, Dual Core, not Core2 Duo) with 6GB RAM could handle the SharePoint 2010 VM reasonably.
Aside from that, you do can install SharePoint 2010 Beta on Windows 7 and Vista (provided that they are 64-bit installations), but it seems too much trouble just for setup a test machine. If you have a Windows 2008 machine, create the VM on Hyper-V, attach the VHD, and that´s it. If not and you have a second machine at hand, go for Hyper-V Server. It took far more time for me to download the Hyper-V Server setup than to get the SharePoint VM running, so that's my suggestion for you.

Note: the only thing I had to change on MS SharePoint Demonstration VM was to fix the virtual network adapter IP address to 192.168.150.1, wich is the address the DNS entries are setup to. and that's it. That took some time to figure out, but not that much... ;-)

Note2: Check out this post too: "Test Machine Using Boot From VHD". Booting from a VHD improves the performance of your test machine, because there's only one operating system running - instead of two, as when you use virtual machines.

Friday, February 26, 2010

SharePoint: The site theme is not applied to my custom page

If you created and/or customized pages in a SharePoint 2007 site, and when changing the site theme this change does not appears on the customized pages, one possible cause is the absence of the ASP.NET server tag that includes site themes on the page. Try placing the following markup on the <head> session of the page:

<SharePoint:Theme runat="server">

This server control writes the <link> tag that includes the selected theme CSS styles on the generated HTML.

Tuesday, February 2, 2010

Using Microsoft.SharePoint.dll to find out the ID of a list

When moving a site between web applications using SharePoint Designer backup (Site > Administration > Backup Web Site menu), several web parts wich had been converted to XSLT were displaying a "strange" message:

The server returned a non-specific error when trying to get data from the data source. Check the format and content of your query and try again. If the problem persists, contact the server administrator.

When an web part is converted to XSLT, SharePoint Designer places on the page a control of type WebPartPages:DataFormWebPart to display the data. This control uses the list ID to specify wich list is the data source (each list in a SharePoint site is given a GUID as its identifier, wich is stored in the list's ID property). Well, when the site was recreated at the destination, so were its lists, and that generated new ID's to them. And the web parts could not find their lists anymore, so they stopped working.

Being a restless geek, I decided to test the classes on Microsoft.SharePoint.dll by coding a little console app that listed the ID's from a SharePoint site lists. The code is bellow, and can be used as an intro to classes in namespace Microsoft.SharePoint. If you want just the app, just download the following file and run it from a command prompt on your SharePoint machine: SPListId.zip.

Imports Microsoft.SharePoint

Module Module1

Sub Main()
Try
Console.WriteLine()
If My.Application.CommandLineArgs.Count = 0 Then
Console.WriteLine("SPListId - Lists IDs from lists in a SharePoint site")
Console.WriteLine("Copyright (c) 2010 SrNimbus. No rights reserved ;)")
Console.WriteLine("Usage: SPListId site_url")
Return
End If
' The System.Uri class helps manipulating network addresses
Dim urlSite As Uri = New Uri(My.Application.CommandLineArgs(0))
' The Microsoft.SharePoint.SPSite class represents a site collection on SharePoint
Dim siteCollection As New SPSite(urlSite.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped))
' The Microsoft.SharePoint.SPWeb class represents a site on a site collection
Dim site As SPWeb = siteCollection.OpenWeb(urlSite.AbsolutePath)
Console.WriteLine("Lists on {0}:", urlSite.OriginalString)
Console.WriteLine("------------------------------------")
' The SPList class represents a SharePoint list
For Each lista As SPList In site.Lists
Console.WriteLine("{0}: {{{1}}}", lista.Title, lista.ID.ToString().ToUpper())
Next
Console.WriteLine("------------------------------------")
Catch erro As Exception
Console.WriteLine("Ooops:")
Console.WriteLine("{0} - {1}", erro.Source, erro.Message)
Finally
Console.WriteLine()
End Try
End Sub

End Module

Remember to add a reference to Microsoft.SharePoint.dll in order to compile the project. It is located on the SharePoint server, at %ProgramFiles%\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI.

Monday, January 25, 2010

SharePoint 2007 Content Deploy Quick Start Guide

And there was I working my 455 off preparing for teach Microsoft´s 50149 SharePoint Operations training when...
A word of advise: stay away from this training. The author tries to show EVERYTHING about SharePoint operations but there's no details on *any* of the topics shown. The result is frustrated and/or distracted students, and that's a saddening thing to me as an instructor - not to mention to the students...
Well, there I was studying the module on Content Deployment when I stumbled upon this guide from Stefan Gossner (great SharePoint blog!). It gives a quick and straightforward view on the main points about Content Deplyment on SharePoint 2007. Nice reading if you are new to the subject. Till now, the guide covers the following:

Part 1 - The Basics
Part 2 - The Basics continued
Part 3 - Configuration
Part 4 - Communication
Part 5 - Quick Deployment
Part 6 - Logging
Part 7 - Change Token Basics
Part 8 - Job and Change Token

Part 1 has an link index to the other parts. Enjoy!