Showing posts with label Visual Studio 2013. Show all posts
Showing posts with label Visual Studio 2013. Show all posts

Thursday, August 18, 2016

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: https://confluence.jetbrains.com/display/TCD10/FxCop



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:









Sunday, June 12, 2016

Generating Entity Framework Code First POCO classes from an existing Entity Framework Model (EDMX file)

If you have an existing Entity Framework model (EDMX file) you generated from your database and you would like to generate POCO classes for using as Domain Classes or for Entity Framework Code First, you can easily accomplish this using Code Generation!


  1. Right click on your Entity Framework model and select "Add Code Generation Item"
  2. You will then be prompted to select a Visual Studio Template.  Based on the version of Entity Framework that you are using, you can choose either "EF 5.x DbContext Generator" or "EF 6.x DbContext Generator".  For my particular example, I am using Entity Framework 6, so I ended up using "EF 6.x DbContext Generator"
  3. Finally, once you choose your Entity Framework Code Generation template, you will then be prompted to run the template.  
  4. When prompted with the warning dialog about running templates on your computer, make sure you click "OK" in order to allow the template to run.  You may be prompted multiple times, so you may have to either click "OK" multiple times or select the checkbox for "Do not show this message again"
  5. Once you have completed running the code generation templates, you will have your generated code classes!



Friday, June 10, 2016

Get started using Git with Visual Studio and Team Foundation Server

If you are an organization adopting Git with Team Foundation Server/Visual Studio Online, then you will definitely want to check out this article: https://www.visualstudio.com/en-us/docs/git/get-started

The original article references Visual Studio 2013, so if you are using Visual Studio 2015, you will want to check out this article instead for some updates: https://www.visualstudio.com/en-us/docs/git/share-your-code-in-git-vs

For a full overview of how to work with Git, you can go through all of the articles listed in the Overview:  https://www.visualstudio.com/en-us/docs/git/overview

Once you have gone through the articles, you will want to make sure your environment is set up for using Git.

In order to do that, you will have to first set up your Source Control Plug-in to use Git by going into the Tools-->Options-->Source Control--Plug-in Selection:




Unfortunately, Microsoft Visual Studio leaves a lot to be desired when it comes to working with Git inside of Visual Studio.

Therefore, you will also probably want to at least install the GitExtensions Visual Studio extension as well: https://visualstudiogallery.msdn.microsoft.com/35c2f314-a6ab-479a-b4a3-cff99103d32f

In addition, it is frequently useful to have TortoiseGit installed as well to support working with Git from within Windows Explorer: https://tortoisegit.org/

Once you have one of these tools set up, you will want to connect to your Git repository and clone the repository to your local file system.

Now, you can create the solution or open an existing solution from within Visual Studio.

When you are working with Visual Studio solutions, however, there are typically a large number of files that you want to exclude from being checked into your Git source control repository, so  you will need to add a .gitignore file using the following steps:

From the Team Explorer Home menu, click on the Settings button:



You will then be presented with a Settings dialog which will allow you to configure the Settings for Git.  When you select the "Global Settings", you will be presented with options to configure your user credentials for your Git repository:



Next up, you will have to configure your Git Repository settings.  You can go back to the Git Settings screen and now select "Repository Settings":



At the bottom of this screen, you will see the options to add a .gitignore file as well as a .gitattributes file.

You can click on the "Add" button to add a default .gitignore file to your solution so that the bin directory, packages and various other common directories and files will automatically be excluded from checking into your Git repository.

Once you have this set up, you will see the source control bindings in your Visual Studio Solution Explorer:


When you make changes to your code files, you will then be able to commit your changes to source control:


Just as with standard source control check-ins, you can enter a code comment before committing your code:



Then once you click on the "Commit All" button, this will ONLY commit the source code changes to your local file system!  You will still have to "PUSH" or "SYNC" the source code changes to get them into your remote Git Repository!


After you commit your source code, Visual Studio will automatically provide you with a reminder notification that you have to do "Sync" of your source code to get it up into your remote Git Repository.  You can click on the Sync hyperlink in the notification message to push your changes up to your Git repository:


Once you click on the "Push" hyperlink, your changes will be pushed up to your remote Git repository!

That is all there is to getting started with using Git with Visual Studio!!

Generating Entity Framework Code First POCO classes

Normally, when you think of Entity Framework Code First, you think that you have to generate all of your POCO classes completely by hand!

Fortunately, though, a clever developer has created the Entity Framework Reverse POCO Generator! https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838

If you are using Visual Studio 2013, you will also need to install the Entity Framework 6 Tools for Visual Studio 2013 from here: https://www.microsoft.com/en-us/download/details.aspx?id=40762

This Visual Studio extension allows you to generate Entity Framework Code First POCO classes directly from your database!

Unfortunately, all of the POCO classes are generated into a single class file, but you can then use a tool such as Resharper to extract the classes into separate C# files and then customize the generated POCO classes to ensure they match the design requirements of your system!

How neat is that??

Tuesday, June 7, 2016

Easily implementing Design Patterns into your C# code using PostSharp

If you want to easily implement common Design Patterns into your C# code, then look no further than PostSharp!

https://www.postsharp.net/features

PostSharp offers a "PostSharp Express Free" edition as well as a Professional and Ultimate edition: https://www.postsharp.net/purchase

In addition, if you are an open-source contributor or a Freelancer, you are eligible to get a Free PostSharp license.  Other organizations are also eligible for some significant discounts as well: https://www.postsharp.net/purchase/discounts

Thursday, March 17, 2016

Code Commenting for an entire Visual Studio Solution

If you have a lot of code in your Visual Studio solution that is uncommented (which is often the case), then you might want to take advantage of a tool/extension that can help fill in the gaps and help you generate some usable Help documentation from your code base.

There are several tools on the market such as the following:

Of the above solutions, Atomineer Pro is by far the most affordable solution and based on this comparison matrix seems more fully featured than GhostDoc Pro: http://www.atomineerutils.com/compare.php

For example, GhostDoc Pro cannot document an entire project or an entire solution, while Atomineer Pro can!

Furthermore, GhostDoc Pro has indicated that they WILL NEVER add this feature!! http://support.submain.com/kb/a32/can-ghostdoc-run-on-the-whole-solution.aspx

On the other hand, as you can see from the comparison Atomineer Pro still does not provide support for CHM Export like GhostDoc Pro does.

Fortunately, you can use this free and open source project on CodePlex to build your CHM files from your code: https://shfb.codeplex.com/

With the combination of these 2 tools you can have an affordable code commenting solution for your Visual Studio projects/solutions that can help provide more complete code documentation for your developers/end-users!


Tuesday, March 1, 2016

Securing ASP.NET Web API using Azure Active Directory and Visual Studio 2015

If you are looking to secure your ASP.NET Web API application using Azure Active Directory, you might come across this article: https://azure.microsoft.com/en-us/documentation/articles/api-management-howto-protect-backend-with-aad/

Unfortunately, this article is a bit outdated and only addresses how to accomplish this using Visual Studio 2013.

But what if you are using Visual Studio 2015?  Though the steps and screens are different, they are similar in a lot of respects.

Below are the screenshots for how to set this up using Visual Studio 2015 instead:










Tuesday, February 23, 2016

Using the Async keyword in Visual Studio Unit Tests

I was recently attempting to use the async keyword with some of my Visual Studio Unit Tests for ASP.NET Web API and I soon discovered that every time I attempted to run my Unit Tests, they always came up Inconclusive!!

Well, as it turns out, Visual Studio Unit Tests DO NOT support the async keyword!!

So how do you work around this problem?


var actual = testAPIController.Get().GetAwaiter().GetResult() as OkNegotiatedContentResult<List<string>>;


Using this code segment allows you to still use the same Unit Test method signatures and test asynchronous ASP.NET Web API methods!

Sharing and importing Visual Studio Project and Item Templates

If you want to re-use and share Project and Item Templates in Visual Studio, then you will want to know how to create/export Visual Studio Project and Item Templates and then share them with others.

First up, in order to create/export Project and Item Templates, you must have installed the Visual Studio SDK.  When you are installing Visual Studio 2015, there is an option during the installation to include the Visual Studio SDK installation.

Once you have that installed, you will get the following option from your File menu:



Then, once the Project or Item Template has been exported, you need to share it with others!

First up, you need to determine where Visual Studio stores the Project and Item Templates by default.  This can be found under the Tools-->Options-->Projects and Solutions menu item:



You then simply drop the Project or Item Template into the root of these folders or into their respective category and you will then be able to create a new Project or Item based on this exported template!


NOTE:  One of the caveats of manually dropping these files into one of these Visual Studio folders is that if Visual Studio is already open, it will not pick it up until you shut down and re-start Visual Studio.  

Thursday, January 21, 2016

Ignoring error messages from Visual Studio Post Build events

I don't use Post Build events in my Visual Studio solutions very often since I prefer to code this directly into MSBuild as part of my Continuous Integration build process usually run by a CI Server such as Jetbrains TeamCity or Team Foundation Build.

However, since I work on a wide variety of projects as part of my consultancy, I frequently encounter projects which still leverage Visual Studio Post Build events.

A large number of Visual Studio projects have a Post Build event which redirects the output to another directory on the file system and may even include a set of commands needed to create the correct target directory structure.

The problem with this approach is using standard command line commands such as mkdir will emit errors if a directory already exists, thus causing Visual Studio to raise an error if a particular command in the Post Build event fails even though it may have worked just fine overall.

So, how do you handle this problem?

Well, using the capabilities of MSBuild, you can modify the Visual Studio project file to ignore these Post Build event error messages!

The solution provided here is by far my favorite solution to this problem: http://blogs.msdn.com/b/astebner/archive/2006/08/08/691849.aspx

It overrides the PreBuildEvent (or PostBuildEvent) target in order to ignore the exit codes or continue the Post Build event even if an error occurs.  Alternatively, you can even do both!

Once you include this target in your relevant Visual Studio project files, the Post Build event error messages you are encountering should disappear!

You need to install the latest Silverlight Developer Runtime

I recently had to build a Silverlight project in Visual Studio and when I attempted to compile my solution, I received the following error message:


The error message directed me to download and install the Silverlight Developer runtime from this Url: http://go.microsoft.com/fwlink/?LinkId=229324

Once I did this and installed the Silverlight Developer runtime on my machine, I was able to successfully build the solution!

What are the NuGet v. 2 and v. 3 Urls?

I have found myself recently having to bounce back and forth between Visual Studio 2013 and Visual Studio 2015 and if you have not already discovered this for yourself, they use different versions of NuGet as well as different NuGet Urls!

Therefore, when I am setting up Autobuilds in a CI Build Server such as Jetbrains TeamCity, I usually need to configure the appropriate NuGet Server Urls in order to ensure all of the correct NuGet packages are downloaded at build time.

So I needed to know the different NuGet Urls used by the different versions of NuGet!

Well, for NuGet v. 2.x, the Url is the following:
https://www.nuget.org/api/v2/

If you are using NuGet v. 3.x, the Url is slightly different as follows:
https://api.nuget.org/v3/index.json

You can then configure these respective Urls in Visual Studio and your CI Build Server to ensure that all of the NuGet packages are downloaded and restored correctly!

Wednesday, January 20, 2016

Supporting Visual Studio Installer projects

If you are upgrading from an older version of Visual Studio (such as Visual Studio 2010) to Visual Studio 2013 or Visual Studio 2015, you may find that, by default, these newer versions of Visual Studio no longer support Visual Studio Installer Projects (.vdproj files)!

Fortunately, though, Microsoft has heard our complaints and has addressed this problem by releasing a Visual Studio Extension for both of these IDEs to allow these project types to be opened once again!

Microsoft Visual Studio 2013 Installer Projects
https://visualstudiogallery.msdn.microsoft.com/9abe329c-9bba-44a1-be59-0fbf6151054d

Microsoft Visual Studio 2015 Installer Projects
https://visualstudiogallery.msdn.microsoft.com/f1cc3f3e-c300-40a7-8797-c509fb8933b9

Once you have installed one of these extensions, you will once again be able to open your Visual Studio Installer Projects!

Wednesday, December 9, 2015

Using Visual Studio Publish Profiles in TeamCity

If you want to use Publishing Profiles while building your Visual Studio solutions in TeamCity, you will discover that it is relatively easy to accomplish!

First of all, you will want to create 2 System Properties (under the Parameters for the Build Configuration Settings):

  1. system.PublishProfile - set to the value of your PublishProfile (such as Release)
  2. system.DeployOnBuild - set to true
