Nuget Package Errors DNX451 & ASP.net 5.0

We've been early adopters of Asp.Net 5.0 and MVC6 by using the framework for an A/B test that we are performing.

One thing that has been most apparent is the impact of nuget packages in the application and more specifically the nightmare that can unfurl when with nuget packages can't be found and and the project won't build.

The key thing that took me a while to figure out is that using the command line along side the Visual Studio tools is imperative to getting the application to build correctly against the correct version of the framework.

In the early stages of the framework when the Beta releases are being released against a consistent release cycle it's important to know the few key commands to get you through the pain that can occur when you aren't aware of whats going on with your packages.

What we've found is that right clicking on the project and selecting "restore packages" can sometimes work but many times just fails.

In the instances where the application won't build, there are a few steps we can take to get the application to work, this is really important in the case where you might have upgraded your application to the latest beta but the rest of the team you work with haven't.

If you haven't set up the visual studio command line you can find the details on how to do this here;

http://docs.asp.net/en/latest/getting-started/installing-on-windows.html

I always start by navigating my powershell location to the project in question.

The first step I get the guys to do when something won't build is:

dnvm upgrade - this will upgrade the dotnet version manager to include all of the latest versions of the framework.

Once we have all the versions we should choose the version that we wan't to use our project by executing:

dnvm use --version 1.0.0-beta8 (with the version being the version you wish to use against your project).

Performing dnvm list should show the list of current projects and you should see a star next to the project you've set for usage.



Once we have selected the version that we want to use, we need to make sure that the packages in our application are correct and matching the version of the runtime that we are using.

In order for this to work we need to ensure that all of our nuget package locations are in check, the locations I have setup in my global nuget config are as follows:

 <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
    <add key="AspNetVNext" value="https://www.myget.org/F/aspnetcirelease/api/v3/index.json" />
    <add key="Roslyn" value="https://www.myget.org/F/roslyn-nightly/" />
    <add key="DotNet" value="https://www.myget.org/F/dotnet-corefx/" />
  </packageSources>

You can find the global nuget file at: C:\Users\<your account>\AppData\Roaming\NuGet.

The final step to run is the restoration of the nuget packages by running the following command in the project directory location:

dnu restore --no-cache

This should restore all of the packages to their correct state with the correct versions of the packages for the currently selected runtime. from here we can navigate back to Visual Studio, rebuild our project and all of our dependency errors should disappear!




StructureMap and ASP.net 5 VNext

I've created a working example of how to integrate StructureMap with ASP.net 5. IoC in VNext comes out of the box but if you want to bring your own container you can.

Below you can find a full working example of how to integrate with MVC 6.

https://github.com/aaron-hammond/StructureMap.VNext


EDIT: I removed this repo, as it got stale,  proper support has been created here:

https://github.com/structuremap/structuremap.dnx

Shouldn't need to roll our own now:

MVC6 and the IHttpContextAccessor

I've been working a lot with ASP.net 5 recently using the RC of visual studio 2015. There is considerable changes in both the .net framework and of course MVC.

One of the most significant changes is the dependency of APS.net 5 on the System.Web DLL. This DLL, that pretty much underpinned all that was web development, has been broken up into independent granular nuget packages that allow you to ship your application with exactly what you need.

Applications that we've used in the past have relied heavily on the HttpContext class allowing us to access the current context by the static property HttpContext.Current.

In ASP.net 5 large parts of the framework are now Dependency Inject-able. In order to access the current context you can simply inject the IHttpContextAccessor into your class in order to access the current context.


The beauty of this is we can then create our mocked HttpContext using the DefaultHttpContext object that can be found in Microsoft.AspNet.Http.Core.dll




Don't forget all .net code is now open sourced and this particular interface can be found at:

https://github.com/aspnet/Hosting/blob/release/src/Microsoft.AspNet.Hosting.Abstractions/IHttpContextAccessor.cs

The interface has also been moved to:

https://github.com/aspnet/HttpAbstractions/tree/release/src/Microsoft.AspNetCore.Http.Abstractions

Update:

As of RC2 some changes have been made to the IHttpContextAccessor and it's availability. The announcement on this change can be found here:

https://github.com/aspnet/Announcements/issues/190



AWS Deploy Failure

In trying to use the AWS Toolkit to deploy our .net application packages to elastic beanstalk we encountered a rather strange error:

[Error]: Caught exception probing for redeployment mode, Object reference not set to an instance of an object.
Failed to parse deployment configuration file: Deployment failed during post-processing of configuration settings.

Unfortunately this error told me very little and searching the web provided no resolve.

After speaking to Amazon it tuns out that the Frankfurt region does not support any version of the AWS toolkit prior to 2.3.4.

Updating the toolkit ultimately fixes the deployment issue.

http://aws.amazon.com/visualstudio/

Git Merge In Visual Studio

I recently switched job roles and as always, with moving to a new company, you get a new machine that just doesn't feel right until you've installed and set up every application that you need to get developing.

Probably one of the most crucial aspects of this for me is setting up Merge for Git. I've played around with a couple of merge tools since I started using Git, first KDiff and then Beyond Compare and I have to say that neither have come remotely close to the built in Visual Studio Merge tool I've used in the past working with TFS.

The most frustrating thing I found moving to Git was this poor merging and at the early stage it nearly put me off completely. That was, until a colleague mentioned that it was possible to use the built in VS merge tool with git. 

This was a revelation, but how?

It's quite simple actually, you just need to edit your git config that can be found at 
'‪C:\Users\<USERNAME>\.gitconfig'

Then copy in the following settings:

[user]
name = <gitusername>
email = <yourname@youremail.com>

[diff]
    tool = vsdiffmerge
[difftool]
      prompt = false
[difftool "vsdiffmerge"]
      cmd = '"C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/vsdiffmerge.exe"' "$LOCAL" "$REMOTE" //t
      keepbackup = false
      trustexitcode = true

[merge]
      tool = vsdiffmerge
[mergetool]
      prompt = false
[mergetool "vsdiffmerge"]
      cmd = '"C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/vsdiffmerge.exe"' "$REMOTE" "$LOCAL" "$BASE" "$MERGED" //m
      keepbackup = false
      trustexitcode = true

It's useful to note I tend to close my project before I do any merging, especially if the .csproj file is conflicted, trying to merge this with the project open will cause Visual Studio to crash. 

Git should fire up a new instance allowing you to merge the necessary files once the merge is in progress.