Deploy ASP.Net 5 apps in IIS

Deploying an ASP.Net 5 web application in IIS

The preview of Visual Studio 2015 contains no built in tooling to directly deploy web apps to an IIS, this post describes a way of doing it without tooling.

Prepare project for IIS deploying using command line tools

Running commands can be done from any prompt that has the K tools in the current path. The Package Manager Console from within VS2015 works fine if you have installed the K toolset already.

First off, pack the site you intend to publish by:

1. Make sure you are located in the project root, the folder containing the project.json file.

2. Execute the following ‘kpm pack –out outputDirectory –runtime choosenRuntime‘, for ex ‘kpm pack –out C:\tmp\WebApp –runtime KRE-CLR-amd64.1.0.0-beta1′.

(If you are going to host this on an IIS locally or on a server, use a CLR version of the runtime. If you plan on deploying the package to Azure, use a Core version of the runtime.)

3. Create a new Website in IIS pointing to the chosen outputDirectory/wwwroot. (In our ex above C:\tmp\WebApp\wwwroot)

Happy Hacking! :)

Polymer Bower packages in Visual Studio 2015

Working with Polymer in Visual Studio 2015 (Preview)

In the new VS2015, there is built in Bower and npm support. This is great if you want to use Visual Studio to build Polymer components and apps. Importing the basic Polymer packages in to VS2015 is not completely straight forward unfortunately, as the autocomplete for the Bower packages doesn’t seem to work a 100% just yet.

Importing the Polymer packages with Bower in to VS2015

To import the packages polymer, polymer/core-elements and polymer/paper-elements you first edit the bower.json file in your VS2015 web project. The way to insert them is under the dependencies tag, see code snippet.

"dependencies": {
     "polymer": "^0.5.1",
     "core-elements": "Polymer/core-elements#^0.5.1",
     "paper-elements": "Polymer/paper-elements#^0.5.0"
}

After the bower.json file is edited, you will see the packages pop up under your Bower dependencies. Then you need to right click the Bower packages header and select Restore Packages to actually download the packages from Bower. See picture.
VS2015BowerRestore

Then check your Output Window and select Package Manager Log to see what’s being imported from Bower and make sure there are no errors!

Happy Coding! :)

PowerShell – Working with JSon

Manipulating JSon with PowerShell

A few tips about working with JSon in PowerShell. (more…)

post image

Windows 8.1 Command Binding in a DataTemplate

DataTemplate and Bindings

In Windows 8 there are a few controls that rely on a DataTemplate to show item collections.

The DataTemplate is used to define visual layout for each item that will be rendered in an ItemsControl, it data binds nicely to collections of objects. It then iterates through the collection and uses each item as context for every new Template that is created.

Binding to Commands in the DataTemplate

When binding Commands to controls we usually bind them to Commands in the ViewModel, set as datacontext.

But if you, for example, have a button defined in the DataTemplate and bind that to a Command defined in your ViewModel, set as the datacontext for the page, it won’t work.

Why doesn’t Binding to Commands work in the DataTemplate?

The reason that the Command isn’t executed is that the Command can’t be found.

Since it’s an item in the collection that’s actually the current DataContext for the item being created, the command can’t be found if it’s defined globally in your ViewModel.

The solution is to refer the Command binding to look further up the object tree to find the Command.

In WPF it was solved using FindAncestor, but that’s removed now in WinRT.

The easiest way to solve it is to actually give the view/page an x:Name. Then refer to the Command in the binding as DataContext.MyCommandName, and set ElementName to the name of the view/page. See code snippets below, how the Command is defined, the x:Name set in Page and the Command defined in the ViewModel.

Code Snippets

ViewModel snippet:

        private RelayCommand headerClickedCommand;
        public RelayCommand HeaderClickedCommand
        {
            get { return headerClickedCommand ?? (headerClickedCommand = new RelayCommand(ExecuteHeaderClickedCommand)); }
        }

        private void ExecuteHeaderClickedCommand()
        {
            NavigateSomewhere();
        }

XAML PageRoot snippet:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      x:Name="MyView"
      ...
      >

XAML DataTemplate snippet:

<DataTemplate>
  <Grid Margin="0,0,0,2">
    <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
				    AutomationProperties.Name="Group Title"
                                    Command="{Binding DataContext.HeaderClickedCommand, ElementName=MyView}"
                                    Style="{StaticResource TextBlockButtonStyle}">
     <StackPanel Orientation="Horizontal">
       <TextBlock Text="{Binding Category}" Margin="0,-11,10,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
        <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-11,0,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
      </StackPanel>
    </Button>
  </Grid>
</DataTemplate>

Happy Coding :)

Tests not running in Visual Studio 2013 with ReSharper 8

Unit tests not executing from ReSharper’s Unit Test Sessions

Pressing Ctrl+U,L resulting in nothing but spinning bars? I had this problem trying to run my unit tests from Visual studio 2013 using ReSharper 8.

The test indicators would keep spinning but nothing happened, setting breakpoints on the first row on the code showed that the unit tests actually never even started executing. No matter how long time that passed (ok I stopped waiting after 3 minutes to be honest) the bars just kept spinning and no error shows up.

After searching the web it turns out this is a known issue, stated to be solved in several ReSharper releases. Not sure if it’s the case and the bug has just resurfaced in the new ReSharper 8 version.

The easy solution

Turns out this problem is avoidable just by starting Visual Studio 2013 as an administrator.

If you got VS2013 pinned ot the taskbar in Windows 8/8.1, this is the easiest way to make it always start in admin mode:

1. Right click the pinned Vs2013 icon and select properties.

Pinned VS2013 Properties2. Click the “Advanced” button in the properties window.

3. Check the “Run as Administrator” checkbox.

Advanced Properties

After this your Visual Studio will always be run as Administrator.

 

Happy Testing :)

The .winmd file ‘Microsoft.Advertising.WinRT.UI.winmd’ contains duplicate type names

Error when using Microsoft Advertising SDK in Windows 8 / Windows 8.1 projects

After updating my Windows 8 app to Windows 8.1 I got a compilation error saying:

"The .winmd file 'Microsoft.Advertising.WinRT.UI.winmd' contains duplicate type names.
Type 'Microsoft.Advertising.WinRT.UI.AdControl' is already registered with the in-process
 server 'CLRHost.dll'.	ListWee_W8_Tests"

For me, this occurred in the test project.

 

After scouring the web I found a few solutions but most of them seemed like hacks, and I feared I would have to revert them later on if I used them now.

The easy solution

The easiest solution for me was to just add a reference to the Microsoft Advertising SDK in the test project.

Adding Microsoft Advertising SDK

After that the compilation works and there’s no need for more hacks :)

post image

Using Azure Mobile Services in Portable Class Libraries now possible!

Using Azure Mobile Services in Portable Class Libraries

It’s now possible using Mobile Services from PCL’s for your data access. Just add the Windows Azure Mobile Services NuGet package to your Portable Class Library and you should be ready to go.

Bcl.Async Error

When I added my package I got some errors however. A prerequisite is having NuGet of version 2.1 or later, but mine was over that.

*(If your’s is of a lower version, make sure to upgrade in Visual Studio via Tools -> Extensions and Updates…)

The error I got is shown in the image below:

BclAsyncError when installing Mobile Services NuGet package

The solution

Seems the Mobile Services package isn’t going to work with Windows Phone 7 and higher selected, so the trick was to raise the version for supported Windows Phone OS to 7.5. See picture below.

Windows Phone 7. 5 and higher selected for PCL

post image

MVVM Light on Windows Phone 8 & Design-Time ViewModels

Using ViewModels to inject Design-Time data

MVVM Light

To make your apps design more tangible when using Blend or Visual Studio we often resort to injecting design-time data in some kind of way.

