Wednesday, July 31, 2013

Going from Tasks back to IAsyncResult(the Asynchronous Programming Model pattern)

There are lot’s of posts out there talking about how to go from the traditional APM approach(using the typical BeginXXX and EndXXX syntax with IAsyncResult) to the Task based approach. Probably the easiest way is by using the Task.Factory.FromAsync method.

But what if you want to do it the other way around and want to go back from a task to the APM way of working?

In my specific case I wanted to wrap the async version of ExecuteNonQuery that returns a Task<int>:

Task<int> ExecuteNonQueryAsync();

It had to be mapped to an APM implementation with the following signatures:

IAsyncResult BeginExecuteNonQuery(DbCommand command, AsyncCallback callback, object state);
int EndExecuteNonQuery(IAsyncResult asyncResult);

I started by implementing it myself but there a lot of edge cases, so I was happy to find the following extension method :

The implementation of my BeginExecuteNonQuery method became:

And the implementation of my EndExecuteNonQuery method became:

A similar solution could be found on Stephen Cleary’s blog who created a useful Helper library for async task-based programming called Nito AsyncEx.

Tuesday, July 30, 2013

The Reactive Manifesto

After the Agile Manifesto it is now time for the Reactive Manifesto, focusing on the why and how of Reactive systems…

reactivehazard

Monday, July 29, 2013

Enable NuGet package restore on the build server

When trying to build a project on our TFS build server, it failed with the following error message:

c:\b\1\AppName\.nuget\nuget.targets (88): Package restore is disabled by default. To give consent, open the Visual Studio Options dialog, click on Package Manager node and check 'Allow NuGet to download missing packages during build.' You can also give consent by setting the environment variable 'EnableNuGetPackageRestore' to 'true'.

For licensing reasons you have to give consent at least once when using NuGet. The exception message already points us to the solution: we need to add an environment variable.

To do this, log into the build server and execute the following steps:

  1. Click Start and right-click on Computer.
  2. Choose Properties from the context menu
  3. Choose Advanced System Settings from the System window.
  4. Choose the Advanced tab in the System Properties window and click the Environment Variables button.
  5. In the System Variables section, click the "New..." button.
  6. Enter "EnableNuGetPackageRestore" for the "Variable name" field
  7. Enter "true" for the "Variable value" field and click the OK button.
  8. Restart the TFS build service(or server) and retry your build

The error should be gone…

Friday, July 19, 2013

Team Foundation Server: Map different branches to the same folder using the SAK

Last week when giving a Team Foundation Server for Administrators course, I got a great tip from one of the students(thanks Guy!).
Let’s first set the scene and explain the situation. For every release they make a new branch is created(and they do a lot of releases). By default every branch is mapped to a different folder so if you don’t clean up your workspace you end up with lots of folders each containing a specific (released) version of the source code.
Because they only need one release on disk at a time, they decided to update the workspace mapping to put the active release in a ‘Release’ folder on their local disk. So with every new release, they update the mapping and map a different release to this ‘Release’ folder. Everyone is still with me?
Now the problem is that when they try to load the solution in the release folder, Visual Studio gets confused and gives the following message:
There appears to be a discrepancy between the solution's source control information about some project(s) and the information in the project file(s).
To resolve this discrepancy it will be necessary to check out the project file(s) and update them. If the check out fails, however, and the solution is closed without saving, you will see this warning again the next time you open the solution.
Clicking OK eventually leads to a checkout  of your solution file and the whole list of project files.
Now what I didn’t know is that you can fix this if you don’t want to see this message. Open up your csproj files and find the following lines:
<SccProjectName>$/MyProject/Hello</SccProjectName>
<SccAuxPath>http://servername:8080/tfs/defaultcollection</SccAuxPath>
<SccLocalPath>.</SccLocalPath>
<SccProvider>{11111111-1111-1111-1111-111111111111}</SccProvider>
Change these values to:
<SccProjectName>SAK</SccProjectName>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
<SccProvider>SAK</SccProvider>
After doing this, Visual Studio will complain no more next time you map a different branch to the same folder.


Remarks:

Thursday, July 18, 2013

Guidelines for Migrating an Application Built Using WIF 3.5 to WIF 4.5

When going from WIF 3.5 to 4.5 a lot of (breaking) changes were introduced. To help you walk through the migration process, Microsoft created 2 useful articles on MSDN:

From the article:

Windows Identity Foundation (WIF) was originally released in the .NET 3.5 SP1 timeframe. That version of WIF is referred to as WIF 3.5. It was released as a separate runtime and SDK, which meant that every computer on which a WIF-enabled application ran had to have the WIF runtime installed and developers had to download and install the WIF SDK to get the Visual Studio templates and tooling that enabled development of WIF-enabled applications. Beginning with .NET 4.5, WIF has been fully integrated into the .NET Framework. A separate runtime is no longer needed and the WIF tooling can be installed in Visual Studio 2012 by using the Visual Studio Extensions Manager. This version of WIF is referred to as WIF 4.5.

