Monday, February 29, 2016

Using NBuilder for Unit Tests

When you are typically writing Unit Tests for your Visual Studio C# code, you will generally want to generate dummy/test data.  Well, you can hard code many of these values into your Unit Tests or you can use a tool such as NBuilder to do the job of generating all that test data for you!

If you are not familiar with NBuilder, it is available as a NuGet Library (which has not been updated in quite a while: https://www.nuget.org/packages/NBuilder/) as well as available for download from GitHub: https://github.com/garethdown44/nbuilder

If you take a look at the GitHub documentation, it may get you started, but may not be sufficient to answer all of your questions.

Fortunately, there is some additional documentation still sitting on the Google Code site that you can use as a secondary reference: https://code.google.com/archive/p/nbuilder/wikis/Overview_HowTo.wiki

Finally, this article covers some pretty good examples on how to work with NBuilder as well: http://www.jerriepelser.com/blog/creating-test-data-with-nbuilder-and-faker


More than one migrations configuration type was found in the assembly 'MyData'. Specify the name of the one to use.

I was recently running an Add-Migration command using Entity Framework Code First Migrations, when I encountered the following error message:

"More than one migrations configuration type was found in the assembly 'MyData'. Specify the name of the one to use."

As I soon discovered, I was using multiple Configuration.cs classes (to correspond to my multiple DbContext instances) in my assembly, thus causing the Add-Migration command to fail with this error message.

Therefore, I had to modify the command to be the following instead:


Add-Migration "MyMigration" -ProjectName "MyProject" -StartUpProjectName "MyProject" -ConnectionStringName "MyConnString" -ConfigurationTypeName "MyMigration.Configuration" -Force Verbose

The target context 'MyDbContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

I was recently using Code First Migrations with Entity Framework and I was using the Enable-Migrations command when I suddenly encountered this error message:

The target context 'MyDbContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

As it turned out, I was missing a default constructor in my DbContext implementation that was preventing the running of this command.

As soon as I added this back in, I was able to run my migration command!


Friday, February 26, 2016

Connecting to LocalDb from within Visual Studio 2015

If you have worked with SQL Server LocaldDB in the past, you may have connected to LocalDB using the following type of connection string: (localdb)\v11.0

However, this changes with the release of Visual Studio 2015.  Instead, of using (localdb)\v11.0, you will instead use a connection string such as this: (localdb)\MSSQLLocalDB

You can then connect to the Visual Studio 2015 instance of LocalDB in the following manner:




Microsoft acquiring Xamarin!

If you haven't already heard the news, Microsoft will be acquiring Xamarin!  If this pattern follows similar Microsoft purchases, this should ultimately make Xamarin tools ultimately free for all Microsoft developers.  I am definitely crossing my fingers and hoping that happens!

You can read more about the acquisition here: http://blogs.microsoft.com/blog/2016/02/24/microsoft-to-acquire-xamarin-and-empower-more-developers-to-build-apps-on-any-device/

Using T4 Templates in Visual Studio 2015

If you are planning on using T4 Templates in your projects, Microsoft has changed the names of the templates slightly from earlier versions such as Visual Studio 2010.

Now, when you attempt to add a new item to your project, you will have the following 2 options:


  • Runtime Text Template
  • Text Template

Selecting one of these templates will result in a file with a .tt extension.  

If you want to know the syntax for writing a Text Template then you will definitely want to check out this MSDN article: https://msdn.microsoft.com/en-us/library/bb126445.aspx

Wednesday, February 24, 2016

Tail for Windows

If you ever need to monitor a log file for output, you may be looking for tools such as Tail for the Windows platform.

Fortunately, there is an abundance of Tail utilities for Windows!

Here are just a few of them:


  1. WinTail: http://www.baremetalsoft.com/wintail/
  2. BareTail: http://www.baremetalsoft.com/baretail/index.php
  3. BareTail Pro: http://www.baremetalsoft.com/baretailpro/index.php
  4. Tail for Win32: http://tailforwin32.sourceforge.net/
  5. SnakeTail: http://snakenest.com/snaketail/
  6. tail for Windows: http://www.trisunsoft.com/tail-for-windows.htm
  7. mTail: http://ophilipp.free.fr/op_tail.htm



Limitations of using Git with Visual Studio Online

I recently started working on a project that was using Git with Visual Studio Online.  Of course, it was a .NET Development project, so we were naturally using Visual Studio for all of our development.

Unlike Team Foundation Server integration with Visual Studio which Microsoft has been developing for the last 10+ years, Git support with Visual Studio is a relatively new addition based on user demand for DVCS (Distributed Version Control System) support.

However, as I started working with Git in Visual Studio, I quickly learned the number of limitations that existed with using Git through Visual Studio:


  1. Unlike TFS which has a built-in Source Control Explorer, there is no Repository browser or Source Control Explorer integration for the Git repository.
  2. Unlike TFS, which supports configuring external user tools such as Beyond Compare, the Microsoft Git Provider has no such support and developers are left to using the built-in Microsoft compare and merge support within Visual Studio.
  3. Unlike TFS which has support for shelvesets, Git support in Visual Studio does not have the corresponding equivalent of stashes.
  4. Unlike TFS which allows you to easily go into any work item and link up the associated Changesets even after a commit has been performed, Git support in Visual Studio does not have this feature.  Instead, you can only associate the Work Item at the time of committing to Git.  There is no support for adding Work Item association after the commit has been performed!
I am sure there are many, many more limitations that I have not discovered yet, but these are just some of the limitations to be aware of when determining whether to choose Git with Visual Studio Online/Team Foundation Server!


Versioning ASP.NET Web API

If you search the Internet for methods to version ASP.NET Web API services, you will find a slew of solutions to overcome the fact that ASP.NET Web API has no built-in method to support versioning of the services.

However, I came across one interesting solution which leverages custom attributes that I found rather appealing.  Even though it does not directly address this solution for ASP.NET Web API and instead addresses it for ASP.NET MVC, the concept of Route Versioning is the same:  https://visualstudiomagazine.com/articles/2014/10/28/asp-net-mvc-5-1-new.aspx

Since it creates a distinctly different Routing attribute for holding the Route version information, it is much clearer than simply embedding route version information directly into specific routing Urls.

Hopefully, in a future release of ASP.NET Web API, a feature like this will be built directly into the ASP.NET Web API framework!!

Mapping RESTful Design Http Status Codes in ASP.NET Web API

I was recently presented with a RESTful design document complete with Http Status Codes, but having worked with ASP.NET Web API exclusively for my RESTful services, I could not readily match the direct Http Status Code numbers to their .NET equivalents in ASP.NET Web API!

Well, fortunately, Microsoft has provided a handy article which provides just that information!

HttpStatusCode Enumeration

https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.118).aspx

