Thursday, March 31, 2016

Advanced Installer now supports creating virtual machines!

The latest release of Advanced Installer (Advanced Installer 11.8) now supports creation of virtual machines!

You can see how to create a virtual machine for testing your installations here:


Tuesday, March 29, 2016

SharePoint 2016 RTM has been released!

If you haven't yet heard the news, SharePoint 2016 RTM has been released!

You can now download a trial version of SharePoint 2016 RTM from here: https://www.microsoft.com/en-us/download/details.aspx?id=51493

If you want to read up more on SharePoint 2016, you should definitely check out these links:


One of the welcome changes to SharePoint 2016 is the removal of stsadm.exe and complete migration to  PowerShell commands, however, on a sadder note, SharePoint Foundation 2013 is the last free edition of SharePoint and now all SharePoint editions will require paid licensing....

And while it might still be quite some time before all of the features are available for use considering that SQL Server 2016 has yet to be released as well as Windows Server 2016, but at least you can begin experimenting with this new release and seeing how much the platform has changed since SharePoint 2013!!

Monday, March 28, 2016

Could not load file or assembly 'Telerik.Sitefinity.Resources' or one of its dependencies. Strong name signature could not be verified

I regularly monitor my hosted websites for possible failures and I recently received an alert for one of my Sitefinity websites indicating that it was no longer responding.

When I ended up accessing the website, I was faced with the following error message:


No matter what I tried in terms of shutting down and re-starting the site, refreshing the bin directory etc., nothing seemed to work!

I even did a file system comparison with the site running on my local virtual machine and the files were IDENTICAL!

Since I completely ran out of other ideas and options, I decided to try and upgrade my system from v. 8.1.5820 to v. 8.2.5900.

After completing the upgrade and re-uploading the new files to my hosted website, my site worked just as it did before!

I am still not sure of the exact cause of this failure of my hosted Sitefinity website, but if you also encounter this problem with your Sitefinity v. 8.1.5820 instance, a minor upgrade may resolve it for you as well!!

Upgrading Sitefinity versions just got easier!

In the past, upgrading Sitefinity from one version to the next has been inherently very difficult.

However, I was just recently upgrading one of my Telerik Sitefinity sites from v. 8.1.5820 to v. 8.2.590 when I was surprised to see the following screen:



In the past, I would have to wait for some incredible length of time wondering if the site was actually being upgraded or was simply hanging during the upgrade process, therefore, these informative screens are a most welcome change to the Sitefinity product especially when going through version upgrades!

Hopefully the Sitefinity team will continue to make these useful upgrades as they continue to work on and improve the product!!

Changes to Visual Studio Web Essentials!

If you have been using Visual Studio Web Essentials in the past, you may be surprised to know that Visual Studio Web Essentials has changed with the release for Visual Studio 2015:

https://visualstudiogallery.msdn.microsoft.com/ee6e6d8c-c837-41fb-886a-6b50ae2d06a2

Several of the features that were available as part of the Web Essentials extension has now been split up into several other extensions as follows:


You can find the links for these other useful extensions directly from the Web Essentials download in Visual Studio Gallery.


Friday, March 25, 2016

Mocking a private property setter in your Unit Tests using Moq

I recently encountered a requirement whereby I had to set the value for a property that had a private setter in my Mock objects.

Fortunately, Moq has a solution for this!

You can use the .SetupGet method to set a private property as follows:


myMock.SetupGet(m => m.MyPrivateSetter).Returns(myValue);

If you want to read more about how to accomplish things using Moq, you can read the QuickStart and API Docs here:

https://github.com/Moq/moq4/wiki/Quickstart

http://www.nudoq.org/#!/Packages/Moq/Moq/Mock(T)/M/SetupGet(TProperty)

Thursday, March 24, 2016

Finding matching values between 2 collections using LINQ

If you ever have a requirement to find matching values between 2 collections (especially if they are collections of classes) using LINQ, you might be wondering exactly how to accomplish this.

Well, fortunately, there are multiple solutions on solving this dilemma!

The most readily obvious solution involves leveraging properties on these classes which consist of primitive types such as strings as follows:


public class MyClass
{
    public string Name {get; set;}
}

List<MyClass> coll1 = new List<MyClass>()
{
  new MyClass() {Name="Value1"},
  new MyClass() {Name="Value2"}
 };

List<MyClass> coll2 = new List<MyClass>()
{
  new MyClass() {Name="Value1"},
  new MyClass() {Name="Value3"},
  new MyClass() {Name="Value2"}
 };

 
var results = coll2.Where(x => coll1.Any(z => z.Name == x.Name));

