Fix: The Dnx Runtime package needs to be installed. See output window for more details

I do not know what caused the problem, but at a given moment, I got the error:

 

The Dnx Runtime package needs to be installed. See output window for more details

 

To fix this problem I entered the following commands in the package manager console:

 

dnvm upgrade

dnvm upgrade –r CoreClr

dnu restore

 

Closed all visual studio 2015 instances and opened the project,  now I could build the project wihout errors.

 

Found my solution at: http://stackoverflow.com/questions/31957824/the-dnx-runtime-package-needs-to-be-installed-see-output-window-for-more-detail

Starting with ASP.NET 5, TypeScript 1.7 and the ES6 module syntax compiled to ES5.

In the previous post I described how you can get started with ASP.NET 5.

 

The code used in this blog post can be found at: https://github.com/roelvanlisdonk/Research/tree/master/Blog/ASP.NET5/TypeScriptES6Modules

 

In this post I will describe how you can get start using TypeScript 1.7 and the ES6 module syntax to order your code in an ASP.NET 5 application.

 

I will explain how you can use a “library” module inside your app code, by using TypeScript and the ES6 module syntax.

The compiled code will be in ES5, so we need a library called “system.js” to load the “ES6 modules” at runtime.

 

  • After you followed the steps in the previous post
  • Add a TypeScript JSON Configuration File to the wwwroot folder

 

image

 

Add "module": "system", under compileroptions:

 

image

 

Add two typescript files:

  • wwwroot/core/app.ts
  • wwwroot/libraries/list/findIndexByItem.ts

image

 

 

 

  • Add the system.js bower package.

This is needed, because the typescript will be compiled into javascript modules.

These modules are in the “system.require” format.

We could use other module formats (see tsconfig.json), but in this example I wanted to use system.js

 

Before we can add the system.js bower package, add a “Bower Configuration File” to the root of the project.

 

image

 

In this example I use the folder “wwwroot/libraries” to store all my runtime JavaScript dependencies, so we have to edit the .bowerrc file, change:{ “directory”: “wwwroot/lib” } to { “directory”: “wwwroot/libraries” } .

 

image

 

Now we can install the system.js bower package:

 

image

 

 

findIndexByItem.ts

 

image

 

 

app.ts

 

image

 

 

index.html

 

The only javascript file we directly load in the index.html is “/libraries/system.js/dist/system.js”. All other logic will be dynamically loaded by the system.js loader.

 

