Monday, November 26, 2012

Branches, Team Foundation Service and Azure Websites

Microsoft did some great work on integrating Azure Websites and Team Foundation Service. It's really easy to setup continuous deployment by creating a website, linking it to Team Foundation Service and kicking of a build.

But there is a downside to this. Each change will be published immediately to your production site! Not the best thing when you haven't completely finished a feature or some styling needs be done.

So, what if we wanted to have a DTAP environment?

Last week I came across this post Multiple Environments with Windows Azure Web Sites which showed how you can setup a Staging/Production environment on Azure Website with Git.

I couldn't find any information however how to do this with Team Foundation Service. But after searching the web and having some contact with Microsoft I found the necessary configuration steps and I want to share them with you in this blog.

Create a new Team Project in Team Foundation Server


To get started we first need a new project in Team Foundation Service and upload some code to it. On the TFS website you can find some good tutorials that will help you to get started if you don't already have a project:


For this blog, I've created a new TFS Project 'MyApp' and added a default MVC 4 Internet application.

Create two websites in Windows Azure

Now that we have our code in place, we need to create two websites in Windows Azure. One website will contain your production ready site which is live for all your customers. The other site will be a free website where you can test changes before showing them to customers.

Creating a website is the easiest with Quick Create.


For this example, I've created two websites:

The Production site will run in Shared Mode so I can link my custom domain name to it. The staging website will run in free mode. It's only for internal use and it won't cost us any money!

Link to Team Foundation Service

The next step is to link our websites to our Team Foundation Service account. By default, this will setup a Continuous Integration Build that will deploy each check in automatically to our Azure Website.


Test it

Now that we have setup two websites and linked them to TFS we can test that our continuous deployment is working.

If we make a change to our MyApp project and check it in we will see that Team Foundation Service notices the change, queues two new builds and deploys to both our Azure Websites.


Both builds will run and after that we have the same code deployed to our staging and production environment.

Branch your code

Now we can start configuring the different branches for the staging and production environment. 

Creating a branch in Visual Studio is a quick operation. I have two branches in Source Control:


We can now make changes to our Staging environment, test them and then merge them to our Main branch.

When working with multiple branches this extension changes the Visual Studio window title to include the parent folder. This makes it easier to see in which branch you're working so you don't make any mistakes.

Adjusting the build definition

When linking the Azure websites to TFS, a new build definition is created in your TFS project: ApplicationName-WebsiteName-CD (continuous deployment). Since we have linked two websites, we have two new build definitions.

The build definition for the staging build, MyApp-Staging-CD, needs to be updated . We need to adjust the workspace mappings and it should point to the correct solution file.



We have to change the Workspace mapping for the Staging Build Definition. It will now point to MyApp\Main and it should point to MyApp\Staging.



Then we have to change the 'Solution to Build' in the Process settings. By default it will point to MyApp\Main\MyApp.sln.



One other thing we have to change is the UNC path in the Build Defaults.By default this is an incorrect UNC path for the drop folder. I have just switched to 'This build does not copy output files to a drop folder' so I could save the changes to my build definition file.

Run it!


After saving our build definition we can make changes to both our Staging and Production branch. Each will be detected by the correct build definition and deployed to the Azure Website.

So, to recap:
  • Link the two websites to your team project 
  • Branch your code 
  • Adjust the workspace mappings on the build definition to the staging site to map the branch 
  • Change the solution to build property on the build definition for your staging site to point to the solution in your branch

What do you think? Is it useful to support different branches for Azure Websites? Any features you miss? Please leave a comment!

Friday, November 23, 2012

My favorite Visual Studio 2012 features Part #5

In this series of blogs I highlight some of my favorite Visual Studio 2012 features that helped me to increase my productivity.

Today we are going to look at..

The New Team Explorer

The Team Explorer has a new look and feel. Instead of having a couple of windows for managing all your pending changes, work items and other team stuff you now have a single window you can go to.

Just hit Ctrl + ' to open up the new Team Explorer. It has a new search box and it offers easy access to all your team features.


With access to a Team Foundation Server 2012 (or the service version in the cloud!) you also have access to the new My Work and Code Review options.

The My Work tab helps you with a quick overview of the things you have to do. It offers easy access to your work items and code reviews.

But there is also a completely new feature! You can now suspend your current activities and save it to TFS. It not only saves your current work but also the current view of your environment such as open documents, breakpoints and other tool windows. After saving your current work, you can start working on something else (maybe a quick bug fix) and then return to your current task easily by looking at your 'Suspended Work'.