Therefore, by using the Any extension, you can find matching values.

Alternatively, you can also override the Equals method in your class so that you can use the .Contains method as follows:


public class MyClass
{
 public string Name {get; set;}

 public override bool Equals(Object obj)
 {
  if (obj == null || GetType() != obj.GetType()) 
  {
   return false;
  }
  
  MyClass t = (MyClass)obj;

    if (t.Name.Equals(this.Name))
    {
  return true;
  }//if
  
  return false;
 }

 public override int GetHashCode()
 {
  return base.GetHashCode();
 }

}

var results = coll2.Where(x => coll1.Contains(x)).ToList();

That is all you need to do!!

An expression tree may not contain a call or invocation that uses optional arguments

I was recently writing Unit Tests with Moq, when I suddenly encountered this error message while typing one of my Async methods:

"An expression tree may not contain a call or invocation that uses optional arguments"

However, when I was looking at my existing code, I was calling the code with just 1 argument, so what was the problem?


Well, as it turns out, when this method is called as part of a lambda expression as when used with Moq, calling methods with optional arguments is no longer acceptable.

Therefore, this lambda expression DOES NOT WORK:


scope.MyMock.Setup(x => x.GetAsync(myList).GetAwaiter().GetResult()).Returns(myList);

Instead, I had to modify my lambda expression to the following:


scope.MyMock.Setup(x => x.GetAsync(myList,CancellationToken.None).GetAwaiter().GetResult()).Returns(myList);

That was all there was to it!!

Building LINQ Expressions Dynamically

I recently had a requirement to chain together LINQ Expressions and oddly enough, I could not find an easy way to accomplish this!!

You can read more about this dilemma here: https://www.simple-talk.com/dotnet/.net-framework/giving-clarity-to-linq-queries-by-extending-expressions/

Fortunately, I found a library to accomplish this written by the same author of LINQPad: http://www.albahari.com/nutshell/predicatebuilder.aspx

Unfortunately, from this article it is not obvious that this library belongs to the NuGet Package LINQKit: https://www.nuget.org/packages/LinqKit/

You can read more about LINQKit here: http://www.albahari.com/nutshell/linqkit.aspx

In addition, there are several other libraries such as PredicateExtensions which assists with this as well: https://github.com/EdCharbeneau/PredicateExtensions

It is also available as a NuGet package: https://www.nuget.org/packages/PredicateExtensions/

Unfortunately, the PredicateExtensions NuGet package is not strongly signed, so you may encounter problems with using this assembly in your own strongly signed projects.




Understanding Routing in ASP.NET Web API

If you are interested in learning more about Routing in ASP.NET Web API, then you should definitely check out these articles:

http://www.asp.net/web-api/overview/web-api-routing-and-actions

However, one thing I discovered as part of my development which is not obviously covered in these articles are the nuances of routing in ASP.NET Web API.

For example, if you have this type of a method signature defined in Web API:


public IEnumerable<Order> Get(int id)

You will then need to define a route similar to this:


[Route("{id}")]

However, if you instead define a method signature like the following:


public IEnumerable<Order> Get(int orderId)

Your existing route WILL NOT WORK!

Instead, you will have to define your route like this:


 [Route("{orderId}")]

Alternatively, you can do something like the following as well to match them up:


public IEnumerable<Order> Get(int [FromUri(Name = "id")] orderId)

This subtle nuance is not well documented in Microsoft's MSDN documentation nor on the ASP.NET website, but it is useful to know!!

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!


Using the Attach method in Entity Framework

If you are working with Entity Framework, you might encounter situations where you end up working with older versions of Entity Framework that still use ObjectContext whereas newer versions of Entity Framework use DbContext instead.

In any case, if you end up working with some old code or code samples that use ObjectContext, you may end up seeing references to using the Attach method.

However, one of the problems with using the Attach method is that it will not do anything if the object state is not tracked as modified!

Therefore, even when you call SaveChanges, your changes may not be persisted to the database!

Fortunately, there is a solution to resolving this problem by setting the EntityState to Modified.

This is outlined in this article: https://msdn.microsoft.com/en-us/data/jj592676.aspx

So your code will probably end up looking like this instead:


context.Attach(existingBlog);
context.Entry(existingBlog).State = EntityState.Modified; 
context.SaveChanges();

You can read up more about Attaching and Detaching objects using ObjectContext on MSDN here: https://msdn.microsoft.com/library/bb896271(v=vs.100).aspx

Wednesday, March 16, 2016

Migrate from Oracle to SQL Server--for FREE!

Microsoft is offering FREE licenses of SQL Server for customers wishing to migrate away from Oracle!

