TFS connection Value cannot be null

In our company, we are upgrading from TFS 2010 to TFS 2012. So to test everything we have a clone of the TFS server.

To test the clone I had to switch my connection to the different servers. After hitting the “Connect” button I got a “Value cannot be null.” exception. It said that also that: “Parameter name: baseUri” was the problem. After contacting a colleague of me, he said to delete the cache of the TFS server on my computer. After doing that, the connection worked as it suppose to work.

You can find the cache in the following folder: C:Users<yourusername>Local SettingsApplication DataMicrosoftTeam Foundation2.0Cache

Localization validation in MVC

Edit 10\15\2014: I created a new version of this blogpost. It is called MVC localization validation.

 

Last week I was working on a bug for my website SeeTings. The bug was the validation of a DateTime not working in Firefox and Chrome. In Internet Explorer (even version 9) was working correct.

After hours searching and hundreds of form posts further, I finally found the solution. I thought the problem was in implementing the jQuery UI Date picker with the Dutch Format of a date (dd-MM-yyyy) in it but that wasn’t true.

The problem was the client validation of the jQuery Validation plugin. ASP .NET MVC 3 and 4 are shipped with an installed version of jQuery. And the internet template (the one that I was using) creates a login and register process.

To test the bug, I created a new project and added a DateTime field called Birthday to the register login form. I deleted all other fields except the UserName field because then I could test if the Required validation still worked if I might fined a solution for the validation problem.

After long searching, I saw this post http://stackoverflow.com/a/511670 on Stackoverflow. There was another script file called “additional-methods.js” that I didn’t heard of. After long searching, I found the official website of jQuery validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/ and saw the CDN of all the jQuery instances.

It seemed not necessary to link the additional-methods.js file because that would only add extra validation like credit card or url TLD and email TLD etc. checking.  I found out that jQuery validation had multiple localization files. One for the messages and one for one other thing and that was my problem, the date validation.

When you go to the CDN of Microsoft, you will see in version 1.6 (current version is 1.9) a long list of localization files.

imageimage

The files ending with messages_xxx.js are the error messages localized. The files ending with methods_xx.js (like for me methods_nl.js) are the localized versions of some script. In this case only the date.

So reference on your page where you need to do the validation, the standard validation scripts of jQuery and after that you need to reference you localization file. Only do this if the browser is that language. So I only reference the script file if I know that the browser is Dutch. You can find the current language settings of the browser with JavaScript as well. You can change the url of the CDN from 1.6 to 1.9 and it will still work. Why the files are not listed on the 1.9 page, I don’t know.

After completing this above, you can after that implementing the jQuery Datepicker. Don’t forget that the Datepicker of jQuery UI also needs a localization reference file. See the last part of the documentation on this page http://jqueryui.com/demos/datepicker/. Just reference the file and everything is working.

Good luck and hopefully you guys aren’t that long busy figuring out why the validation isn’t working.

Creating a TFS Build definition with deploying

This week I was busy to give our environment more quality by adding a deployment of the newly checked in work. This was a quite more difficult then I imagined before I started. This is because the environment that we have is off course not completely the same as the standard demos of Microsoft and the documentation for this is… not available?

 

Team project setup

We have a Team project with 3 different branches.image

  1. Production (Release)
  2. Staging
  3. Development (Debug)

Off course we develop in the Development branch. When the iteration is finished, we merge everything to the staging environment where we and the customer could test everything. When testing (and bug fixing if you made bugs) is done we merge everything to production. In that way we can do bug fixes directly on every version of the project.

 

Build definitions

We wanted to have a build definition for development that:

  1. Builded the solution;
  2. Deploys the multiple projects to the different locations.

I created for each branch a different build definition with different needs to get to the result. I will explain the Development because this build definition will publish multiple ASP .NET Web Applications to different IIS websites.

 

Creating the build definition for development

imageCreate a new build definition and give it the name that you want. I called it Development because I want to use it only on my Development branch in the team project.

I set the trigger to go off on each check-in. In that way, the build will also publish all new versions of the web applications to the desired environments on the server(s).

image

Set the workspace to the workspace of your branch.

Define a drop location and which build controller you like to use of your build server. Maybe you have a dedicated build controller for particular solutions that have many check-ins.

Setup deployment to the server

The last part is the part where we setup the deployment to the server.

image

Define on step 1 the Configurations that you want to build. With config transformations you could setup the different build environments. See for a tutorial this following link:

http://www.asp.net/mvc/tutorials/deployment/deployment-to-a-hosting-provider/Deployment-to-a-Hosting-Provider-Web-Config-File-Transformations-3-of-12

Because it is a development environment, we use the default Debug config with all the links to the test database and test web services. Set the projects to build to the solution file.

 

Note for building only one project
When you set the projects to build to only one project, you could get an error when you are building. The error will say something about your output path that is not right.

c:WindowsMicrosoft.NETFramework64v4.0.30319Microsoft.Common.targets (484): The OutputPath property is not set for project ‘UI.Web.Mvc.csproj’.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration=’Debug’  Platform=’Any CPU’.  You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn’t exist for this project.