Code Reviews is also a great new feature. You can now easily request a code review from a coworker. He can accept your review request, make some comments and send them back to you. This is all nicely tracked by Team Foundation Server so you get a report of which code was reviewed and which wasn't.

At Build 2012 there where a couple of sessions that covered the Team Exploer. One such a video can be found here on Channel9. It demonstrates all the features and highlights some of the changes that are coming to the Team Explorer in the quarterly update of Visual Studio.

Remember, when using the Team Explorer in Visual Studio 2012: your shortcut of the day:
Ctrl + ' to open your Team Explorer.

What are your favorite Visual Studio 2012 features? Or is there a feature you want to know more about? Please leave a comment!

Monday, November 12, 2012

My favorite Visual Studio 2012 features Part #4


In this series of blogs I highlight some of my favorite Visual Studio 2012 features that helped me to increase my productivity.

I have already blogged about:
  1. Project Round-Tripping
  2. How Search improves your productivity
  3. How the new Solution Explorer makes your life easier
Today we are going to look at...

Unit Testing improvements!


Unit Testing support is also targeted in this release. There is a new Test Explorer that gives you a nice overview of all your unit tests and the current state they are in. It groups your tests on failed/succeeded and it immediately shows the details of the selected test. And of course the Test Explorer supports search!


Another important feature is the ability to run your unit tests automatically after each build.This really helps you getting into the Test Driven Deveopment mode (see this blog most for more details: TDDing in Visual Studio 2012).

Test Runners


The Test Explorer now easily integrates with other unit testing frameworks. You can install frameworks like NUnit, xUnit or QUnit and the test explorer will automatically pick up your tests.


Code Coverage


Running Code Coverage for your Unit Tests also became much easier. Just right click the test you want to execute and select 'Analyze Code Coverage for Selected Tests'.


Remember, when using the Unit Test features in Visual Studio 2012: your shortcut of the day: Ctrl + R, A. to run all the tests.

What are your favorite Visual Studio 2012 features? Or is there a feature you want to know more about? Please leave a comment!

Saturday, October 20, 2012

My favorite Visual Studio 2012 features Part #3


In this series of blogs I highlight some of my favorite Visual Studio 2012 features that helped me to increase my productivity.

I have already blogged about:

  1. Project Round-Tripping
  2. How Search improves your productivity
  3. How the new Solution Explorer makes your life easier

Today we are going to look at...

The New Solution Explorer


The Solution Explorer is one of the most used windows in Visual Studio. If you want to work on some code, you will probably use the Solution Explorer to open it. Visual Studio 2012 really improves the Solution Explorer.

Where Visual Studio 2010 had separate windows for Class View and Solution Explorer, Visual Studio 2012 combines them into one window. You can easily drill down into classes to view all the members of a class and jump right to them.

;

One other feature that immediately jumps out is the search box at the top. Try it out! Hit Ctrl + ; to start filtering your solution explorer. It will filter all the way down to individual members in your class. It really helps me when navigating trough a large solution.


Are you ever lost in your solution explorer? You can now scope your solution explorer and show only the area you are currently working on. I find this feature useful when I'm working on a large solution with multiple layers and I'm only doing some work on one layer.


New Buttons


Also check out all the buttons at the top. There are back, forward and home buttons which can ease your navigation.

Ever wondered which files have changes? Or which files are open? Try out the new Pending Changes Filter and Open Files Filter.


Looking for the location of a file in your Solution Explorer? Hit the Sync with Active Document so you can immediately see where a file is located.


And now you can finally collapse all files and folders with a single mouse click.



The new Solution Explorer really is a nice piece of work! Visual Studio 2012 adds a lot of features that where previously only in custom extensions. Now they are more easily accessible and better performing.

My personal favorite is the search feature so remember:

Shortcut of the day: Ctrl + ;

What are your favorite Visual Studio 2012 features? Or is there a feature you want to know more about? Please leave a comment!

Monday, October 15, 2012

My favorite Visual Studio 2012 features Part #2

In this series of blogs I highlight some of my favorite Visual Studio 2012 features that helped me to increase my productivity.

This are the current parts
  1. Project Round-Tripping
  2. How Search improves your productivity
  3. How the new Solution Explorer makes your life easier
Today we are going to look at...

Search


Microsoft understands that Visual Studio is a big product with a lot of features.As discussed on the Visual Studio Blog in 'Introducing the New Developer Experience', Microsoft tried to address these problems by creating a better User Interface.

That's where the new Search capabilities fit in. In VS 2012 you can now search for commands throughout the whole IDE by simply hitting Ctrl + Q. It will search trough all menu commands and also trough all the settings.