SQL Server on Linux!!

If you haven't already heard the news, Microsoft has recently announced that SQL Server will become available on the Linux platform!!

https://blogs.microsoft.com/blog/2016/03/07/announcing-sql-server-on-linux/

Unfortunately, you will have to wait until sometime next year (in 2017) to get this exciting release of SQL Server, but you can follow the progress of this product development here: https://www.microsoft.com/en-us/server-cloud/sql-server-on-linux.aspx

Removing all comments from a C# file

If you end up doing a lot of refactoring in your Visual Studio project/solution, you may quickly notice that your comments quickly become OUT OF DATE!!

Well, unfortunately, there is no easy way to clean up all of your code comments in a C# class file using Visual Studio--UNTIL NOW!!

If you use a tool such as Atomineer Pro, you now have the option to "Delete Documentation from this File":


Even better, once you delete all of the documentation from the C# file, you can then choose "Document this File" and refresh your C# class file with all new comments!

How cool is that??

You can download Atomineer Pro from here: http://www.atomineerutils.com/

Better Unit Testing with Fluent Assertions

When you are using Unit Testing with a framework such as MSTest, the assertion information and exceptions you get from the results of your Unit Tests may not be very indicative of the actual problem nor very informative.

Fortunately, there are alternative assertion frameworks such as Fluent Assertions!  http://www.fluentassertions.com/

Fluent Assertions allows you to provide much more informative assertions and error messages than you get with just using MSTest assertions.

You can read the documentation about how to use Fluent Assertions here: https://github.com/dennisdoomen/fluentassertions/wiki

Tuesday, March 15, 2016

Creating T4 Templates using Visual Studio 2015

If you are not already familiar with T4 Templates, it is the syntax that Visual Studio internally uses for all of its code generation through Project Templates, Item Templates as well as tooling for ASP.NET MVC, Web API, Entity Framework etc.

Therefore, you may find it useful to leverage T4 Templates in your development team to reduce the amount of hand coding and manual effort that is required to develop your projects.

If you want to learn about writing T4 Templates using Visual Studio, you will definitely want to check out these MSDN articles first:



However, one of the best ways to learn how a T4 template is constructed is to open up an existing T4 template that Microsoft uses!

For Visual Studio 2015, the ProjectTemplates are located here:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates

and the ItemTemplates are located here:  
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates

Most developers will want to target the C# or the Web folders which are listed individually as:

  • CSharp
  • CSharp\Web
  • Web\CSharp
The folders you will want to examine are generally beneath the 1033 folder.

Below is an example class file which shows how to use some T4 syntax:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Friday, March 11, 2016

xUnit.Net replacement to TestCategory in MSTest

If you are migrating your Unit Tests from MSTest to xUnit.Net, you may be wondering how to migrate the TestCategory attribute available in MSTest over to xUnit.Net.

Well, fortunately, you can accomplish that pretty readily using the Trait attribute!!

http://dennymichael.net/2015/01/23/how-to-extendmigrate-traitattribute-to-xunit-v2-and-why-has-become-sealed/

Therefore, instead of something like this in MSTest:

[TestMethod, TestCategory(Unit)]

You will decorate your xUnit.Net Unit Test methods like this:

[Trait("Category", "Unit")]
[Fact]


Even better is that you can simply decorate your entire class with this Trait attribute so that you do not have to categorize your individual Test methods!

[Trait("Category", "Unit")]
public class UnitTest1


Unfortunately, when you run your tests in Test Explorer in Visual Studio, they still display in the following manner:






