How developers start to ignore code smells

Many people wonder how some developers blissfully ignore some best practices when writing code, or aren’t too bothered when they see a code smell in their project.
There are many explanations, but an old one is the code they see when working with Microsoft framework and samples (and not only Microsoft).

Even if Microsoft did great improvements in this direction in the recent years (clean code, best practices etc.), when some developers see code like the one below, in one of the most recent Microsoft frameworks, what conclusion will they draw? It’s from MS, so it must be right, no? :)

IdentityConfig.cs – part of the latest ASP.NET Identity 2.0 project template – 6 classes in one file:
IdentityConfig.cs

UserManager class – part of ASP.NET itself, new class added by Microsoft last year – the screenshot is truncated, it could be twice this size – I’m too tired to count how many public members are in there:
UserManager class

Posted in .NET, IT | Tagged , , , , | Scrie un comentariu

Service Bus for Windows Server: How to define authorization rules at topic level

This is just a ‘reminder’ post for myself (and maybe others) when encountering the same issue.
For Service Bus 1.0 for Windows Server (not Azure), at least on a server not joined to a domain, when using local (workgroup) Windows users for authentication, in order to define the topic authorization rules, the AllowRule.ClaimValue must be the username (without machinename\ in front).
The IssuerName must be the namespace name, and ClaimType must be “nameidentifier”.

An example:

const string userClaim = "nameidentifier";
string userName = "TestUser"; // actual name of the local Windows user
string issuerNamespace = "TestNamespace"; // maybe dynamically obtained using namespaceManager.Address.PathAndQuery.Replace("/", "")
List<AccessRights> accessRights = new List<AccessRights> {AccessRights.Listen};
//...
var topicDescription = namespaceManager.GetTopic("MyTopic");
topicDescription.Authorization.Add(
         new AllowRule(issuerNamespace, userClaim, userName, accessRights));
//...
namespaceManager.UpdateTopic(topicDescription);

Obviously, the local Windows user must exists on both the Service Bus server machine and on the client computer, with the same name and password, and the client application must run using this user.
This type of authentication, Windows integrated using ‘workgroup users’, not joined to a domain, is not quite supported by Microsoft, that assumes that all computers will be joined to a Windows domain, but it works so far.

The MSDN documentation on this issue is not helpfull at all:
http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.authorizationrule.claimvalue.aspx
or http://msdn.microsoft.com/en-us/library/windowsazure/jj193003.aspx
– just auto-generated stuff, with examples taken from Azure Service Bus and usually not updated for Service Bus for Windows Server.

Posted in .NET | Tagged , , , , | Un comentariu

On confusing error messages

This morning I stumbled upon a strange error message, while running a .NET console application:

The application has failed to start because its side-by-side configuration is in
correct. Please see the application event log or use the command-line sxstrace.e
xe tool for more detail.

All went fine until yesterday.. I was ready to start digging using sxstrace and other esoteric tools, learning again about assembly side-by-side loading (http://msdn.microsoft.com/en-us/library/windows/desktop/ff951640(v=vs.85).aspx) or searching for a misterios .manifest file that I was sure does not exists there.
Instead I decided to actually.. read what the error message was telling me: ‘Please see the application event log’ :)

I was not expecting to find anything there, but, in EventLog/Windows/Application log, was a much more helpful message:
Activation context generation failed for “E:\[....]Service.exe”.Error in manifest or policy file “E:\[....]Service.exe.Config” on line 33. Invalid Xml syntax.

Looking in the .config file, the problem was obvious: a missing quote (“) at the end of a config setting.. :)
Fixed that and the error is gone.

The conclusion.. wtf ?

Posted in .NET, C# | Tagged , , , | Scrie un comentariu

ADO.NET connection resiliency