That is all that is needed!  Now, when TeamCity builds your Visual Studio solution, it will automatically pass these parameters to your Visual Studio build step as MSBuild Command line parameters.

If you then take a look at your Build Configuration's Build Log, you will notice that these parameters are being passed to your Visual Studio build so that your Publishing Profile is being executed!


Thursday, December 3, 2015

.NET Framework v. 4.6.1 and Developer Targeting Packs

.NET Framework v. 4.6.1 was recently released and along with it the Developer Targeting Packs were also released!  The Developer Targeting Packs allow you to target .NET Framework v. 4.6.1 using older Visual Studio IDEs such as Visual Studio 2012 and Visual Studio 2013.

You can read more about the .NET Framework v. 4.6.1 release here: http://blogs.msdn.com/b/dotnet/archive/2015/11/30/net-framework-4-6-1-is-now-available.aspx

You can download the Developer Targeting Pack here: http://www.microsoft.com/en-us/download/details.aspx?id=49978

Thursday, October 1, 2015

Performing a Team Foundation Server Merge Operation

If you have ever want to learn how to perform a TFS Merge Operation using Visual Studio (and not the command line), this is how you do it:










Once you are completed with the Merge operation, you will have to resolve any Conflicts which arise due to the Merge operation and you have the following options to choose from:


  • Merge Changes in Merge Tool
  • Keep Target Branch Version
  • Take Source Branch Version
If you intend on merging changes in a Merge Tool, I highly recommend a 3rd party merging tool such as Beyond Compare (http://scootersoftware.com/) to help you perform the merge operations!  

That is all there is to it!

Tuesday, September 22, 2015

NuGet Package Sources for Visual Studio 2013 and Visual Studio 2015

If you haven't already noticed, there is a major change in NuGet between Visual Studio 2013 and Visual Studio 2015 which is undocumented on the NuGet site!

If you read this article: https://docs.nuget.org/consume/nuget-config-file#package-sources

You will see that the NuGet official package source is listed as: https://nuget.org/api/v2/

This is the same Url that is listed in Visual Studio 2013 as the NuGet official package source.

However, Visual Studio 2015 is using a newer version of NuGet and therefore has the following Url: https://api.nuget.org/v3/index.json


Friday, September 18, 2015

Resolving NuGet Package Reference problems

If you have numerous NuGet Package References in your project or solution, at one point or another, you will inevitably run into NuGet Package Reference problems.

One of the most common problems with NuGet Packages is ensuring that your NuGet Package References are restored at build time.

This can be corrected by making sure you have this setting in Visual Studio Tools-->Options-->NuGet Package Manager:



The second problem you might run into is that NuGet Package References may differ across your entire solution!  For example, if you have numerous projects in your solution, there is a good chance that they may be using different NuGet Package Reference Assembly Versions!  So how do you manage these discrepancies?

You right click on the solution and select "Manage NuGet Packages for Solution":



This will show you all of the projects in your solution that have NuGet Package References along with their associated installed assembly versions.

 If you are still having problems with differing assembly versions, then you will need to check the packages.config file for each of the different projects:

In the packages.config file you will find all of the various NuGet Package References along with their respective versions.  You will have to compare all of these versions across all of your Visual Studio projects to ensure that they have matching versions.

Lastly, if you are still encountering issues with building packages with NuGet references, then you should close your Visual Studio solution and delete the packages  directory from the root of the solution in Windows Explorer.





Now, you can go ahead and try re-building your Visual Studio solution to restore all missing NuGet Packages to see if your NuGet Package Reference issues are now resolved!

If you are having problems with NuGet Package Versions and you need to re-install a NuGet Package Reference, in Visual Studio 2013 (especially if that NuGet Package has other dependencies), you will have to MANUALLY REMOVE the references from Visual Studio and the referring lines in packages.config.

Then, in NuGet Package Manager in Visual Studio 2013, you can re-install the NuGet packages.

If you are using Visual Studio 2015, however, you have a new option to remove or uninstall the NuGet Packages called "Force uninstall, even if there are dependencies on it":


This removes many of the obstacles present while working with NuGet Package References in Visual Studio 2013 and allows you to effortlessly uninstall and reinstall any NuGet Package References that you need!