Wednesday, August 31, 2016

Unit Testing jQuery with QUnit

One of the main problems with writing JavaScript or jQuery code is that it is difficult to debug and test.

Well, that is where QUnit comes in!

QUnit allows Unit Testing your JavaScript/jQuery code through a standard web browser.

You can learn about QUnit here:

If you are working with older documentation for QUnit, you may encounter error messages regarding deprecated methods, in which case you should consult the upgrade guide:

To get started with a basic sample of a QUnit Unit Test, you can use the following sample code:

Implementing Remote Validation using Data Annotations in ASP.NET MVC

If you want to implement Remote Validation using Data Annotations on your ViewModels in ASP.NET MVC, you can follow this article:

Even though this article references ASP.NET MVC 3, it still applies to ASP.NET MVC 5.

In order to ensure that you do not encounter conflicts with the System.ComponentModel.DataAnnotations namespace while using the Remote attribute, you should decorate your ViewModels with the fully qualified namespace for the attribute in this manner:

[System.Web.Mvc.Remote("IsUID_Available", "Validation")]

Then, once you have the necessary appSettings configured in your Web.config (these are set by default in most ASP.NET MVC projects), you should be able to leverage your MVC Controller Validation code as follows:

IMPOTANT NOTE: The parameter to the Remote validation method should MATCH the attribute name in the ViewModel, otherwise, you will end up getting a null value passed to your Remote validation method!

This will then perform the validation via an AJAX call seamlessly within your ASP.NET MVC Web Application!

Tuesday, August 30, 2016

Server-side paging with ASP.NET Web API and Kendo UI Grids

If you need to implement a custom server-side paging solution with Kendo UI, it may not be readily obvious as to how to accomplish this!

Kendo UI has some code samples which partially addresses this scenario:

However, this example is tightly coupled to Entity Framework and does not work so well with ASP.NET Web API...

So how exactly can you adapt the Controller code sample to your own needs that work with ASP.NET Web API?

First of all, you need to make sure your Kendo Grid is setup as follows:
Notice that you have to set ServerOperation(true)

Next, you have to set up your Controller code as follows:

That is all there is to it!

Monday, August 29, 2016

Unit Testing Data Annotation Validation/View Model Validation

If you are building functionality for validating your ViewModels either using IValidateableObject or through using Data Annotations/ValidationAttribute, you will probably want to be able to Unit Test this functionality as well!

Well, thankfully, this can be done quite easily by following this article:

Unit Testing ASP.NET MVC with Request.IsAjaxRequest()

One of the difficulties in Unit Testing ASP.NET MVC Web Applications is any dependence on HttpContext.

Code that you may encounter frequently in your ASP.NET MVC Controller methods is a check for whether or not the Request object is an AJAX Request.

So how exactly do you Unit Test this for your ASP.NET MVC Web Applications?

Well, there is an article on how to do just that!

Once you set the Request Headers for the Mocked HttpRequestBase object, you will be able to return true for the Ajax request and complete your Unit Tests!

Sunday, August 28, 2016

Add xUnit.Net support to TeamCity

If you want to run your xUnit.Net Unit Tests to your TeamCity Builds, you can accomplish this quite conveniently using the xUnit.Net TeamCity plugin.

You can download the latest release of the xUnit.Net TeamCity plugin from here:

Once you have installed the TeamCity plugin, you will be able to use xUnit.Net as a TeamCity Build step as follows:

That is all there is to it!!

Friday, August 26, 2016

Code Metrics PowerTool for Visual Studio 2015

If you want to run Code Metrics for your Visual Studio solution, you will need to download the appropriate version of Code Metrics Power Tool corresponding to your Visual Studio version.

For Visual Studio 2015, you can download the Code Metrics Power Tool from here:

If you need to integrate the calculation of Code Metrics into your automated build process (such as Jetbrains TeamCity), then you can learn how to use Code Metrics from the command line here:

If you need to make the XML results viewable, then you will probably need a set of XSLT and CSS files to transform the XML into HTML, then you will want to take a look at this article:

Unfortunately, the links to the XSLT and CSS are broken!

I was able to dig up a version of the MetricsResultsTransform.xslt file from another site which I am providing for your reference below:

However, I was not able to find the Default.css file, so this functionality is still missing from the XSLT transformation.

If you think Microsoft should ship a default set of XSLT and CSS files with the Code Metrics PowerTool, you can vote for this UserVoice item here:

Mocking Entity Framework Database Transactions

If you are writing Unit Tests for any of your classes that leverage Entity Framework, you should be able to Mock most operations leveraged by Entity Framework.

However, Entity Framework 6 introduces features for wrapping your operation in a BeginTransaction() statement as described here:

The problem with this scenario, of course, is that this code cannot easily be Mocked!

Well, what do you do in these scenarios?

Well, first of all, I would recommend looking at the use case scenarios to determine if you really need this functionality since Microsoft describes this in the article:

In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction. This transaction lasts only long enough to execute the operation and then completes. When you execute another such operation a new transaction is started.

Therefore, transactions are being used as required by Entity Framework for insert, update and delete operations!!

If you read articles such as this which does some additional examination on the repercussions of using Entity Framework Database Transactions (, you will see that the recommendation is to NOT USE Entity Framework Database Transactions (especially with Web Applications).

Therefore, unless the business logic ABSOLUTELY requires the use of Entity Framework Database Transactions in this manner, you can probably safely remove this code and thereby save yourself a great deal of hassle while writing your Unit Tests!!

Thursday, August 25, 2016

Where is FxCopCmd.exe located?

If you are looking for the location of FxCopCmd.exe that ships with Visual Studio 2015, you can find it at this path:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe

In addition, you will find the FxCop Rule Sets available in this folder location:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets

Using Unity IoC with constructor parameters

I recently decided to switch my IoC container from Ninject to Unity for my ASP.NET Web API Web Application because the Ninject IoC container has poor support for Web API and has not been updated/refreshed in several years.

So, naturally, when I began switching my code over from Ninject over to Unity, one of the questions that immediately came up was how to use constructor parameters for my types in Unity?

Well, after doing some quick searching and experimentation, I came up with this solution:

It is as simple as that!

Wednesday, August 24, 2016

Unit Testing ASP.NET MVC

If you have ever had to write a Unit Test for your ASP.NET MVC Web Application, you may quickly discover that writing Unit Tests for ASP.NET MVC for all but the most basic scenarios is quite difficult!

This is because Unit Tests, by default, do not understand how to test the various conventions used in ASP.NET MVC for routing to Views etc.

So how exactly do you solve this dilemma?

Well, thankfully, there is a NuGet package that allows you to fill in the gaps left by the Unit Testing framework!  The name of this NuGet package is TestStack.FluentMVCTesting:

Here is a list of some of the basic types of statements you can include in your MVC Unit Tests using TestStack.FluentMVCTesting:

Working with Resource Files in ASP.NET MVC

If you need to localize your ASP.NET MVC Web Application, then you will probably need to use Resource files.

You have to start by first adding a Resources File to your Web Application:

The primary resource file should be named without a language code to specify the default language (ex: DefaultResources.resx) For many organizations, the default resource file will correspond to the en-US language resource file.

Once it has been added to your Web Application, you will need to make sure that you change the "Access Modifier" to "Public".  By default, the Access Modifier is set to "No code generation".  If this is left at the default, the Resource file will not be available to use within the application as a strongly typed file:

Next, you need to add additional Resource files for each additional language that you are planning to support:

The list of language codes that you can support for your application are listed here:

Next, on the properties for the Resource files, you will have to change the "Build Action" from "Content" to "Embedded Resource":

Once you have done that, the Resource File should now be strongly typed for use within your ASP.NET MVC Web Application and you should be able to reference it as follows throughout your Controller Code and Razor Views: ex: App_LocalResources.DataAnnotation_Localization.RequiredMessage

That is all there is to it!!

Tuesday, August 23, 2016

Running Code Analysis on your Visual Studio solution

In previous versions of Visual Studio prior to Visual Studio 2015 (such as Visual Studio 2013), Code Analysis was not available to editions of Visual Studio lower than Premium edition.  However, with the release of Visual Studio 2015, Code Analysis is now available even with the Professional Edition!

Now that Code Analysis is available to a much wider audience of developers, you may be wondering how to perform Code Analysis on your solution or on your individual Visual Studio Projects.

You can run Code Analysis on your solution simply by right-clicking on your solution and selecting "Analyze-->Run Code Analysis on Solution":

You can then enable Code Analysis on individual projects by going into the Project Properties and selecting the tab for "Code Analysis":

Finally, you can suppress specific error messages and warnings from the Build log by selecting the "Build" tab and semicolon-delimiting the specific error messages and warnings you wish to suppress:

Lastly, you can view any Code Analysis Error Messages and Warnings directly from the Error List window:

To read more about Visual Studio Code Analysis, you can check out the following articles:

Unit Testing Kendo UI

If you want to Unit Test Kendo UI, unfortunately, there is no documentation on how to accomplish this on the Telerik website.

But, thanks to Stack Overflow, you can find a handy example on Unit Testing functionality for Kendo UI:

However, I prefer to use MSTest as my Unit Testing framework and having a few things outlined in the Unit Test to make the Unit Test a bit clearer as follows:

Monday, August 22, 2016

Windows Azure Classic Portal vs. Windows Azure Portal

Unfortunately, even though many new features are available in the new Windows Azure Portal, there still remains a significant amount of functionality which still can ONLY be completed in the Windows Azure Classic Portal.

So what are the respective Urls for both the Windows Azure Classic Portal and the new Windows Azure Portal?

Below are the respective Urls:

Windows Azure Classic Portal

Windows Azure Portal

Changing the Account Owner in Visual Studio Online

If you use Visual Studio Online, you might know that you are only allowed to have a single account owner for Visual Studio Online.

But what happens in instances where that staff member is out of the office, goes on leave or even leaves the company?

Thankfully, any administrator in Visual Studio Online can change the Account Owner to another individual in the organization!

The steps for accomplishing this can be found in this blog:

Using AutoFixture in your Unit Tests

If you are writing Unit Tests, you may frequently run into a need to autogenerate test or dummy data for your Unit Tests.

For many cases, you can use a framework such as NBuilder, however, NBuilder has numerous limitations in that it is primarily geared towards generating test data for classes which have default class constructors, but it is not as useful for generating test data for primitive types such as strings and integers.

So what do you do in those cases?  Well, that is where AutoFixture ( comes in!

AutoFixture is available as a NuGet package and fills in many of the gaps that are left by a framework such as NBuilder.  It allows the generation of random strings and integers as well as allowing the generation of test objects in a manner similar to NBuilder:

Mocking HttpClient in your ASP.NET Web API Unit Tests

If you use HttpClient in your Unit Tests, you may encounter a great deal of difficulty with using this object since this object is not easily mocked using a Mocking Framework such as Moq.  In fact, you may encounter an error message such as the following:

There are a great number of solutions posted on the Internet on how to resolve this issue by creating a custom HttpMessageHandler and then passing this in the constructor of the HttpClient to create the HttpClient object for Unit Testing.

However, a clever developer has already provided a very handy NuGet package to allow you to do just that!

MockHttp is just one particular NuGet package that supports Unit Testing of the HttpClient.  There are other such as HttpClient.Helpers

However, the use of MockHttp is very intuitive and I highly prefer using it in my Unit Tests.

One of the things that I like to do, however, is use frameworks such as NBuilder or AutoFixture to help me generate test data rather than manually type this information into my Unit Tests myself.  But, one of the problems with using an object such as HttpResponseMessage is that it does not lend itself well to generating all of the properties needed to pass a Unit Test such as setting the Content of the HttpResponseMessage.

Therefore, I had to use a solution such as the following to get my entire Unit Test to work:

Friday, August 19, 2016

Managing NuGet packages for your solution

When you have multiple projects in a solution, you may find it difficult to ensure that all of the NuGet packages in the solution are all using the same NuGet package versions.

Fortunately, you can manage all of these various NuGet package versions at the solution level!

  1. Right click on your solution and select "Manage NuGet Packages for Solution"
  2. You will then be presented with a dialog which allows you to see all of the NuGet packages that are being used in your solution
  3. In addition, if there are differing version numbers of NuGet packages in different projects, you also have the option to "Consolidate" to ensure that all of your projects are using the same NuGet package version numbers!

Thursday, August 18, 2016

Supported versions of jQuery with Telerik Kendo UI

If you are using the Kendo UI libraries, you may be wondering what versions of jQuery are supported  with these controls?

Well, the Telerik Kendo UI team has provided a handy article for you to look up the specific versions of jQuery that are supported:

Using the FxCop Build Runner in Jetbrains TeamCity

If you want to use the FxCop Build Runner in Jetbrains TeamCity, you can consult the documentation here:

For the path to FxCop, using the option for "Autodetect installation" should work well for you in most cases:

In some cases, if you have multiple versions of Visual Studio installed on your machine, you may want to select a specific version of FxCop:

  • For Visual Studio 2013, you will select 12.0
  • For Visual Studio 2015, you will select 15.0
For the list of Assemblies, you can specify a relative path to the assemblies by using a wildcard expression such as *.dll.  Of course, since you are probably compiling a solution with multiple projects, you will want to specify the path to each set of assemblies in your solution such as MyProject\bin\MyProject.dll, MyProject2\bin\MyProject2.dll as follows:

You need to make sure that the names of your assemblies are all SPACE SEPARATED in order to ensure that FxCop runs correctly against them.

You must then configure the Advanced Options for FxCop in order to be able to run correctly against referenced assemblies etc.:

I like to specify the path to "Search referenced assemblies in directories" and uncheck the option for "Search referenced assemblies in GAC".  This is especially relevant since I distribute most of my assembly references through NuGet packages rather than installing them into the GAC.

Optionally, you can also configure the "Build Failure Conditions" such that any "Fail on analysis errors" in FxCop will also trigger a failure of the build

Personally, I avoid this setting since I generally work with offshore development teams which do not run regular code analysis on their projects/solution nor may they be licensed to run Code Analysis on their solution with their edition of Visual Studio.  Visual Studio 2015 Professional and above now offers Code Analysis and Code Metrics, but earlier editions of Visual Studio such as Visual Studio 2013 did not.  However, setting this value can potentially be disruptive to any team which is not very particular about following all of the guidelines outlined by FxCop.

Finally, once you have specified the correct path for your build, you will be able to use the TeamCity FxCop Build Runner to generate a Code Inspection report!

You can access the Code Inspection report from the "Build shortcuts" dropdown menu:

Create a README file for Team Foundation Server/Visual Studio Online

If you want to create a README file for your Team Foundation Server/Visual Studio Online project in a manner similar to what you see used on sites such as GitHub, you can do that rather readily using the web-based User Interface!

Just as with standard markdown files (.md), you can use Markdown syntax to create the README file in VSO as outlined in this article:

In addition, you even have the option of adding this README file directly to the Overview dashboard so that it is immediately visible to all of your users!

Wednesday, August 17, 2016

Adding Bundling support for Kendo UI

If you want to add Bundling support for your ASP.NET MVC Web Application, you should take a look at this article:

Using ELMAH to log exceptions/errors in an ASP.NET MVC Web Application

Now that Microsoft's Application Blocks are no longer under active development, I began looking for alternatives to the Exception and Logging Application Blocks for my ASP.NET MVC Web Applications.

Fortunately, ELMAH has a NuGet package especially for ASP.NET MVC Web Applications!  The other nice thing about ELMAH is that it also provides a nice web-based User Interface for you to view your Exception/Error Logs which is not provided by the Microsoft Exception and Logging Application Blocks:

Once you have installed ELMAH into your ASP.NET MVC Web Application, it is still not configured for your application by default!

You still have to go into your Web.config and configure your ELMAH settings for logging and security.

In fact, if you do  not configure your ELMAH settings, when you navigate to the /elmah Url of your ASP.NET MVC Web Application, you will probably get a screen similar to the following:

If you are using this for your own development environment, security may not be much of a concern, and you may not also require a database so you can use the following Web.config settings for your ELMAH Configuration Section:

If everything is configured correctly for ELMAH in your Web.config file, you should now get this screen instead when you navigate to the /elmah Url:

You can read more about setting up and configuring ELMAH here:

For configuring ELMAH with ASP.NET MVC:

For securing your ELMAH Error Log pages:

Tuesday, August 16, 2016

Assigning roles and privileges in the Windows Azure Portal

There may be times in your organization when you need to set up additional users and privileges to manage your Windows Azure instances.

This is yet another feature which is not well documented by Microsoft, but with just a few simple steps, you can easily accomplish this:

  1. Once you log into the Windows Azure Portal, click on Subscriptions
  2. Click on the name of your Windows Azure Subscription
  3. Click on the icon in the left hand top corner for "Access"
  4. You can then see the list of existing users and from the toolbar, you can click on "Add" to add new users
  5. You will then be able to add additional roles to the users such as "Owner", "Contributor", "Reader" etc.
  6. Once you have selected a Role, you can then invite the specific user using their Microsoft account.
  7. Click on the "Invite" button and once the user logs into the Azure Portal, they should have access to manage that Windows Azure Subscription!

Setting up a Paid Subscription for Visual Studio Online in the Windows Azure Portal

If you want to set up a Paid Subscription for Visual Studio Online, you have to set this up within the Windows Azure Portal.

Unfortunately, the process to accomplish this is not documented and instead you have to figure this out on your own.

But not to worry!  It is a relatively easy process to follow!

  1. From the More Services section in the Azure Portal, click on Team Services Accounts
  2. You should then see your existing Visual Studio Online account
  3. If you have an existing Azure Subscription, you can then link your Visual Studio Online account to your Azure Subscription, by clicking on the Link button
  4. Once the accounts have been linked, you can view the settings for your Visual Studio Online account
  5. When you are ready to purchase additional Visual Studio Online licenses for your users, you can click on "Users" in the Settings pane
  6. The new pane that displays will allow you to select the number of users you want to purchase
  7. Once you have chosen the number of users you want to purchase, you click on the "Save" button in the toolbar.
  8. Finally, once the purchased users have been assigned, you can go into Visual Studio Online and assign the appropriate purchased User Licenses to your Visual Studio Online users!

Monday, August 15, 2016

Testing Internationalization Support for an ASP.NET Web Application using Internet Explorer

If you want to test Internationalization support for your ASP.NET Web Application, you will probably end up using Internet Explorer to test out your various language settings.

Below you will find instructions on how to change Language settings in Internet Explorer:

  1. From the Tools menu, select Internet Options
  2. From the General tab, beneath the Appearance section, click on Languages
  3. On the Language Preference dialog, click on "Set Language Preferences"
  4. Click on "Add a language" to add a desired language
  5. Click on the "Move up" link to move the desired language to be used for testing to the top of the list
  6. Close and re-open your Internet Explorer browser to navigate to your ASP.NET Web Application to test out your various language settings

Testing Internationalization Support for an ASP.NET Web Application using Google Chrome

If you are testing Internationalization/Localization support for your ASP.NET Web Application, you may be wondering how to change the Language settings for Google Chrome.  This might especially be the case because most examples for Internationalization/Localization focus on changing Language settings in Internet Explorer.

Thankfully, changing the Language settings for Google Chrome is surprisingly easy!

  1. From the Google Chrome menu, select Settings
  2. From the Settings dialog, click on Show advanced settings
  3. Click on the button for Language and input settings
  4. Click on the Add button to add any additional language
  5. Move the desired language for testing to the top of the list
  6. Click on "Done" to close the Language and Input Settings dialog
  7. You will have to restart the Google Chrome browser in order to display content in the newly selected language
  8. That is it!!