In a previous post I mentioned a new feature that was going to be added in .NET Framework 4.5.1: ‘ADO.NET connection resiliency’.
Now that .NET 4.5.1 is RTM, I decided to check what exactly is this all about.I wrote a very simple console application, that uses ‘raw’ ADO.NET to connect to a database and repeatedly performs a simple query:

        static void Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;

            string queryString = "SELECT Id, FirstName, LastName FROM dbo.Persons WHERE FirstName = @firstName";
            string firstName = string.Empty;

            while (firstName.ToLowerInvariant() != "q")
            {
                Console.Write("First name: ");
                firstName = Console.ReadLine();

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    command.Parameters.AddWithValue("@firstName", firstName);

                    try
                    {
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            Console.WriteLine("\t{0}\t{1}\t{2}", reader[0], reader[1], reader[2]);
                        }
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }

All we do is to read a name, open a ADO.NET connection, execute a command, read the results, repeat.

Let’s build and run this code on a previous version of .NET Framework, let’s say 3.5:
Target NET 3.5
All works fine:
Run 3.5 Success

until somebody or something kills the connection from the ADO.NET connection pools that was still opened and connected to SQL Server:
CloseConnectionTcpView

If we try to execute a command on the database, ADO.NET uses the same connection from the pool, without knowing that it was closed by an external party:
Run 3.5 Error

This is not a permanent error and if we try again a new connection is created:

Run 3.5 Success

Let’s try to target .NET Framework 4.5.1 (under Visual Studio 2013):
Target .NET 4.5.1

Now, even if we kill the TCP/IP connection to the database server, no exception is triggered:
Run 4.5.1 Success

What happens under the covers? I didn’t found too much on MSDN, but most likely, ADO.NET catches the error and re-opens the connection.
All I found is a short info on a .NET 4.5.1 announcement post (http://blogs.msdn.com/b/dotnet/archive/2013/10/17/net-framework-4-5-1-rtm-gt-start-coding.aspx):
‘Under the covers, this new feature provides a robust connectivity system for recreating broken connections and re-trying transactions’

Unfortunately, it seems that this feature can be used only with SQL Azure or SQL Server 2014 (CTP2) (source).
This is somehow expected, because it’s a feature of the MS ADO.NET Provider for SQL Server, not ADO.NET in general.
Other third-party ADO.NET providers seem to have a similar feature for some time, like DataDirect ADO.NET providers:
http://www.datadirect.com/resources/resource-library/adonet-developer-center/adonet-tutorials/failover-support-in-datadirect-adodotnet-data-providers/connection-retry

It seems that by default, ADO.NET will retry only once, but this can be configured (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.connectretrycount(v=vs.110).aspx).

The same goes for the interval between retries (10s by default): http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.connectretryinterval(v=vs.110).aspx

Posted in .NET, C# | Tagged , , , | Scrie un comentariu

Connection resiliency in Entity Framework 6

The EF team has recently announced the new features added to EF 6 beta 1, and among them is one I found interesting: connection resiliency.
Despite the fancy name, it’s a simple concept: having built-in support for automatically retrying an EF operation, if the cause of the failure is a transient one, that has a high probablity to be “fixed” in a short interval of time.

While such “transient failures” are quite rare when using a single database server, located near the client (ex.: web application), on the same high-speed LAN, they are expected when the database server is located in a huge server farm, like Azure or Amazon ones. Expected not only once every blue moon, but quite often as servers are automatically relocated or connections are throttled when the load is high.
It’s true that such “retry” functionality it’s not so hard to build manually in our application, but it’s a repetitive process that I would rather have already available when needed.

As expected, currently this feature is available only on the EF provider for SQL Azure, and only in EF 6 beta 1 or newer. Since anyway I am on the bleeding edge on this case, I used the nightly EF6 build, taken with NuGet from MyGet feed (https://entityframework.codeplex.com/wikipage?title=Nightly%20Builds) instead of the official one.

Let’s write some code in a console application:

    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer<AdventureWorksContext>(null);

            Console.WriteLine("Persons starting with T: ...");
            using (var context = new AdventureWorksContext())
            {
                context.Database.Log = Console.Write;

                var personsStartingWithT = from p in context.Persons
                                           where p.FirstName.StartsWith("T")
                                           select p;

                foreach (Person person in personsStartingWithT)
                {
                    Console.WriteLine("{0} {1} {2}", person.Suffix, person.FirstName, person.LastName);
                }

                Console.WriteLine("\n\rPress any key to continue..");
                Console.ReadLine();

            }

        }
    }