The integration of WIF into .NET necessitated several changes in the WIF API surface. WIF 4.5 includes new namespaces, some changes to configuration elements, and new tooling for Visual Studio. This topic provides guidance that you can use to help you migrate applications built using WIF 3.5 to WIF 4.5. There may be scenarios in which you need to run legacy applications built using WIF 3.5 on computers that are running Windows 8 or Windows Server 2012. This topic also provides guidance about how to enable WIF 3.5 for these operating systems.

Wednesday, July 17, 2013

Use NHibernate to execute raw SQL

In some cases it’s a lot easier to just write a simple SQL statement instead of using LINQ, HQL or one of the other syntaxes that NHibernate supports.

Executing a raw SQL:

Converting the result to a typed object:

Tuesday, July 16, 2013

Unofficial Redis for Windows

While looking for a good distributed cache solution in ASP.NET I felt in love with Redis, a fast and feature rich key-value store solution. Unfortunately there is no official support(yet) for Redis on Windows.

However the Microsoft Open Tech group created an unofficial port that works great!

From the Redis site:

image

The easiest way to install Redis is through NuGet:

  • Open Visual Studio
  • Create an empty solution so that NuGet knows where to put the packages
  • Go the Package Manager Console: Tools –> Library Package Manager –>Package Manager Console
  • Type Install-Package Redis-64

image

  • Go to the Packages folder and browse to the Tools folder. Here you’ll find the Redis-server.exe. Double click on it to start it. Redis is ready to use and start’s listening on a specific port(6379 in my case)

image

  • Let’s open up a client and try to put a value into Redis. Start Redis-cli.exe. It already connects to the same port by default.

image

  • Add a value by executing following command:

image

  • Read the value again:

image

Monday, July 15, 2013

Some JavaScript lecture for during the holidays

During the year I collect a lot of articles that I should have a look at, now during the holidays I can finally have a look at them.

2 great articles on Smashing Magazine I can recommend are:

Journey Through The JavaScript MVC Jungle: A review of some JavaScript frameworks by the same guys that brought us TodoMVC(a GitHub project that contains the same site written in all possible JavaScript frameworks). It lists the Pros and Cons of each of these frameworks helping you to choose one you’ll like.

image

Designing Better JavaScript APIs: This article talks about designing the APIs that developers will love using. It covers the most important things that you will need to consider before and while writing your own utilities and libraries.

Friday, July 12, 2013

Error after upgrading an MVC 4 project to .NET 4.5

After upgrading an ASP.NET MVC 4 project to .NET 4.5, I was welcomed with the following error message in my output window:

‘CompareAttribute’ is an ambiguous reference between ‘System.ComponentModel.DataAnnotations.CompareAttribute’ and ‘System.Web.Mvc.CompareAttribute’.

 

clip_image002

And it are the following lines that are causing the issue:

clip_image004

It seems that the CompareAttribute in .NET 4.5 has been added to the list of core data annotations making it available in both the System.Web.Mvc and System.ComponentModel.DataAnnotations namespace.

Fix the problem by removing the reference to System.Web.Mvc from your class file (by deleting‘using System.Web.Mvc’ line).

Thursday, July 11, 2013

Green SSL

I always wondered when browsing around that for some sites a green address bar is shown when using https, where for others it’s just the normal behavior.  Green address bar sites are using something called an Extended Validation Certificate (EV Cert).

image

image

image

From Wikipedia:

An Extended Validation Certificate (EV) is an X.509 public key certificate issued according to a specific set of identity verification criteria. These criteria require extensive verification of the requesting entity's identity by the certificate authority (CA) before a certificate is issued. Certificates issued by a CA under the EV guidelines are not structurally different from other certificates (and hence provide no stronger cryptography than other, cheaper certificates), but are designated with a CA-specific policy identifier so that EV-aware software can recognize them.

The criteria for issuing EV certificates are defined by the Guidelines for Extended Validation Certificates, currently (as of May 2012) at version 1.4. The guidelines are produced by the CA/Browser Forum, a voluntary organization whose members include leading CAs and vendors of Internet software, as well as representatives from the legal and audit professions.