image

This isn’t really the cause why you would get that error. When you open your project file (unload your project and right click to edit) you will see that the build platform is AnyCPU and not Any CPU. You will have to change the “Configurations to Build” in your build definition to AnyCPU. You can just type it in the dialog.

imageWhen you have a whole solution to build, you don’t need to do this.

 

 

 

 

 

 

Setup deployment for only one web application

When you have only one web application, you can add the “MSBuild Arguments” in your build definition directly.

See for a large explanation for each build argument the following blog:

http://vishaljoshi.blogspot.nl/2010/11/team-build-web-deployment-web-deploy-vs.html

The arguments that you typically have to use are the following:

/p:DeployOnBuild=True
/p:DeployTarget=MsDeployPublish
/p:MSDeployServiceURL=http://servername
/p:DeployIISAppPath=”Default Web Site”
/p:CreatePackageOnPublish=False
/p:MsDeployPublishMethod=RemoteAgent
/p:AllowUntrustedCertificate=True
/p:UserName=username
/p:Password=password

 

Setup deployment for multiple web applications

If you have multiple web applications in a solution (like we have), you can’t use the build arguments like above because then every application would be publishing to the same website on the same server. This will fail your build definition eventually. Maybe not the first time but it will fail sometime. For me it failed every second time I tried it.

Well stop the complaining and tell me how to fix this!

Change the build arguments that you have setup above. Only use the following build arguments:

/p:DeployOnBuild=True
/p:CreatePackageOnPublish=False

image

 

Now unload your projects that you want to publish and edit every project file to their specific needs. Go to the PropertyGroup of your build configuration that you are using in your build definition. I use Debug so will change that PropertyGroup.

image

Use the same name as the build arguments and enter your values.

 

Note for exposing your publish username and password
When you setup your deployment, you have to enter your username and password. Otherwise you will get an error that your not authenticated.