To  make system.js recognize file paths like System.register(["../libraries/list/findIndexByItem"] , so without the “*.js” extension, we need to add a little configuration by using System.config, then we load the “core/app.js” file and start the application.

The “core/app.js” uses the library module “findIndexByItem.js”. This JavaScript file will be dynamically loaded, when “core/app.js” is requested.

 

 

image

 

Result

 

image

 

 

Note: for browsers that do not support promises you need a promise polyfill, that can be found, here: https://github.com/jakearchibald/es6-promise/

Starting with ASP .NET 5 (empty project) and static files.

 

If you are starting with ASP .NET 5 (1.0.0-rc1-final) and you just want to serve an “index.html”, you can follow these steps:

  • Start Visual Studio 2015
  • File > New > Project…
  • Installed > Templates > Visual C# > Web > ASP.NET Web Application
  • Choose .NET Framework 4.6.1

image

 

  • ASP.NET 5 Templates > Empty

image

 

  • Open project.json and add package dependency “Microsoft.AspNet.StaticFiles”: “1.0.0-rc1-final”:

 

image

 

  • Open Startup.cs and remove the app.Run lines:

image

 

  • In Startup.cs add the follwoing lines in the Configure methode beneath app.UseIISPlatformHandler();:
    • app.UseDefaultFiles();
    • app.UseStaticFiles();
  • Like so:

image

 

 

  • Add an index.html file to the wwwroot folder and add the text “Hello world from html” to the body:

 

image

 

 

 

Press F5 and the a browser should appear showing you the “Hello world from html”

 

image

Microsoft ASP.NET and Web Tools 2015 (Beta8) – Visual Studio 2015

Just a reminder beta 8 for ASP .NET is out:

http://www.microsoft.com/en-us/download/confirmation.aspx?id=49442

 

If you don’t install this tooling, you will get an error (see below), when manual upgrading from beta 5 to 8 in the project.json file.

Don’t forget to set the Solution DNX SDK version of your project to 1.0.0-beta8.

 

image

 

Also make sure you set the default document, when using only static files and target the .NET Core runtime.

 

image

 

 

Server Error in ‘/’ Application.


Could not load file or assembly ‘Microsoft.Dnx.Host.Clr’ or one of its dependencies. The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Dnx.Host.Clr’ or one of its dependencies. The system cannot find the file specified.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Assembly Load Trace: The following information can be helpful to determine why the assembly ‘Microsoft.Dnx.Host.Clr’ could not be loaded.

=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Dnx.Host.Clr
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.Dnx.Host.Clr | Domain ID: 3
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/roel/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta5/bin
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Configuration file C:\Program Files (x86)\IIS Express\iisexpress.exe.config does not exist.
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Users\roel\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/roel/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta5/bin/Microsoft.Dnx.Host.Clr.DLL.
LOG: Attempting download of new URL file:///C:/Users/roel/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta5/bin/Microsoft.Dnx.Host.Clr/Microsoft.Dnx.Host.Clr.DLL.
LOG: Attempting download of new URL file:///C:/Users/roel/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta5/bin/Microsoft.Dnx.Host.Clr.EXE.
LOG: Attempting download of new URL file:///C:/Users/roel/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta5/bin/Microsoft.Dnx.Host.Clr/Microsoft.Dnx.Host.Clr.EXE.

Stack Trace:

[FileNotFoundException: Could not load file or assembly 'Microsoft.Dnx.Host.Clr' or one of its dependencies. The system cannot find the file specified.]
[TypeLoadException: The domain manager specified by the host could not be instantiated.]
System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags, PolicyLevel policyLevel, Exception appDomainCreationException) +303
[HttpException (0x80004005): The domain manager specified by the host could not be instantiated.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9922864
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +90
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +261


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.106.0

Server side resource rendering with Angular and ASP .NET MVC: pass data from html to controller.

I wanted a ASP .NET MVC view (*.cshtml) to render on the server and pass the static text contained in the the ASP .NET project resource file to an Angular controller, so on the initial page request, all static data is returned to the client. Dynamic data, like grid content would then be requested by a separate JSON call.

The resource data is passed to the Angular controller by using ng-init.

 

For the sake of this blog post I stuffed everything in one *.cshtml page, including CSS en JavaScript.

Of course this would be separate files in an real application.

 

Project resource file

image

 

Layout.cshtml

 

<!DOCTYPE html> <html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="app"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Send initialization data from HTML to Angular controller</title> <style> /* Add some initial styling. */ html, body { background-color: #F1F1F1; font-family: "Open Sans", sans-serif; font-size: 13px; height: 100%; } body { margin: 20px; } [ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak { display: none !important; } </style> <!-- Library scripts --> <script src="https://code.angularjs.org/1.4.1/angular.min.js"></script> </head> <body ng-cloak> @RenderBody() <div> <div ng-controller="main" ng-init="resources={ textFromResource: '@WebApplication1.Properties.Resources.TextFromResource'}"> <h1>{{ title }}</h1> {{ resources.textFromResource }} </div> </div> <script> // Angular module. (function () { "use strict"; angular .module("app", []); }()); // Angular controller. (function () { "use strict"; function controller($scope) { $scope.title = "Show ASP .NET MVC resoure (*.resx) data to user on initial load."; } angular .module("app") .controller("main", ["$scope", controller]); }()); </script> </body> </html>

 

 

Result

image

Post arbitrary JSON data to a MVC controller action method

If you want to sent arbitrary JSON data to a MVC controller action method, you can use the following code:

[HttpPost]
public ActionResult HandleRequest()
{
// Read the RAW JSON from the request, 
// because MVC does not support a dynamic input parameter or an input parameter of type JObject.
// ASP .NET Web API does support a dynamic input parameter or an input parameter of type JObject.
string json = new StreamReader(this.Request.InputStream).ReadToEnd();
dynamic clientData = JObject.Parse(json);

POST multiple parameters to an ASP .NET Web Api REST service from a .NET 3.5 assembly, by using one dynamic JObject "options" parameter.

 

Client code in .NET 3.5

 

namespace WordMerge.EndToEndTests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using Newtonsoft.Json;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Execute_a_post_request()
{
string url = "http://localhost:63544/api/document";
object result = string.Empty;
// Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
using (var client = new WebClient())
{
// Set the header so it knows we are sending JSON.
client.Headers[HttpRequestHeader.ContentType] = "application/json";
// Create the one and only "options" parameter object.
var dto = new DocumentDto
{
TemplatePath = @"C:\Temp\Templates",
DestinationPath = @"C:\Temp\Destination",
Data = new string[] { "This", " is", " test", " input", " data." }
};
// Serialise the data we are sending in to JSON
string serialisedData = JsonConvert.SerializeObject(dto);
// Make the request
var response = client.UploadString(url, serialisedData);
// Deserialise the response into a GUID
result = JsonConvert.DeserializeObject(response);
}
Assert.AreEqual(@"Succesfully uploaded: This, is, test, input, data.", result.ToString());
}
}
public class DocumentDto
{
public string TemplatePath { get; set; }
public string DestinationPath { get; set; }
public string[] Data { get; set; }
}
}

Server code in .NET 4.5

 

 

namespace Service.api
{
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class DocumentController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public string Post([FromBody]JObject jsonData)
{
// Convert the dynamic JObject to a DocumentDto object.
DocumentDto dto = jsonData.ToObject<DocumentDto>();
// Use the given data to create the result.
string seperator = ",";
string data = string.Join(seperator, dto.Data.ToList<string>());
string result = string.Format("Succesfully uploaded: {0}", data);
return result;
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
public class DocumentDto
{
public string TemplatePath { get; set; }
public string DestinationPath { get; set; }
public string[] Data { get; set; }
}
}

How to POST a string[] array to a ASP .NET Web Api REST service that uses JSON, from a .NET 2.0 assembly

In this post I will use a UnitTest project written in C# .NET 2.0 as client code.

And a ASP .NET Web Api project as Server (service).

 

Client code

Create a new UnitTest project and change target framework to .net 3.5.

Add nuget package JSON.NET.

 

 

 

 

namespace WordMerge.EndToEndTests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using Newtonsoft.Json;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Execute_a_get_request()
{
string url = "http://localhost:63544/api/document";
string result = string.Empty;
// Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
using (var client = new WebClient())
{
// Set the header so it knows we are requesting JSON.
client.Headers[HttpRequestHeader.ContentType] = "application/json";
result = client.DownloadString(url);
}
Assert.AreEqual(@"[""value1"",""value2""]", result);
}
[TestMethod]
public void Execute_a_post_request()
{
string url = "http://localhost:63544/api/document";
object result = string.Empty;
// Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
using (var client = new WebClient())
{
// Set the header so it knows we are sending JSON.
client.Headers[HttpRequestHeader.ContentType] = "application/json";
string[] data = new string[] { "This", " is", " test", " input", " data." };
// Serialise the data we are sending in to JSON
string serialisedData = JsonConvert.SerializeObject(data);
// Make the request
var response = client.UploadString(url, serialisedData);
// Deserialise the response into a GUID
result = JsonConvert.DeserializeObject(response);
}
Assert.AreEqual(@"Succesfully uploaded: This, is, test, input, data.", result.ToString());
}
}
}

NuGet packages config

 

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net35" />
</packages>

Server (service) code

Create an empty ASP .NET Web API project in Microsoft Visual Studio 2013.

namespace Service.api
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class DocumentController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public string Post([FromBody]string[] values)
{
string seperator = ",";
string data = string.Join(seperator,values.ToList<string>());
string result = string.Format("Succesfully uploaded: {0}", data);
return result;
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}

Visual Studio Structure

image

NuGet packages config

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0-rc1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
</packages>