Targeting Runtime for New MVC6 Project in VS 2015 - asp.net

I am using ASP.NET beta 7, VS 2015 on Windows 7.
When I create a new MVC6 project in VS2015 targeting .NET 4.6 framework, my project references look like this:
DNX 4.5.1
DNX Core 5.0
I don't wish to create for cross platform, and would expect my references to look like this (from other tutorials):
ASP.NET 5.0
ASP.NET Core 5.0
From the ASP.NET 5.0 documentation:
What I'm seeing in VS 2015 (no check box option for dnx):
Should I just manually hack the project.json file?
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},

That documentation where you can select the platform is from a previous beta or preview version of ASP.NET 5 so not valid anyone.
Your project.json looks correct.
DNX is an execution environment that can run on various underlying .NET frameworks, including .NET framework 4.5.1,4.5.2 or the latest .NET Framework 4.6.
.NET Core 5 is not .NET Framework 5 but is a modular runtime and library implementation that includes a subset of the .NET Framework 4.X. So you can call it mini .NET 4X. It is not the latest version of .NET framework for Windows. The latest version is .NET Framework 4.6
If you leave your project.json by default as
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
then when you publish your project you'll then be able to publish to either .NET Core or .NET Framework.
clr-win = .NET Framework windows
core-clr-win = .NET Core
if you do not wish to create cross platform you can edit your project.json to
"frameworks": {
"dnx451": { },
},
and you'll not be able to see the Target DNC Version of .NET Core as a choice when you publish your site.
If you need to use a later .net version feature or if you you wish to include and reference a class library which itself targets a later .net framework version to your project you would need to change your project.json to target the same or later .net framework version e.g. dnx452 or dnx46
e.g.
"frameworks": {
"dnx46": { },
},
As soon as you save the project.json file your project will automatically update itself. You can view the progress in the output window.

Related

Change the Target Framework from .NET framework 4.7.2 to .NET core 2.2 for a ASP.NET Core app

I have an existing ASP.NET Core application whose target framework is .NET 4.7.2. I would like to change it to use .net core 2.2 instead.
How to do this?
In the end, I am convinced there is no "one-click" solution and did it manually. Here is what I did
in VS 2019, create a brand new solution targeting .net core 2.2
created each project manually, added nuget packages targeted for dotnet core
copied source code files into each project
make sure the solution build and run

.NET Core and .NET Framework 4.7 edit project option not available

When I create a .NET Core 2.0 Project I have to select both .NET Core version (2.0) and a .NET Framework version (4.7). But when I edit the project, I cannot change the .NET Framework version. Why is that? I am trying to use the function app.UseRewriter(options); and it is unavailable so I am theorizing that I have the wrong .NET Framework
I guess I just needed this package
Install-Package Microsoft.AspNetCore.Rewrite -Version 2.0.1
Yes, that's a confusing GUI. That got me mad as well (like some other stuff in Visual Studio if you are used to Jetbrains-IDEs).
You have chosen an ASP.NET Core Web Application as project type. So the only target frameworks that are being offered to you are .NET Core 1.0, 1.1 and 2.0 (in case you have installed 2.0, like you did).
Chose Web -> ASP.NET Web Application (.NET Framework) to get what you want.

Trying to reference a .NET 4.6.1 class library from a .NET Core project

Screenshots => http://imgur.com/a/NWsbh
I'm trying to reference a .NET 4.6.1 Class library in a .NET Core 1.0.0 project. It's added to references but whenever I try to include the "using" statement in a controller I get "the type or namespace could not be found error."
Any suggestions?
You can either only target .NET 4.6.1, and that way you would not support .net core. Or you can add #if NET461 directives in your code, and that way it will be able to find the type and namespace in the if.
your problem is that the code is unsupported in .net core, and therefore cannot build for .net core
You need to set .NET 4.6.1 as the framework for the project. In your project.json, replace the "frameworks"-section with:
"frameworks":
{
"net461": {}
}

Testing .Net Core Library with xUnit

I am creating a .Net Core Library that can be consumed by ASP.NET Core Application. By default Visual Studio create project.json as below
{
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
},
"version": "1.0.0-*"
}
I think its recommended to use netstandard1.x for libraries that will be consumed by other applications. I am going to remove "imports": "dnxcore50" because that was for older .NET Core preview versions (Before .NET Core 1.0 RTM and .NET Core RC2 were released)
Now I wanted to unit test this library using xunit so followed the article Getting started with xUnit.net (.NET Core / ASP.NET Core)
However xunit suggest to mark the library as .Net Core Application
You may have noticed that part of what we did here was mark your class
library as a .NET Core Application (the netcoreapp1.0 framework). When
using .NET CLI for testing, unit test projects are actually an
application*, not a class library. If you forget to make this change,
the compiler will tell you that dotnet-test-xunit is not compatible
with your class library project. We tell Visual Studio to generate a
class library, though, because it most closely matches our desired
project structure.
The application's Main function is provided by the xUnit.net runner, not by you.
So I have updated the project.josn as suggested and I was able to run the unit test. However I have questions:
1> Is it recommended to change the target framework of the .Net Core library from netstandard1.6 to netcoreapp1.0 when that library can be consumed by other applications?
You'll have one project (and project.json) dedicated to your test project.
You need to mark the Test project as a .NET Core application since it will in fact be executed.
No need to change the target project which is your Library to Core App.
Here you can see the project.json for a test project
And here the library project

Referencing a .NET Core class library in a ASP.NET 4.6 MVC App

I think that I am fundamentally missing something here and I apologise for stupidity in advance. Trying to embrace the new world of dotnet core and working on a project as a way of trying to learn.
I have written some authentication middleware to work for with ASP.NET Core Identity. I have used a .NET Core Class Library for the middleware - the advantage being that I believe I can then potentially target any framework as well as build Nuget packages of my component. The middleware works fine when used with an ASP.NET Core Web app targetting .NET Core - based on the VS2015 RC2 template.
Feeling adventurous I decided to create a version of the component based around OWIN/Katana authentication middleware so I could use it with ASP.NET 4.5/6 web applications. Instead of creating a traditional class library project I once again used the .NET Core Class Library template but targetted it only at .NET 4.6, i.e. net46. It compiles fine but I cannot get my ASP.NET 4.6 application to reference it. I followed the information in this SO question to create a NuGet package, create a local source and then try to add it to my ASP.NET 4.6 project but it fails with the error:
Unable to resolve dependency 'NETStandard.Library'. Source(s) used: 'nuget.org', 'AspNetRc2Source', 'MyGet', 'Local', 'Microsoft and .NET', 'Microsoft Visual Studio Offline Packages'. 0
My head is spinning with dependencies and frameworks.
Anyway here is the project.json file in my .NET Core class library - you can see it is only targeting .NET 4.6 which is what my ASP.NET app is targeting too.
I feel I need some serious .NET 101 course to get my head around how all these dependencies fit together, but if anyone is kind enough to point me in the right direction it would be appreciated.
Remove the reference to NETStandard.Library and it should work. You don't need that if you're only targeting .NET 4.5+.
I work on an OWIN middleware library that can be consumed from both .NET 4.5+ and .NET Core. I target both using a project.json that looks like this:
"dependencies": {
"Stormpath.Owin.Abstractions": {
"target": "project",
"version": "1.0.0"
}
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Collections": "4.0.0.0"
}
},
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
}
}
}
Since only .NET Core (netstandardX.Y) requires .NETStandard.Library, it's listed as a dependency for only that target, not as a general dependency. The project will happily build for .NET 4.5 and dotnet pack produces libraries that install perfectly on both platforms.

Resources