C:Program Files (x86)MSBuildMicrosoftVisualStudiov10.0WebMicrosoft.Web.Publishing.targets (3588): Web deployment task failed.(Remote agent (URL http://servername/MSDEPLOYAGENTSERVICE) could not be contacted.  Make sure the remote agent service is installed and started on the target computer.) Make sure the site name, user name, and password are correct. If the issue is not resolved, please contact your local or server administrator. Error details: Remote agent (URL http://servername/MSDEPLOYAGENTSERVICE) could not be contacted.  Make sure the remote agent service is installed and started on the target computer. An unsupported response was received. The response header ‘MSDeploy.Response’ was ” but ‘v1’ was expected. The remote server returned an error: (401) Unauthorized.

For security reasons, you should not enter your username and password in your build definition. So, how to fix this?

The first thing you must do is give your TFS build service account rights to the server that your publishing to. I added it for now to the local Administrators group of my old (but still working) Windows  Server 2003 server.

The second thing that you have to do depends on which way you have setup your arguments. Use the following syntax for the build arguments in your build definition:
/p:UserName=

And for in your project file:
image

This will indicate that the username and password that will be used for deploying is the same as your build controller on your build server.

Hopefully I gave you enough information about deploying your applications. If you have any questions, don’t hesitate to contact me.

 

Update:
When you deployed your project and see the zip with your project in it, you should check your connectionstring in your config. It could be the case that the connectionstring is using parameters to generate the full connectionstring. This is an option that is on by default. If it is using parameters, you will see something like “$(Replaceable Token…)”.

You can simple disable this (so it will use the connectionstring that you have set in your config transform) by adding the <AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings> tag in your project file on the same place where you placed the rest of the properties.

OutputCache explanation

Why should we use caching?

The reason why we should use caching is to increase the performance for the end user and to decrease the traffic on the server. There are multiple kind of caching. In this post, I will explain the OutputCache attribute of MVC 3 (and 4).

Using the OutputCache Attribute

To cache a page or image in ASP .NET MVC 3 we only have to set the OutputCache attribute above the action.

image

The Duration property is the timespan you want to cache the action in seconds. So above, the page will be cached for 10 seconds. When you run the application, the page is showing the ViewBag.Time on your screen. If you hit F5 (refresh), the page is refreshed but your action isn’t. The time is still the same if you did refresh the page within the 10 seconds. When you hit F5 after 10 seconds, the new time is shown.

Use fiddler to see if the caching is working.

Caching the action for different result

When you have an action for different results, like asking for an image for a given imageId you use a parameter in your action. The OutputCache attribute will automatically use the parameter for caching the result for that specific item.

image

So when you ask for example image 3 and 24, the caching will work on both images.

Use fiddler to see if the caching is working.

Caching of private items

By default, the cache is saved on the server and client. But when you have private data the cache should only be used on the client (the browser). Otherwise, private data could be accessed by anyone.

We could use the location property for this.

image

Set the location property to Client to only store the image in the browser.

When you run the page now and see the image or page, the image or page is cached only in your browser. When you hit F5, the caching won’t work. This is because client caching will not work for browser refreshing. If you use a hyperlink to the same page or image the caching will work.

Again use fiddler to see the results.

How to disable caching of an action

To disable caching for an action, use the code as shown below.

image

Caching profiles

When you have a lot of actions with the same settings, you could implement caching profiles. In this way, you only have to change the settings on one place.

In the web.config, add the following section.

image

You then could use the caching profile in the following way.

image

Minification and Bundling in MVC 4 RC

Asp .NET MVC 4 Release Candidate is out. One of the features that is included is called the minification and bundling feature. This was already there in the beta but in the release candidate version it has changed.

Why should we use it

The reason why we should use the bundling and minification feature is performance. The features increases your loading performance of your website. Every time you reference a JavaScript (like jQuery or your own), or CSS file in your page, the browser makes a call to the server. This is done for each separate reference. Each referenced file has included all the comments and spacing in your file. This makes the file larger then when we should delete those spaced. The bundling and minification feature does this for us.

How does it work

In your Global.asax the CSS and JavaScript files are Bundled with BundleConfig.RegisterBundles(BundleTable.Bundles); line.

image

image

Reference the files in your page.

image

When you run the application and use Fiddler to view the calls to your server, you still see all the files called separately.

image

image

This is because the Bundling and Minification feature by default only work when your not in debug mode. This handy because then you could debug with all the whitespaces in your files and have the performance in the production environment.

See the difference in your production environment:

image

image

Force Bundling and Minification

You can use the BundleTable.EnableOptmizations override but the best way for a little test is to remove the debug=”true” attribute in your web.config.

image

Browser caching

When the feature is active, the browser will cache the files. When you add or change some JavaScript or CSS code, the files are generated again and the version number in the references are updated. In that way, the browser knows that there is a new version and your website wont brake.

image

MissingManifestResourceException solution

Every time I edited a C# file in my solution, I had an error called “Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "UI.Web.Mvc.Properties.Resources.resources" was correctly embedded or linked into assembly "UI.Web.Mvc" at compile time, or that all the satellite assemblies required are loadable and fully signed.. The error said that the Resource file wasn’t setup correctly. The strange thing about it that my colleagues didn’t got the error and when I cleaned up the web application mvc project and builded the project again, the site worked correctly. Cleaning and building after each edit is a huge consuming of you time so I needed a fix for this. 

After searching for some time I found the difference between my laptop and the laptops of my colleagues. They had the Dutch version of Windows 7 Enterprise and I had the English version. The project is by default Dutch and we are still developing so in the solution we only had the Dutch version of the resource file. When I ran my project, the culture is set to the host and after that, it looks to the culture of the browser. My host is English so I couldn’t find any English resource. Why It worked correctly after a clean, I don’t know. Can you tell me?

I fixed the error by adding the English version of the resource file. Just copied the Dutch version and added “en” to the name. Simple fix, big irritations and a hope useless time… It’s now time to hurry up and finish the baby :-)

Add Area and Iterations permissions in TFS 2010

IMG_16639mkIf you want to give your user permissions to add Areas and Iterations, you could that be following these steps:

Area

  1. Go to: Team –> Team Project Settings –> Areas and Iterations…
  2. Select on the “Area” tab the “Security…” button on the bottom
  3. Select or add the group/person that you want to give the permissions
  4. Set “Create and order child nodes” to allow

Optional: Set “Delete this node” and/or “Edit this node” to allow for edit and delete permissions.

Iteration

  1. Go to: Team –> Team Project Settings –> Areas and Iterations…
  2. Select on the “Iteration” tab the “Security…” button on the bottom (this is a different button than on the “Area” tab.
  3. Select or add the group/person that you want to give the permissions
  4. Set “Create and order child nodes” to allow

Optional: Set “Delete this node” and/or “Edit this node” to allow for edit and delete permissions.

Use .Where().Select().Single() rather than .Single()

When you use the .Single() lambda statement, you will only get one result. When you only use 1 property of the whole object it is totally waste of the performance. Only get want you want. So you could better use the .Select() statement to get only the property you want.

See the difference below where we have an example for getting the profile picture of an unique user.

.Where().Select().Single()

byte[] imageData = Context.Profiles.Where(p => p.Id == profileId).Select(p => p.Picture).Single();

SELECT
[Limit1].[Picture] AS [Picture]
FROM ( SELECT TOP (2)
[Extent1].[Picture] AS [Picture]
FROM [dbo].[Profiles] AS [Extent1]
WHERE [Extent1].[Id] = @p__linq__0
)  AS [Limit1]

.Single()

byte[] imageData = Context.Profiles.Single(p => p.Id == profileId).Picture;

SELECT TOP (2)
[Extent1].[Id] AS [Id],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[Birthday] AS [Birthday],
[Extent1].[HideBirthdayYear] AS [HideBirthdayYear],
[Extent1].[Picture] AS [Picture],
[Extent1].[ModifiedOn] AS [ModifiedOn],
[Extent1].[UserId] AS [UserId]
FROM [dbo].[Profiles] AS [Extent1]
WHERE [Extent1].[Id] = @p__linq__0

 

Imagine what the difference would be with nested queries, joins, or even bigger objects (tables) than this example.