Few considerations about .NET and Timezones in web scenarios

Probably every web developer has already had topics around time zones. I again had it. I just want to make another notice for the developer community around the globe (and my future me) how I came around this topic now.
In .NET there are two ways to get the time zone (why having only one):

TimeZone.CurrentTimeZone // type System.TimeZone
TimeZoneInfo.Local // type System.TimeZoneInfo

The TimeZoneInfo is the newer one satisfying some features required in Vista or something :). With this class one could also convert times into different time zones using the TimeZoneInfo.Id:

// TimeZoneInfo.Local.Id is something like "W. Europe Standard Time"
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, TimeZoneInfo.Local.Id);

Altogether this whole topic is not so nice. It is not so easy to reproduce problems when developing against a local web server. I managed to do a “hacky” workaround:

  1. Stop the IIS Express processes
  2. Set local time zone at the taskbar’s clock usually on the bottom right of your desktop – so defining the time zone of the web server
  3. Start the debugging – this starts a IIS Express for you with your chosen time zone
  4. Add watch TimeZoneInfo.Local – so that you see what the current time zone is.
    You will need it, to be always aware what the current server time zone is. It was also written somewhere that this information is not thread and not even process safe.
  5. While running the app, change to your favorite client time zone on your clock, so that your browser’s time zone is according to your problem to reproduce

My specific problem was on Kendo grids where binding to a DateTime struct happens automatically by specifying the DateTimeKind…
I know it is hacky – but it’s working – hope I helped someone.

Advertisements
Posted in Technical Interest | Tagged , | Leave a comment

Entity Framework Code First Migrations in multiple branch scenarios

The starting information how the situation related to merging migrations can be read in this great post from Anders Abel.

I just have an extension to it for a specific situation.

Simple case merge migration

This is the case when the merged migration (from the source branch) is the last migration on the target branch. The code to that is fairly easy described in the above linked post. You only need to re-scaffold the code first model (in the designer file of the migration). Just a notice: don’t use the “force” option here, so you don’t add all the changes to your merged migration. Example below:

Add-Migration MergedMigrationClassName

Complicated case merge migration

If your migration is not the last one in the target branch, you have to do a couple of other things.

  1. Clean solution
  2. Delete database (for simplicity, it would be enough to update/downgrade the database to the target merged migration)
  3. Exclude migrations after the merged migration from the project
  4. Re-scaffold the merged migration as described above
  5. Include one of the excluded migrations
  6. Re-scaffold it as described above
  7. Repeat step 5 and 6 till you included all migrations

This way was working for me, however it is not so intuitive. Please comment on my post if you find an easier way.

Posted in Technical Interest | Tagged , , , , | Leave a comment

Dealing with Entity Framework Code First Migrations in a configuration way

I’ve promised in one of my last posts, that I’m going to tell you about our way of using Code First Migrations. If you want to read about it, you can do it here.
For this purpose I’ve created some generic classes as described below. The main goal was to be able to configure Migrations from the web.config and to be able to unit test the migrations, so that when the deployment comes, some quality assurance already happens. A perfect stack overflow site provide perfect guidance on testing whether for every change a migration exists and testing all migrations up and down.
First I’ve created a generic Configuration class as follows including the calling of some data initializers:

using System.Collections.Generic;
using System.Data.Entity.Migrations;

namespace Common.Migrations
{
    /// <summary>
    /// Base class for migration configurations.
    /// </summary>
    /// <typeparam name="TContext">The type of the context.</typeparam>
    public abstract class MigrationsConfigurationBase<TContext> : DbMigrationsConfiguration<TContext> where TContext : System.Data.Entity.DbContext
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MigrationsConfigurationBase{TContext}"/> class.
        /// </summary>
        public MigrationsConfigurationBase()
        {
            ContextKey = typeof(TContext).Name;
        }

        /// <summary>
        /// Gets or sets a value indicating whether data inevitable for the working of the system is to be seeded or not.
        /// </summary>
        public bool SeedBaseData { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether seed demo data.
        /// </summary>
        public bool SeedDemoData { get; set; }

        /// <summary>
        /// Gets the initializers.
        /// </summary>
        protected virtual List<IDataInitializer> Initializers
        {
            get { return new List<IDataInitializer>(); }
        }

        /// <summary>
        /// Determines whether a seed is needed based on e.g.: existance of specific entities in the database.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns> whether a seed is needed based on e.g.: existance of specific entities in the database.</returns>
        protected abstract bool SeedNeeded(TContext context);

        /// <summary>
        /// Seeds the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void Seed(TContext context)
        {
            InitializeSeed(context);
            if (SeedBaseData)
            {
                // No initialization is needed when we already have data there.
                if (SeedNeeded(context))
                {
                    var initializers = Initializers;

                    initializers.ForEach(x =>
                    {
                        x.Seed(context, SeedDemoData);
                        SaveChangesBetweenInitializerSeeds(context);
                    });
                }
            }
        }

        /// <summary>
        /// Initializes the seed.
        /// </summary>
        /// <param name="context">The context.</param>
        protected virtual void InitializeSeed(TContext context)
        {
            // No inevitable initialization needed.
        }

        /// <summary>
        /// Saves the changes between initializer seeds.
        /// </summary>
        /// <param name="context">The context.</param>
        protected virtual void SaveChangesBetweenInitializerSeeds(TContext context)
        {
            // Do nothing, as it is not always needed.
        }
    }
}

After that we need a database initializer deriving from MigrateDatabaseToLatestVersion to be able to get the configuration from the web.config:

using System.Configuration;
using System.Data.Entity;
using System.Reflection;

namespace Common.Migrations
{
    /// <summary>
    /// Database initializer for code first migrations
    /// </summary>
    /// <typeparam name="TContext">The type of the context.</typeparam>
    /// <typeparam name="TConfiguration">The type of the configuration.</typeparam>
    public class MigrationsDatabaseInitializer<TContext, TConfiguration> : MigrateDatabaseToLatestVersion<TContext, TConfiguration>
        where TContext : DbContext
        where TConfiguration : MigrationsConfigurationBase<TContext>, new()
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MigrationsDatabaseInitializer{TContext, TConfiguration}"/> class.
        /// </summary>
        /// <param name="seedBaseData">If true then data inevitable for the working of the system is to be seeded.</param>
        /// <param name="seedDemoData">The seed demo data.</param>
        /// <param name="allowAutomaticMigrations">The allow automatic migrations.</param>
        public MigrationsDatabaseInitializer(string seedBaseData, string seedDemoData, string allowAutomaticMigrations)
        {
            var config = GetConfig();
            config.SeedBaseData = ParseConfigBool(seedBaseData);
            config.SeedDemoData = ParseConfigBool(seedDemoData);
            config.AutomaticMigrationsEnabled = ParseConfigBool(allowAutomaticMigrations);
        }

        /// <summary>
        /// Parses the configuration bool.
        /// </summary>
        /// <param name="configValue">The configuration value.</param>
        /// <returns>The parsed configuration value.</returns>
        private bool ParseConfigBool(string configValue)
        {
            bool result;
            if (!bool.TryParse(configValue, out result))
            {
                throw new ConfigurationErrorsException(
                    string.Format(
                        "There is an error in the migration database initializer configuration for the value: {0}",
                        configValue));
            }

            return result;
        }

        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <returns>The configuration</returns>
        private TConfiguration GetConfig()
        {
            var databaseInitializerType = typeof(MigrateDatabaseToLatestVersion<TContext, TConfiguration>);
            var configField = databaseInitializerType.GetField("_config", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
            if (configField != null)
            {
                return (TConfiguration)configField.GetValue(this);
            }

            return null;
        }
    }
}

It is probably unavoidable to see the hack using reflection for getting the config field. I just didn’t find a better way, if you have, please comment on this post.
After all these things now you only need to configure it from the web.config (using the string parameters of the MigrationsDatabaseInitializer) as follows:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <contexts>
      <context type="DAL.Context.Customer.CustomerContext, DAL">
        <databaseInitializer type="Common.Migrations.MigrationsDatabaseInitializer`2[[DAL.Context.Customer.CustomerContext, DAL], [DAL.Migrations.CustomerContextConfiguration, DAL]], DAL" >
          <parameters>
            <!-- Check the MigrationsDatabaseInitializer constructor parameters to see what the meaning of them is -->
            <parameter value="true" />
            <parameter value="true" />
            <parameter value="false" />
          </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>

If you wanna know more about configuring Initializers in the entity framework config section you can read it here. Another really interesting topic about migrations and multiple branches can be read here: http://stackoverflow.com/questions/10171658/lose-ef-code-first-migration-when-working-on-different-tfs-branches.
You’ll certainly wan’t to derive from the configuration class for you specific context and want to create initialzers after all, but I’m pretty sure you’ll find it out – if not please write me a comment!

Posted in Technical Interest | Tagged , , | Leave a comment

EmailTraceListener and NtWaitForMultipleObjects

We had a problem on our windows service, where for some reason after about a half an hour I couldn’t see any processings in the logs anymore.

Till that point nothing was wrong. When I tried to stop the service, I had the error: “error 1053 the service did not respond to the start or control“. In the event log I could sometimes find “Problem: The Windows Search service terminated unexpectedly“.

I’ve tried many things. I thought that maybe the synchronization of the task processing is wrong. Or that a lock was on a wrong place causing a deadlock somehow and so on. After many tries and fixes (which certainly did good to the service) I decided to create a memory dump from the service on the server.

I started Windbg on it and immediately recognized that I have tons of threads (over 2000). Ran !syncblk but didn’t find any blocks between them. I took a look at the stack of them and on the top I’ve seen a lot of NtWaitForMultipleObjects.

Ok what causes that?

Parallel to that suddenly the situation happened locally. I was really happy. Immediately attached a debugger and searched the threads for something with Wait in their stack. I’ve found them all – thousands of them – they contained “Monitor.Wait()“.

Ok now I knew what to search for. Restarted the service and gone step by step through the methods watching parallel the Threads window with the filter “Monitor.Wait()“. I was excited to wait for the first occurrence . It came fast. When I logged an entry. I just have started to comment out the trace listeners from the Enterprise Library logging configuration section. In this way I pointed out the EmailTraceListener. Checked it with dotPeak – found that the SmtpClient (used by it) uses Timers… Probably this was it… Now we are probably going to think on, how to send log emails after that… Hope it helped someone.

Fazit

In the end it turns out that the correct usage of the Enterprise Library Logging solves the most of the problem. The problem came when we have been using the disposable logwriter without disposing:

var logger = new LogWriterFactory().Create();
logger.Write(logEntry);

The correct usage is when you set the log writer somewhere in the beginning of your processing (or application start for ASP.NET) and then using always static Logger.LogWrite as follows:

// Registering the log writer somewhere in the beginning of the process:
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(new LogWriterFactory(configurationSource).Create());

// Creating logs throughout the app
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(logEntry);

The above initialization can be extended if using configuration described here.

Posted in Technical Interest | Tagged , , , | Leave a comment

Transposing Entity Framework EDMX to Code First

Not so long ago we’ve been turning our Entity Framework usage from the “database project + edmx” approach to the “Code First” way. Why did we do that? Because of the pain at deployments when not having “Entity Framework Migrations”. I’ll just list up some of the problems we had, and our solutions to them.

One of the most important points here: when trying to do the same thing, use a schema comparison tool, with which you can ignore column orders :) like SQL Examiner. I’ve found it particularly useful and the trial period is certainly enough for the time of the transposition. It’s a great tool and I’ll probably buy it when I need it again.

Another thing what you probably wanna do is to reverse engineer the database where you have your entities stored with the Entity Framework Power Tools. This is going to create some starting point for your EntityTypeConfiguration maps for the separate entities and contexts.
After doing so you may recognize the following things:

  • The precision is to be fixed for almost every decimal fields
  • Indices were not always correctly created – you have to create them correctly in the EntityTypeConfiguration classes.
  • Foreign keys are not always correctly created – you’ll sometimes need the HasForeignKey extender method
  • Default and check Constraints should be created for example through migrations – if you really need them
  • Stored procedures can be created also by migrations, however the usage of them will be interesting for example as a method on the DbContext class as follows:
            public virtual string RunSomeSp(int id)
            {
                using (var connection = new SqlConnection(Database.Connection.ConnectionString))
                {
                    connection.Open();
                    var command = new SqlCommand("SomeSp", connection);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new SqlParameter("@Id", id));
                    command.Parameters.Add(new SqlParameter("@SomeOutputParam", SqlDbType.NVarChar, 255, ParameterDirection.Output, false, 0, 0, "SomeOutputParam", DataRowVersion.Default, null));
                    command.ExecuteNonQuery();
                    return (string)command.Parameters["@SomeOutputParam"].Value;
                }
            }
    

    You need it in this way to communicate with the same connection string as the context. And yes it is sadly ADO.NET as the EDMX generated code won’t work with code first.

  • Initial data creation could be solved by using some macro functionality of a text editor
  • Initial int seed values can be configured also in a migration like: Sql(“dbcc checkident(YourTableName, reseed, 500000)”);
  • Database options are a bit more tricky as they cannot be set in multipart transactions (in which migrations run) so we decided to do them manually

In a following post I’m going to talk about how we have been using, configuring and unit testing the Entity Framework Code First Migrations.

Posted in Technical Interest | Tagged , , , , | Leave a comment

WaitAllOneByOne pattern

I’ve just started to deal with parallel programming by watching a great video from Dr. Joe Hummel on pluralsight over here. He is a brilliant author and this course is perfect for learning the basics. I decided to base on his code snippets (which are far better than the ones provided here) to practice a little bit. I’ve also included exception handling.

One of the most interesting thing was in its second module to describe the WaitAllOneByOne pattern. I’ve decided to create a generic implementation of it based on the snippets provided in the course.

This is useful if

  • We would like to wait till all tasks finish, but process results as each one completes
  • When result processing may overlap with the computation
  • Some of the tasks may fail
    public class WaitAllOneByOne<TResult> : IDisposable
    {
        private readonly List<Task<TResult>> _tasks;
        private readonly Action<TResult> _processResult;
        private readonly HandleExceptions _handleExceptions;
        private readonly bool _handleUnobservedAggregateExceptions;

        /// <summary>
        /// Delegate for handling exceptions.
        /// </summary>
        /// <param name="flattenedExceptions">The flattened exceptions of the aggregate exaptions coming from the task hierarchy.</param>
        /// <param name="observed">Whether the exception was observed when dealing with the tasks result, or it was unexpected and not caught at the Wait or Result usage.</param>
        public delegate void HandleExceptions(List<Exception> flattenedExceptions, bool observed);

        /// <summary>
        /// Initializes a new instance of the WaitAllOneByOne class.
        /// </summary>
        /// <param name="processResult">Action for processing the result.</param>
        /// <param name="handleExceptions">Action to deal with exceptions.</param>
        /// <param name="handleUnobservedAggregateExceptions">Determines whether to handle unobserved task exceptions or not. 
        /// It is generally not a good idea to handle them at too many places of the application. So this should be false.
        /// It is just showing how to handle unobserved task exceptions.</param>
        /// <param name="tasks"></param>
        public WaitAllOneByOne(Action<TResult> processResult, HandleExceptions handleExceptions, bool handleUnobservedAggregateExceptions, params Task<TResult>[] tasks)
        {
            _handleUnobservedAggregateExceptions = handleUnobservedAggregateExceptions;
            _handleExceptions = handleExceptions;
            _processResult = processResult;
            _tasks = new List<Task<TResult>>(tasks);
            if (handleUnobservedAggregateExceptions)
            {
                HandleUnobservedTaskException();
            }
        }

        /// <summary>
        /// Starts the tasks and waits for all one by one.
        /// </summary>
        /// <returns>True if at least one task did finish without an uncaught exception in it.</returns>
        public bool StartTasks()
        {
            _tasks.ForEach(t => t.Start());
            bool oneSucceded = false;

            while (_tasks.Count > 0)
            {
                // ReSharper disable once CoVariantArrayConversion 
                // as we know by the generic type parameter that the results are of the same type
                var index = Task.WaitAny(_tasks.ToArray());
                var finishedTask = _tasks[index];

                // Looking at the exception property makes it observed so it's not rethrown at GC
                if (finishedTask.Exception == null)
                {
                    _processResult(finishedTask.Result);
                    oneSucceded = true;
                }
                else
                {
                    _handleExceptions(FlattenAggregateExceptions(finishedTask.Exception), true);
                }

                _tasks.RemoveAt(index);
            }

            return oneSucceded;
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            // Prevent rethrow on garbage collection
            _handleExceptions(FlattenAggregateExceptions(e.Exception), false);
            e.SetObserved();
        }

        private void HandleUnobservedTaskException()
        {
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        }

        /// <summary>
        /// Flattens the aggregate exaptions coming from the task hierarchy into a list.
        /// </summary>
        /// <returns>The flattened list of exceptions.</returns>
        private List<Exception> FlattenAggregateExceptions(AggregateException ae)
        {
            return ae.Flatten().InnerExceptions.ToList();
        }

        public void Dispose()
        {
            if (_handleUnobservedAggregateExceptions)
            {
                TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
            }
        }
    }

The code which I’ve provided here is not widely tested yet. It’s important to say, that I’m just practicing by it, use it as such, and of course watch the pluralsight course of Joe. He also talks about cancellation and a lot of further caveats…

Posted in Technical Interest | Tagged , | 1 Comment

Some considerations about writing Windows Services

For posts like that I normally use Twitter, but it is not reachable currently – whow. This is just a reminder post :).
We have written a windows service in our application. The original goal was to reduce workload by only working up a piece at once. For some of the problems occurring the article here gave us starting points. With this post I just want to remind myself not to forget the old experience from this //TODONT. If you’re still writing worker services with timers, consider reading this here. If you don’t want to have timer elapsed handling parallel, then you can you can check this forum answer here. If you would wanna rewrite your service not to have timers in place check this article here.

Posted in Technical Interest | Tagged | Leave a comment