So far nothing new.
I used the AdventureWorks2012 database, on a SQL Azure instance, so the connection string looks like this:

  <connectionStrings>
    <add name="AdventureWorksContext" providerName="System.Data.SqlClient" 
connectionString="Server=tcp:[.....].database.windows.net,1433;Database=AdventureWorks2012;User ID=tudor@[.......];Password=[.......];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
  </connectionStrings>

All we need to do in order to enable the connection resiliency for all actions is to add the SqlAzureExecutionStrategy to our custom DbConfiguration class, that is automatically discovered by EF at startup:

namespace TestConnectionResiliency.Models
{
    public class SqlAzureDbConfiguration : DbConfiguration
    {
        public SqlAzureDbConfiguration()
        {
            AddExecutionStrategy(() => new SqlAzureExecutionStrategy());
        }
    }
}

To test if it works, since it’s a bit hard to wait for a transient failure to happen on SQL Azure, I just disabled the network connection on the client just before the query was about to execute, and I enabled it back a few seconds later. The log on the debug console say it all:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
'TestConnectionResiliency.vshost.exe' (Managed (v4.0.30319)): Loaded 'EntityFrameworkDynamicProxies-TestConnectionResiliency'
...
The thread '<No Name>' (0x2020) has exited with code 0 (0x0).
The program '[8832] TestConnectionResiliency.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

The timeout exception was thrown by ADO.NET, but EF caught it and retried the operation until it was successful.

What exactly is the EF provider for SQL Azure doing in this case?
Easy to find out – we have the source code for DbExecutionStrategy.cs and SqlAzureRetriableExceptionDetector.cs.

If a SqlException with a certain error code (full details in SqlAzureRetriableExceptionDetector.cs), or a TimeoutException, is thrown by the underlying provider, the operation is retried no more than 5 times until is successful, using exponentially increasing (random) delays between retries.
The formula used is: MIN(random(1, 1.1) * (2 ^ retryCount - 1), maxDelay)
where retryCount is by default 5, and maxDelay is by default 30s.
In other words, it retries slower and slower each time, but no slower than 30 seconds.
This is an exponential backoff algorithm, that is clasically used in such cases.
The list of SQl error codes that trigger a retry is similar with the ones from http://windowsazurecat.com/2010/10/best-practices-for-handling-transient-conditions-in-sql-azure-client-applications/ .