The Extended Validation Certificates do require you to provide more information in order to obtain one.

  • You must be a valid and verifiable legal entity (Government agencies, Corporations, General partnerships, Unincorporated associations, Sole proprietorship's).
  • The employment and authority of the person placing the certificate order must be verifiable.
  • A legal opinion letter (from an Attorney, or Accountant) may be required to confirm that the requester has the authority to obtain SSL Certificate(s) on behalf of the company
  • The entities must have a conformable physical existence and business presence.

Don’t know if this really makes your website more secure, it certainly makes the certificate a lot more expensive Glimlach.

Wednesday, July 10, 2013

Things from Build 2013: $returnvalue

One thing I always found annoying(and it seems I’m not the only one) during debugging is that it was not possible to see return values for functions. The only way was to change your code to first assign the return value to a local variable and then return that value.

Starting from Visual Studio 2013, you no longer need to do this. Now you can examine the return value of a function when you step over or out of a function during your debugging session.

The return value(s) get displayed in the “Autos Windows” (Debug->Windows->Autos) and you can also use the pseudo variable “$ReturnValue” in the Watch and/or Immediate window to fetch the last function’s return value.

If you add a breakpoint to the following code block:

You can see the return values in the autos window

You can also use the last returned value in your immediate window

More info: http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/27/seeing-function-return-values-in-the-debugger-in-visual-studio-2013.aspx

Tuesday, July 9, 2013

FailSafe: Building Scalable, Resilient Cloud Services

A few months ago, Microsoft introduced the FailSafe Initiative. The mission of the FailSafe Initiative is to articulate recommendations and approaches for delivering resilient and scalable cloud architectures. FailSafe delivers general guidance on building resilient and scalable cloud architectures, guidance for implementing those architectures on Microsoft technologies generally, and scenario specific architectures that implement those architectures in context.

Developed by Microsoft Services and the Azure Customer Advisory Team, this series provides both platform agnostic concepts and patterns as well as Windows Azure specific implementation guidance.

Some useful links:

And of course, don’t forget the Channel 9 series:

image

Monday, July 8, 2013

ASP.NET Performance eBooks

If you need some extra lecture to read next to the swimming pool, the guys from RedGate sponsored the release of 2 free eBooks:

image

image

Remark: You need to register first before you can download the eBooks.

Friday, July 5, 2013

Windows Identity Foundation(WIF) Request validation in .NET 4.5

I blogged before about how to solve the ‘A potentially dangerous Request.Form value was detected from the client’ error that you can get back from the ASP.NET request validation when using Windows Identity Foundation. The best way to fix it was creating a custom RequestValidator.

RequestError

If you upgrade your application to use WIF in .NET 4.5 you no longer need this custom validator. Instead the only thing you need to do is setting the RequestValidation option to 4.5 mode in your web.config:

<httpRuntime targetFramework="4.5" requestValidationMode="4.5" />

WIF now plays nicely with the request validation in ASP.NET 4.5.


Thursday, July 4, 2013

Team Foundation Server 2010 Build: Warning: Cannot find the last label '': no changesets will be associated with the build.

After configuring a build definition for some projects, the build always returned the following warning:

Warning: Cannot find the last label '': no changesets will be associated with the build.

Some further investigation made me notice that it only happened on projects were there was no successful build (yet). The first time that the build succeeds(no errors), the warning goes away and never comes back.

Wednesday, July 3, 2013

Impress your colleagues with your knowledge about... <deployment retail=”true”/>

Sometimes when working with C.NETyou discover some hidden gems. Some of them very useful, other ones a little bit harder to find a good way to benefit from their functionality. One of those hidden gems that I discovered some days ago is the retail attribute on the deployment element in your machine.config.

When moving your ASP.NET applications to a production environment, you will most likely want to set the compilation debug attribute in web.config to false, as having debug set to true has some downsides:

  • Compilation takes longer as batch optimizations are disabled
  • Scripts and images from WebResources.axd will not be cached on the client
  • Larger memory footprint
  • Code will execute slower because of enabled debug paths
  • Request execution timeout is disabled

If you set the retail option to true in your web.config, ASP.NET disables trace output, disables debug capabilities, and disables detailed system-generated error messages for remote users. For applications that have a customErrors element in the application Web.config file, the mode attribute is forced to On. These settings override any settings that are made in application Web.config files.

Remark: This value can only be set at the machine level, not at the application level.

Tuesday, July 2, 2013

Using different dataloaders in Effort

Last week I blogged about Effort, a library that helps you write unit test against your Entity Framework code. In that post I showed you how to populate the Effort in-memory database by using CSV files. Turns out that that is not the only way to populate the Effort database.

Effort provides multiple built-in data loaders:

  • EntityDataLoader: fetch data from an existing database by utilizing an existing Entity Framework compatible ADO.NET provider.
  • CsvDataLoader: read data records from CSV files
  • CacheDataLoader: speed up the initialization process by wrapping any kind of data loader with a cache layer

And of course it is also possible to build your own data loader.

More information can be found here.

Monday, July 1, 2013

CSS Float tutorials

I have to admit, I have a big hole in my brain where I could fit some CSS knowledge. Even after years of web development, I still struggle with CSS(don’t ask me why).
One of these concepts I always do wrong is CSS floats. With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it. Float is very often used for images, but it is also useful when working with layouts.
One site that helped me understand CSS Float and how to use it is Floatutorial. Floatutorial takes you through the basics of floating elements such as images, drop caps, next and back buttons, image galleries, inline lists and multi-column layouts.
image