Using this table as a guide, I could then easily map the Http Status Code numbers back to their .NET equivalents in ASP.NET Web API!


Tuesday, February 23, 2016

Using TortoiseGit with Visual Studio Online

If you are using Git with Visual Studio Online, you may also want to use TortoiseGit as your Git client.

However, as you will soon discover, TortoiseGit does not support Windows Live ID credentials!

Therefore, what do you do in this scenario?

Fortunately, Visual Studio Online supports a scenario called "Alternate Credentials" as described here: https://www.visualstudio.com/en-us/integrate/get-started/auth/overview

You need to set up these alternate credentials in Visual Studio Online in order to be able to use an alternative client such as TortoiseGit in conjunction with Visual Studio Online Git.

You will first need to log into Visual Studio Online and then under "My Profile"-->Security, you will be able to enter your Alternate Credentials as shown below:



Once you have done that, you will be able to log into TortoiseGit using these Alternate Credentials rather than your Windows Live ID credentials!

That is all there is to it!!

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.  

Monday, February 22, 2016

Microsoft Git Provider lacks support for User Tools such as Beyond Compare

I recently began working on a project that was using Git with Visual Studio Online and when I suddenly had the need to integrate Beyond Compare as one of my User Tools, I discovered, I could not do this!

When I go into my Source Control Provider options for "Visual Studio Team Foundation Server", I get the following options:




However, as soon as I switch to using the "Microsoft Git Provider", I only see the following options:


Therefore, I cannot integrate Beyond Compare as one of my User Tools for managing my comparisons and merges as I can when using Team Foundation Server! :-(

If you would like Microsoft to include support for 3rd party tools such as Beyond Compare out-of-the-box, then you should vote for this UserVoice item here: https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/13375314-allow-3rd-party-compare-tools-when-using-git-integ

We can't sign you in using Visual Studio Online

I was recently attempting to connect to Visual Studio Online on a brand new development server that I built when I kept on getting incessant unauthorized error messages.

Well, I went into the "Register Product" menu and attempted to add a Microsoft account:



When I clicked on "Add an account" to attempt to add my Microsoft account, I got the following error message:


Once I saw this error message, I decided to check out my cookie settings in Internet Explorer and sure enough, I found this!


I lowered the cookie security and also added visualstudio.com to my list of Trusted Sites:




Finally, when I attempted to log into Visual Studio Online once more, I could successfully get into the system!


Wednesday, February 17, 2016

INS-20802 Oracle Net Configuration Assistant failed

I was recently attempting to install the Oracle 12c 32-bit client on Windows Server 2012 R2 when I was faced with the following error message:


When I had earlier installed this same Oracle 12c client on Windows Server 2008 R2, I encountered no such problems.

As I was looking for solutions for this confounding error message, I encountered this interesting article: http://www.staygeo.com/2015/07/resolving-ins-20802-oracle-net.html

It directed me to go ahead and download the Microsoft Visual C++ 2010 Redistributable (x86) from here: https://www.microsoft.com/en-gb/download/details.aspx?id=5555

After installing the Visual C++ Redistributable as outlined in the article, the Oracle 12c client installed without issue!


Friday, February 12, 2016

Scripting your SQL Server Maintenance Tasks

When you normally set up maintenance for your SQL Server databases, most of the time you will end up manually setting up these jobs and tasks from within the SQL Server Management Studio User Interface.

However, if you like to simplify the management of these tasks, then it makes sense to use scripts to accomplish this.

Fortunately, a set of scripts already exist that allow you to do just that!

You can download the SQL Server Maintenance scripts from here that allow you to perform a wide variety of SQL Server Maintenance operations effortlessly: https://ola.hallengren.com/

Updating IIS SSL Certificate Bindings using PowerShell

I recently had a requirement to use PowerShell to automatically update the SSL Certificate bindings in IIS so I started hunting around for scripts that would help me accomplish this.

Initially, I encountered this script which provided some insight on how to accomplish this:  http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in

Unfortunately, this article was quite old and outdated and relied on a PowerShell Snap-In which I could not easily determine how to load.

Thankfully, I then came across this much, much NEWER article which is much, much more helpful in determining exactly how to accomplish this task: https://blogs.technet.microsoft.com/heyscriptingguy/2015/03/01/weekend-scripter-use-powershell-to-update-ssl-bindings/

I ended up using this slightly modified version of that PowerShell script to achieve my goals:


[CmdletBinding()]
Param (
 [Parameter(Mandatory = $True, HelpMessage = "Please enter the name of the Web Site")]
 [string]$WebsiteName = "Default Web Site",
 [Parameter(Mandatory = $True, HelpMessage = "Please specify the SSL Port Number")]
 [string]$SSLPort = "443",
 [Parameter(Mandatory = $True, HelpMessage = "Please enter the SSL Certificate Common Name such as *.microsoft.com")]
 [string]$SSLCertSubject
)

Import-Module WebAdministration

Import-Module PKI


function Get-CertificateThumbprint
{
 Param ([string]$CertificateSubject)
 
 $CertThumbprint = (Get-ChildItem -Path cert:\LocalMachine\My -Recurse | Where-Object { $_.Subject -like "CN=$CertificateSubject*" } | Select-Object Thumbprint).Thumbprint
 
 return $CertThumbprint
}


$thumbPrint = Get-CertificateThumbprint -CertificateSubject $SSLCertSubject
$IPAddress = "0.0.0.0"

Clear-Host

New-WebBinding -Name $WebsiteName -IPAddress "*" -Port $SSLPort -Protocol "https"

Get-Item -Path "cert:\LocalMachine\My\$thumbPrint" | New-Item -Path "IIS:\SSLBindings\$IPAddress!$SSLPort"

Wednesday, February 10, 2016

Download Windows Server 2012 R2 Evaluation

I recently had to download an evaluation version of Windows Server 2012 R2 and soon ended up in an infinite loop attempting to download the ISO image from Microsoft TechNet here: https://msdn.microsoft.com/en-us/windowsserver2012r2


Fortunately, I ended up at this link which ACTUALLY allowed me to download the evaluation ISO image of Windows Server 2012 R2: https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2012-r2

Where is the Global Assembly Cache?

I recently had to install some assemblies into the Global Assembly Cache so I needed to figure out where to put my assemblies in the GAC!

Interestingly enough, there are 2 locations for the Global Assembly Cache:

In the case of .NET Framework 4.0 and 4.5, there are x86 and x64 subfolders.  In addition, when installing assemblies into the GAC, you need to use the appropriate version of gacutil.exe as noted here: http://samirvaidya.blogspot.com/2016/02/where-is-gacutilexe.html

Where is gacutil.exe?

If you are looking for gacutil.exe, you may have noticed that it no longer ships natively with the .NET Framework and instead ships separately with the Microsoft Windows SDK.

Depending on what OS you are running will determine what version of the Microsoft Windows SDK you will want to install, but for this example, I will use the Microsoft Windows SDK for Windows 7 and .NET Framework 4: https://www.microsoft.com/en-us/download/details.aspx?id=8279

One of the confusing things about this version of the Microsoft Windows SDK is that it ships with 2 versions of gacutil:

  • gacutil.exe for .NET Framework v. 3.5
  • gacutil.exe for .NET Framework v. 4.0
If you use the default location for gacutil.exe, you will be using the .NET Framework v. 3.5 version:

  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\gacutil.exe
  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\x64\gacutil.exe 

However, the location of gacutil.exe for .NET Framework v. 4.0 is located here:

  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\gacutil.exe
  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\x64\gacutil.exe
Therefore, if you are targeting .NET Assemblies for .NET 3.5 and .NET 2.0, then you will want to use the older version of gacutil.exe, but if you are targeting .NET Assemblies for .NET 4.0 and .NET 4.5, then you will instead want to use the newer version of gacutil.exe to target these assemblies.

If you accidentally use an older version of gacutil.exe against a newer .NET assembly (ex. gacutil.exe for .NET 3.5 against a .NET 4.0/4.5 assembly), you will encounter the following error message:

"Failure adding assembly to the cache:  This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."

If you are interested in the other Windows SDKs, you can check them out here:

Connecting to an Oracle database using Oracle Client

If you have to connect to an Oracle database, you will need to know how to configure the Oracle Client in order to connect to the Oracle databases.

The process consists of 2 parts:

  1. Configuring an Oracle Listener
  2. Configuring 1 or more Net Service Names 

First up, I will demonstrate how to configure the Oracle Listener:








Once you have the Oracle Listener up and running, you then have to configure your Local Net Service Name (for your tnsnames.ora file):

















While configure the Local Net Service Name and testing the connection, you may have to change the login credentials by using the "Change Login" dialog.

Once you have done that, you still need to configure the Data Sources in the ODBC Data Sources section (odbcad32):








 When configuring your ODBC Data Source, you will need to use the TNS Service Name (Local Net Service Name) that you configured earlier.

Finally, you can use an Oracle connection string such as one of the following to connect to your Oracle database instance! (The name of your Data Source is the name of your ODBC DSN): http://www.connectionstrings.com/oracle/


Friday, February 5, 2016

SharePoint 2016 Release Candidate released!

SharePoint Server 2016 Release Candidate has been released!  Unfortunately, unlike earlier releases, it is not a complete installation release but rather a patch on top of SharePoint Server 2016 Beta 2.

Therefore, you will first need to download and successfully install SharePoint Server 2016 Beta 2 and then apply the Release Candidate Patch on top of the Beta 2 installation.

You can read more about the SharePoint 2016 Release Candidate in the links below:

https://blogs.office.com/2016/01/20/sharepoint-server-2016-and-project-server-2016-release-candidate-available/

https://technet.microsoft.com/en-us/library/mt346121(v=office.16).aspx

http://www.computerworld.com/article/3027969/web-apps/how-to-install-microsoft-sharepoint-server-2016-rc.html

Finally, you can download SharePoint Server 2016 Release Candidate (Patch) from here: https://www.microsoft.com/en-us/download/details.aspx?id=50737

Monday, February 1, 2016

Profiling your .NET Applications with Visual Studio 2015

It seems that with every new release of Visual Studio, Microsoft changes how you perform various operations.  In the case of Visual Studio 2015, Visual Studio 2015 is not the exception.

In particular, performing profiling in Visual Studio 2015 has changed from how you used to perform profiling in Visual Studio 2013.

In Visual Studio 2015, Profiling capabilities are now found beneath the Debug menu:





Once you have that in place, you have to set up your Profiling settings via Performance Explorer:


You then go into the Property Pages for the Performance Session in order to turn on Instrumentation:


You can read more details about how to exactly instrument and profile your application here: https://msdn.microsoft.com/en-us/library/dd264993.aspx