One of the best ways to do it to actually fake the entire ViewModel and have one that’s used at design-time.
I tried this approach on a new app that I’m also using MVVM Light in. And I couldn’t get it to work at first, I kept getting the error: The name “DesignTimeViewModel” does not exist in the namespace “clr-namespace:MyNamespace.ViewModel”.

How to bind a Design-Time ViewModel to the XAML View

For those that do not know, here’s how you bind a desing-time viewmodel to your views. You need to declare a namespace where your view exists, a reference to http://schemas.microsoft.com/expression/blend/2008 and point out a DesignInstance DataContext. Add the following code in the PhoneApplicationPage-tag:

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:designTimeViewModel="clr-namespace:MyNamespace.ViewModel"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance Type=designTimeViewModel:DesignTimeViewModel, IsDesignTimeCreatable=True}"

With MyNamespace.ViewModel being the namespace your viewmodel is located in and DesignTimeViewModel being the name of your viewmodel.

The way I created my design-time viewmodel was by just copying the regular viewmodel, adding a parameterless constructor and adding data to the properties in that constructor.

The solution

With me using MVVM Light all the viewmodels I ha dimplemented was inheriting from GalaSoft.MvvmLight.ViewModelBase, when copying the viewmodels to create my design-time viewmodels I din’t remove the inheritance. And it turns out that this inheritance was the root of the problem.

So if you remove the inheritance from GalaSoft.MvvmLight.ViewModelBase on your design-time viewmodels the error should disappear.

Happy coding!

post image

WACK Issues

WACK not finding your app?

Screenshot of WACK

Fig 1. Windows App Cert Kit

I had some problems when I tried to run WACK 2.2 (Windows App Cert Kit) to test my new App.

After selecting “Validate Windows Store App” – WACK persisted to show an old test project, in doubles,  instead of the name of my Windows App. If I unistalled my app from the start screen, WACK still showed the old test project as installed, but this time only one time.

Installing my App again for testing, just resulted in a copy of the line describing my old test project as an installed App.

It was clear that my old testproject was some how messign things up, problem is it wasn’t visible on the start screen so there was no way of uninstalling it from there.

How to fix it and uninstall a Windows 8 app manually

Screenshot of Debuggable Package Manager

Fig 2. Debuggable Package Manager

To locate all installed packages I ran the “Debuggable Package Manager” tool shipped with Visual Studio 2012.

In DPM you can list all your installed Apps with the “Get-AppxPackage” command. See Fig 2.

Then use the command “Remove-AppxPackage -name” to remove the package.

Observe!  The name parameter required is the full name of the package that you can find listed for each package in the complete package list, see screenshot in fig 2.

Old App removed

After removing the old package, I could deploy my App again from Visual Studio and actually see it listed with the correct package name in WACK.

And my App passed on the first test run :)

 

Happy coding!

post image

Using Portable Class Library for Azure Mobile Services model

Using Portable class Library to hold your Azure Mobile Services model

If you want to use your model classes in other projects (like for example your upcoming Windows Phone 8 app) you can define them in a Portable Class Library.

Normally when using Azure Mobile Services we use the DataTable attribute on classes to denote their table name when stored in the Azure database.

But if you want to define the model classes from PCL using the DataTable attribute isn’t possible, as the namespace Microsoft.WindowsAzure.MobileServices isn’t available in a PCL.

 
using System.Runtime.Serialization;
using Microsoft.WindowsAzure.MobileServices;

namespace Demo
{
    [DataTable(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }
    }
}

The solution here is to use the DataContract attribute from System.Runtime.Serialization. Make sure though that you got the latest version of the Azure Mobile Services SDK or might not work!

using System.Runtime.Serialization;

namespace Demo
{
    [DataContract(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }

    }
}

For a download of the Azure Mobile Services SDK as a source, visit the GitHub page.

Or use this link for a straight msi download.