You can configure Quick Launch to use a couple of providers. Just hit Ctrl + Q and type 'Quick Launch'. The first option lets you configure the Quick Launch settings.


Hitting enter opens the settings for Quick Launch.


Also notice the shortcuts. For example: @menu lets you search trough all menu items.

The cool thing about Quick Launch is that it teaches you new shortcuts so you won't have to search for it anymore.As you can see they are displayed behind the items that support a shortcut. Try learning a new shortcut each day and

There are many other areas of Visual Studio that support searching but lets save them for another day!

And remember, the shortcut of today  is: Ctrl + Q.

What are your favorite Visual Studio 2012 features? Or is there a feature you want to know more about? Please leave a comment!

Friday, October 12, 2012

Rendering an ASP.NET MVC View to a string in a Web Api Controller

Today I was working on a Web Api Controller that integrated with a third party application. The application wants us to return an XML document. One of the nodes is a piece of HTML that is displayed in the application.

So I started thinking about a flexible way to render HTML from a WebApi Controller. After searching around on the web I came up with a solution.

Sneaking my way into MVC


The biggest problem was that rendering a View is something that normally shouldn't happen inside Web Api (see this discussion why not). The solutions I found on the web all use some kind of MediaTypeFormatter to return HTML when the client requests it. 

However, that's not what I want. I want to return the following data to the client where the Body property should contain an HTML string:

public class ContentItem
{
    public string Title { get; set; }
    public string Body { get; set; }
    public string Widgets { get; set; }
}

Eventually this is what I came up with:

public static string RenderViewToString(string controllerName, string viewName, 
                                         object viewData)
{
    HttpContextBase contextBase = new HttpContextWrapper(HttpContext.Current);

    var routeData = new RouteData();
    routeData.Values.Add("controller", controllerName);
    var controllerContext = new ControllerContext(contextBase, routeData, 
                                                   new EmptyController());

    var razorViewEngine = new RazorViewEngine();
    var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, 
                                                     "", false);

    var writer = new StringWriter();
    var viewContext = new ViewContext(controllerContext, razorViewResult.View, 
           new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
    razorViewResult.View.Render(viewContext, writer);

    return writer.ToString();
}

private class EmptyController : ControllerBase
{
    protected override void ExecuteCore() { }
}

You can simply call this from anywhere in your Web Api Controller: 

RenderViewToString("controller", "view", model)
Do you have run into similar issues? Can you use this code or do you have another idea? Please leave a comment!

My favorite Visual Studio 2012 features Part #1


See also the other parts
  1. Project Round-Tripping
  2. How Search improves your productivity
  3. How the new Solution Explorer makes your life easier
A year ago Microsoft released the first preview of Visual Studio 11. Now we have the full RTM release available with a bunch of new features!

As we all know, Visual Studio is an enormous product. As developers we spend a lot of time using it and knowing your tools will really help you to speed up development. In this series of blogs I want to highlight some of the new features that I particularly like and that helped me speed up my development.

Lets start with....

Visual Studio 2010-2012 Project round-tripping


As Anders Abel stated you're a top developer! This also means that you probably want to work with the latest software and try out all the cool new things. But you can't. You have to stay compatible with your co-workers...

That's over now with Visual Studio 2012! You can open a project in Visual Studio 2012, save it and then open it in Visual Studio 2010 without any problems. Your colleagues won't even notice that you are suddenly running Visual Studio 2012 (but they will notice that you are getting more work done!)

Let's do it!

  1. Create a new project (a console app for example) in Visual Studio 2010.
  2. Save and close the solution.
  3. Launch Visual Studio 2012 and open the solution.
  4. Make some changes to the code, save and close the solution.
  5. Open the project in Visual Studio 2010.
  6. Enjoy!

Some rules..


First of all, to make round-tripping work you need to have Service Pack 1 installed for Visual Studio 2010. This shouldn't pose a big problem, especially since Service Pack 1 is distributed trough Windows Update.

There are also some project types which don't support round-tripping. The only one I encountered was the Visual Studio Database Project. VS2012 wants to convert this project and after conversion it can't be opened in VS2010.

If you use any special project types checkout MSDN for the official list of project types that need conversion: Visual Studio 2012 Compatibility. If you need more detailed info check the Visual Studio blog: Round-tripping with Visual Studio 11.

This means you can start using Visual Studio 2012 right now! You don't have to wait for your coworkers and nobody will notice that you are enjoying all the new features ;-)

What are your favorite Visual Studio 2012 features? Or is there a feature you want to know more about? Please leave a comment!