Some context: even since Azure was launched, various teams within Microsoft have tried to implement something similar:
– the ‘Transient Fault Handling Framework for SQL Azure, Windows Azure Storage, Service Bus & Cache’ (http://windowsazurecat.com/2011/02/transient-fault-handling-framework/) from the former Azure CAT (Customer Advisory Team)
– followed by Enterprise Library Transient Fault Handling Application Block (http://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx) included in Enterprise Library 5.0 Integration Pack for Windows Azure by P&P team
– now EF 6 will includ this built-in: https://entityframework.codeplex.com/wikipage?title=Connection%20Resiliency%20Spec
– the Windows ODBC Driver for SQL Server/Azure offers something similar: ‘Connection Resiliency in the Windows ODBC Driver
– and in .NET 4.5.1 preview, it might seem that something similar will be included inside ADO.NET itself, as ‘ADO.NET idle connection resiliency’ (I found no details about this one yet)

Azure container

Source: Microsoft: http://news.cnet.com/2300-10805_3-10001679-1.html (Chicago Data Center)

Posted in .NET, Azure, Entity Framework, SQL Server | Tagged , , , | Un comentariu

Some multi-threading gotchas

Most programmers know that they must make their code thread-safe when accessing shared data from multiple threads at the same time.
However, there are some cases which are not so obvious and which might cause very subtle bugs that appear only in some particular circumstances.

Below is a list of such cases that I started to ‘collect’. Probably for some programmers the next examples are nothing new, but at least for me they were ‘unexpected’, at least initially when I read about them:

Example 1:

    class Foo
    {
        int _answer;
        bool _complete;

        public void A()
        {
            _answer = 123;
            _complete = true;
        }

        public void B()
        {
            if (_complete) Console.WriteLine(_answer);
        }
    }

Seems harmless and thread-safe, right? Well, not allways :) – if A() and B() are executed in parallel from multiple threads, in some particular cases Console.WriteLine will display… 0 (zero).
Source and explanation: http://www.albahari.com/threading/part4.aspx#_Memory_Barriers_and_Volatility – in short, the two assignments could be reordered on some processors.

Example 2:

    class Program
    {
        static void Main()
        {
            bool complete = false;
            var t = new Thread(() =>
                {
                    bool toggle = false;
                    while (!complete) // waiting for the flag to be set...
                    {
                        toggle = !toggle; // do something :)
                    }
                });
            t.Start();

            Thread.Sleep(1000); // wait a bit to let the thread to run..
            complete = true; // 'signal' the thread

            t.Join();        // wait the thread to complete - blocks indefinitely (!)
        }
    }

The thread in the above code will never complete (if the code is compiled in release mode with optimizations on).
Details and a solution again at: http://www.albahari.com/threading/part4.aspx#_Memory_Barriers_and_Volatility

Example 3:

    class Test
    {
        private bool _flag = true;
        public void Run()
        {
            // Set _flag to false on another thread
            new Thread(() => { _flag = false; }).Start();
            // Poll the _flag field until it is set to false
            while (_flag) ;
            // The loop might never terminate!
        }
    }

Notice the ‘might’ – sometimes it will terminate, sometimes the .NET JIT compiler will decide to ‘cache’ the _flag like so:
if (_flag) { while (true); }
I managed to reproduce the ‘never-ending loop’ by compiling on release mode, optimizations enabled, on 1 out of 3 tries.
Source: http://msdn.microsoft.com/en-us/magazine/jj883956.aspx

Example 4:

    class Program
    {
        class Test
        {
            private decimal _x;

            public void Write(decimal newValue)
            {
                _x = newValue; // just a 'simple' write
            }

            public void Read()
            {
                decimal v = _x; // read the written value
                Console.WriteLine(v);
            }

        }

        static void Main()
        {
            Test f = new Test();

            const int numThreads = 100;
            Thread[] threads = new Thread[numThreads];
            for (int i = 0; i < numThreads; i++)
            {
                if (i % 2 == 0)
                {
                    int k = i;
                    threads[i] = new Thread(() => f.Write(8999999999999999999999999999m));
                }
                else
                {
                    threads[i] = new Thread(f.Read);
                }
            }
            // some threads write while other read from the same memory location
            for (int i = 0; i < numThreads; i++)
            {
                threads[i].Start();
            }

            // wait for all threads to complete
            for (int i = 0; i < numThreads; i++)
            {
                threads[i].Join();
            }
            Console.ReadLine();
        }
    }

Is it possible to read a value that was never written? Yes :)
Source and explanation: http://www.bluebytesoftware.com/blog/2006/02/08/ThreadsafetyTornReadsAndTheLike.aspx
Why? decimal _x = newValue;
is not an atomic operation, even on 64-bit processors (decimal is stored on 128 bits).
Even a long x = 1231243124214423; might be non-atomic on 32-bits processors.
An example that better reproduces the issue described above can be found at: http://stackoverflow.com/questions/11360645/should-i-always-synchronize-access-to-all-double-field-property-variables-that

The conclusion? Try to avoid sharing state and instances between multiple threads, but if you can’t, make sure you read twice the excellent article on this topic by Joseph Albahari: http://www.albahari.com/threading/
Do not assume the code will always run on x86-x64 architecture where you can make some some assumptions – even if .NET 4.5 does not support Itanium processors anymore, recently .NET code started to execute on ARM processors, that have their own different instruction set.

Posted in .NET, C# | Tagged , , | Scrie un comentariu

Tommy

Tommy (3)Tommy (1)Tommy (2)

Tommy, a set on Flickr.

Via Flickr:
My 2’nd cat (one year old)

Posted in Uncategorized | Scrie un comentariu