vrijdag 21 augustus 2015

Set and reset CultureInfo inside a using block

Sometimes you want to set a specific CultureInfo, do something and afterwards reset to the original CultureInfo. We wanted to do that when rendering a pdf, which had to work with dates in the correct format.

So what we did was this:

var originalCultureInfo = Thread.CurrentThread.CurrentCulture;
var culture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = culture;
RenderPdf();
Thread.CurrentThread.CurrentCulture = originalCultureInfo;


But that can be made a lot simpler, with less risk of forgetting to reset the CurrentCulture on the last line:

using (new CustomCulture("fr-FR"))
{
   RenderPdf();
}

The key is writing a CustomCulture class that inherits from IDisposable:

public class CustomCulture : IDisposable
{
   readonly CultureInfo _originalCultureInfo;

   public CustomCulture(string name)
   {
      _originalCultureInfo = Thread.CurrentThread.CurrentCulture;
      Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
   }

   public void Dispose()
   {
      Thread.CurrentThread.CurrentCulture = _originalCultureInfo;
   }
}

maandag 20 april 2015

Technical stories on the backlog

How can you add technical stories to the backlog when the product owner wants every story to have business value? Let's take the following titles as an example:

Connect the monitoring system to ServiceX.
Use the configuration system and not the web.config for settings of ServiceX.

As a developer these titles are perfectly fine. As a product owner the titles are too technical. One solution is to rewrite the titles in the following way:

As operations staff I want to monitor aspect A of ServiceX.
As operations staff I want to configure ServiceX with the configuration system.

woensdag 30 juli 2014

Authenticate Json Web Token in ASP.Net Web API

public class JwtMessageHandler : DelegatingHandler
{
   protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   {
      var authorizationParameter = request.Headers.Authorization.Parameter;

      var handler = new JwtSecurityTokenHandler();


      var x509Certificate2 = new X509Certificate2(@"c:\temp\ADFS Signing.cer");


      var x509SecurityToken = new X509SecurityToken(x509Certificate2);


      var tokenValidationParameters = new TokenValidationParameters

      {
         SigningToken = x509SecurityToken,
         AllowedAudience = "[relyingparty identifier]",
         ValidIssuer = "[Federation Service identifier]"
      };

      ClaimsPrincipal res;


      res = handler.ValidateToken(authorizationParameter, 

                                  tokenValidationParameters);

      HttpContext.Current.User = res;

      Thread.CurrentPrincipal = res;

      return base.SendAsync(request, cancellationToken);
   }
}

The JwtMesssageHandler can be configured in the global.asax:

public class WebApiApplication : System.Web.HttpApplication
{
   protected void Application_Start()
   {
      // other configuration

      GlobalConfiguration.Configuration.MessageHandlers.Add(new JwtMessageHandler());

   }

}

maandag 24 februari 2014

zondag 23 februari 2014

Asynchronous Programming Model

This is a good explanation of how to make asynchronous calls with the Asynchronous Programming Model ( begin/end):

Calling Synchronous Methods Asynchronously

maandag 27 januari 2014

Competing consumers and returning results with in-memory-queues (BlockingCollection)

I needed a way to to put some work in a queue, consume it and return the result to the producer of the work. In C# we can use the BlockingCollection class and TaskCompletionSource for that.

The code in this blogpost is based on the code from Albahari.

What I want to do is this create a Producer which creates work and places that on a queue. I'll also want to create Consumers which take work off the queue and send the result back to the Producer when they are done. See the following image for an overview:





Let's start with a simple console application:
(Don't forget to set it to the correct framework, as VS 2010 defaults to client framework 4.0)

class Program
{
   static void Main()
   {
      // Put something on a queue
   }
}


This is the class that we are going to put on the queue:



public class WorkItem
{
   public readonly TaskCompletionSource<int> TaskSource;
   public readonly int Context;
   public readonly CancellationToken? CancelToken;

   public WorkItem(
      TaskCompletionSource<int> taskSource,
      int context,
      CancellationToken? cancelToken)
   {
      TaskSource = taskSource;
      Context = context;
      CancelToken = cancelToken;
   }

}


Next we create the queue we are going to use for enqueueing the work:


public class ProducerConsumerQueue : IDisposable
{
   readonly BlockingCollection<WorkItem> _workitemQueue =
      new BlockingCollection<WorkItem>();

   public Task<int> EnqueueTask(int context)
   {
      return EnqueueTask(context, null);
   }

   public Task<int> EnqueueTask(int context,
                                   CancellationToken? cancelToken)
   {
      var tcs = new TaskCompletionSource<int>();
      _workitemQueue.Add(new WorkItem(tcs, context, cancelToken));
      return tcs.Task;
   }

   public void Dispose()
   {
      _workitemQueue.CompleteAdding();
   }
}


As you can see our ProducerConsumerQueue uses a BlockingCollection internally. This is a great class that let's multiple consumers/workers read from it in a thread-safe manner.

Next, create the consumer:



public class Consumer
{
   private readonly BlockingCollection<WorkItem> _workitemQueue;

   public Consumer(BlockingCollection<WorkItem> workitemQueue)
   {
      _workitemQueue = workitemQueue;
   }

   public void Consume()
   {
      foreach (WorkItem workItem in _workitemQueue.GetConsumingEnumerable())
      {
         if (workItem.CancelToken.HasValue &&
             workItem.CancelToken.Value.IsCancellationRequested)
         {
            workItem.TaskSource.SetCanceled();
         }
         else
         {
            try
            {
               Thread.Sleep(1000); // simulate work

               workItem.TaskSource.SetResult(workItem.Context + 1);
            }
            catch (OperationCanceledException ex)
            {
               if (ex.CancellationToken == workItem.CancelToken)
               {
                  workItem.TaskSource.SetCanceled();
               }
               else
               {
                  workItem.TaskSource.SetException(ex);
               }
            }
            catch (Exception ex)
            {
               workItem.TaskSource.SetException(ex);
            }
         }
      }
   }

}



So how do we start the consumers?
We do that in the constructor of the ProducerConsumerQueue:


   public ProducerConsumerQueue(int workerCount)
   {
      for (int i = 0; i < workerCount; i++)
      {
         var consumer = new Consumer(_workitemQueue);

         Task.Factory.StartNew(consumer.Consume);
      }
   }

Finally we can finish the console application:

class Program
{
   static void Main()
   {
      var producerConsumerQueue = new ProducerConsumerQueue(2);

      Task<int> task1 = producerConsumerQueue.EnqueueTask(1);
      Task<int> task2 = producerConsumerQueue.EnqueueTask(2);
      Task<int> task3 = producerConsumerQueue.EnqueueTask(3);

      Console.WriteLine(task1.Result);
      Console.WriteLine(task2.Result);
      Console.WriteLine(task3.Result);

      Console.ReadLine();
   }
}


If we run this, we will start three tasks. Each task will get an input value, add one to it, and return the value to the console app. WE start with two consumers, so after one second the first two tasks finish and the following output is printed:

2
3

A second later the third task finished, and we will see this:

4

You can play around with the number of consumers and the number of enqueued WorkItems.