Testing .Net Core Library with xUnit - .net-core

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

Related

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": {}
}

How to create a .NET Core library I can reference from a .NET Core App (Web API)

I'm working in Visual Studio 2015 Update 3 and .NET Core 1.0. I have a Web API project which is of type .NETCoreApp v1.0. When I add a .NET Core class library, it is of type .NETStandard v1.6. I can add this library to the Web API project as a reference, but it is not recognised when I try to add using statements.
If I create another project of type .NETCoreApp, I can reference it and use the classes without a problem.
How do I make use of a .NET Core class library from my .NET Core App?
Edit/Update:
This appears only to be an editor/Intellisense issue, because despite the editor warnings, the .NETCoreApp does build and run, calling into the class library.
I am running Resharper, which I see is blamed for similar problems with other types of projects: I have checked that I have the latest version and have cleared the Resharper cache and restarted VS2105.
This is a Resharper issue. At this time Resharper (v2016.1.2) does not support .NET Core 1.0.
There are 2 possible solutions:
Uninstall Resharper, and the Visual Studio native intellisense works.
Install the Resharper 2016.2 EAP (Early Access Program) version. I've done this and it's working. Obviously it comes with the caveats of any EAP/beta product.
Here is a link to the Jetbrains forum post where I was told .NET Core 1 was not yet supported and pointed to the EAP version.
Once you've built a library that targets netstandard1.X, you can either:
Produce a NuGet package with dotnet pack and host it locally or on NuGet. Then, install it in your netcoreapp project as any other dependency.
If your library and application are part of the same solution, make a local reference:
project.json
"dependencies":{
"MyLibrary.Core": {
"version": "1.0.0",
"target": "project"
}
}
target: project tells dotnet to look in the current solution for the dependency, instead of using your NuGet feeds. Again, this only works if you are developing the library and application in the same solution.

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.

What is wrong with my class library reference in an MVC 6 Web Api project?

Working in VS 2015 Community, I've created a Services class library project, targeted at .NET 4.6. Now, in my main Api project, all references fall under DNX 4.5.1 and DNX Core 5. When I add the reference to Services, I get the compiler error :
Dependency Services >= 1.0.0-* could not be
resolved Api C:\Development\Surveys\src\Api\project.json
What do I have to do to make my class library compatible with the Api project?
Both of your projects have to target DNX Core 5 if your main (Api) project supports it
You can't reference .net project that has higher version than source one. So both of your projects should target 4.6
The way you described the problem I'm not sure if Services is normal (let's call it old) Class Library or ASP.NET 5 Class Library. If it is old then your main project can not target core clr. Just remove this TFM from project.json.

Targeting Runtime for New MVC6 Project in VS 2015

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.

Resources