On the bright side, if you install the Resharper extension for xUnit (https://resharper-plugins.jetbrains.com/packages/CitizenMatt.Xunit/2.3.3), you will instead get Test Windows similar to the following:






You can learn how to download and install the Resharper extension for xUnit from here: https://resharper-plugins.jetbrains.com/packages/CitizenMatt.Xunit/Download

Goodbye MSTest--Hello xUnit.Net!

If you haven't already read the latest news on ASP.NET 5, MSTest as a Unit Testing framework is being replaced by xUnit.Net!

If you have used MSTest for the last 10+ years (as I have), then this will mean learning and transitioning to a newer Unit Testing Framework.

Fortunately, there are plenty of sites and articles on learning xUnit.Net already!



When you are finally ready to get started with xUnit.Net, you can download the NuGet package from here: https://www.nuget.org/packages/xunit/

Database Integration Tests with Entity Framework

If you are using Entity Framework and want to use Database Integration tests as part of your test suite, then you will want to definitely check out this article: http://tech.trailmax.info/2014/03/how-we-do-database-integration-tests-with-entity-framework-migrations/

It provides insight on how to seed data into your database and then ensure that the data is subsequently rolled back out of the database!

Wednesday, March 9, 2016

Great alternative to ASP.NET Web API Help

If you are looking for a better alternative to using ASP.NET Web API Help, then you will definitely want to check out Swashbuckle for Swagger: https://www.nuget.org/packages/swashbuckle

https://github.com/domaindrivendev/Swashbuckle

Swashbuckle for Swagger provides significantly more Web API Help functionality than is provided by ASP.NET Web API Help.

For example, this is the ASP.NET Web API Help even with XML Comments in place:




However, when you add the Swashbuckle NuGet package to your ASP.NET Web API Web Application (with XML Comments enabled in SwaggerConfig.cs), you get this instead:



You will immediately notice that much like ASMX and WCF Services, you can directly test the ASP.NET Web API services from the Swashbuckle Help!

How cool is that??

For additional guidance on incorporating Swashbuckle/Swagger into your ASP.NET Web API Web Application, you can check out this article: http://www.codeproject.com/Articles/1078249/RESTful-Web-API-Help-Documentation-using-Swagger-U




Using the async and await keywords with ASP.NET Web API

If you are looking for a Microsoft/MSDN article on how to use the async and await keywords with ASP.NET Web API Controller methods you will not find such an article very readily.

If you are looking to call your ASP.NET Web API Controller methods asynchronously from a .NET Client, then you can refer to this article: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

However, if you actually want your Web API Controller methods to be decorated with the async keyword, then you will want to refer to this article: http://www.codeproject.com/Tips/805923/Asynchronous-programming-in-Web-API-ASP-NET-MVC

If you need to be able to call a synchronous method asynchronously, you can use Task.Run as follows:


return await Task.Run(() => DoIt());

Therefore, your final ASP.NET Web API Methods will look something like this:


public async Task<IHttpActionResult> MyAsyncGet()
        {
            return await Task.Run(() => DoIt());
        }

private IHttpActionResult DoIt()
{
   return Content(HttpStatusCode.OK, "some string");
}

The source files could not be downloaded REVISITED

I was recently attempting to install .NET Framework v. 3.5 on my Windows Server 2012 R2 server, when I got this dreaded error message:


Well, I had encountered this error message numerous times before and therefore followed my instructions from a previous article: http://samirvaidya.blogspot.com/2015/03/the-source-files-could-not-be-downloaded.html

Unfortunately, this time it DID NOT WORK!!

Well, after a tremendous amount of Internet-based research, I discovered numerous complaints about a series of Windows Hotfixes/Updates which may have caused/introduced this problem.

I also found this MSDN article which seems to indicate that this issue is caused by security update 3005628:  https://msdn.microsoft.com/en-us/library/hh506443.aspx?f=255&MSPPError=-2147217396

https://support.microsoft.com/en-us/kb/3005628

So, naturally, I decided to patch my OS to the latest set of Windows Updates and run the command once again:


This time the same command DID WORK!

Therefore, if you have recently patched your OS and you are also encountering this problem, verify that your Windows Server 2012 R2 OS installation is fully patched!  This could be the root cause of all of your woes!!

Tuesday, March 8, 2016

Unit Testing and Mocking with Entity Framework

One of the major problems with Unit Testing .NET applications in the past has been the inability to test data access layers that leverage Entity Framework.

Fortunately, though, Microsoft has now provided an article which describes how to do just that!

Mocking Entity Framework when Unit Testing ASP.NET Web API 2
http://www.asp.net/web-api/overview/testing-and-debugging/mocking-entity-framework-when-unit-testing-aspnet-web-api-2

This article describes how to implement an interface for Entity Framework that will then not only allow you to use Unit Testing and Mocking for your Entity Framework classes, but also leverage Dependency Injection!

All of this functionality comes with using the newer DbContext functionality that was made available since Entity Framework v. 4.1!

Calling ASP.NET Web API using jQuery and AJAX

If you are wondering how to call ASP.NET Web API using jQuery AJAX calls, you can find a good walkthrough on just how to accomplish this here: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Friday, March 4, 2016

An error occurred while Windows was synchronizing with time.windows.com

I recently noticed that one of my Windows machines was out of sync with the actual time, so, naturally I decided to try and go to sync up my time with the Internet time when I suddenly encountered this error message:


Fortunately, Microsoft provides multiple time servers as an alternative to "time.windows.com":



So, I decided to switch to one of the other Internet time servers such as "time.nist.gov":


As you can tell from the screenshot above, that did the trick!


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: