ASP.NET Core 1.0 (ASP.Net5, vNext... All the names) TFM troubles and Xunit

ASP.NET Core 1.0 (ASP.Net5, vNext... All the names) TFM troubles and Xunit

TFM's, why so many?

TFM stands for "Target Framework Moniker" (if you've not heard the phrase before). TFM's have been around in the .NET world for a while now, but have recently had more of the limelight shone on them thanks to ASP.NET Core 1.0 and the project.json package and dependencies configuration file.

ASP.NET Core 1.0 is currently at RC V1 Update 1. I personally would expect at this point that Microsoft would have gotten the major issues sorted out and be focused on sorting out the smaller niggles of the system. What they have done in open sourcing the whole thing is great for basically everyone; Microsoft, .NET Devs, non-.NET dev, Mono Dev's etc. However, they have yet to address what I think is a pretty big issue. The amount of TFM's they have introduced.

I created a new n-tier project to do some testing of ASP.NET Core 1.0, and spent way too much time coming to grips with the new TFM's which are automatically generated and added to your project.json file when creating different project types. I've been keeping notes of my troubles when working with the new RC technologies, and so far I've come across the following TFM's:

"dnx451", "dnxcore50", "net451", "dotnet5.4", "aspnet50", "aspnetcore50"

And I know there are more as I've seen them previously when I was testing out Beta 6-8 of ASP.NET5.  Some of these TFM's are meant to target the .NET 4.5.1 framework, some are meant to target ASP.NET 5/Core 1.0. But even so, one TFM which targets .NET 4.5.1 may act differently to another TFM which is supposed to target the same thing.

Example:

The "dotnet5.4"(.NET core tfm) TFM supports "Microsoft.Net.Http": "2.2.7-beta" but that doesn't contain the `HttpClient` class currently, whereas "net451"(.NET 4.5.1) supports "System.Net.Http": "4.0.1-beta-23516" which does contain HttpClient, but when you make a new Web Class Library, it comes with its project.json file loaded up with both of these TFM's. Which means you an add a dependency to both TFM's for their respective "*.Net.Http" assemblies, but one of them doesnt contain what most developers would probably consider a pretty important class (HttpClient). Meaning if you want to include this class currently you need to delete, from your project.json file, the entire "dotnet5.4" TFM, making your project only compatible with ASP.NET 4.5.1 + and not the far more modular and portable ASP.NET Core 1.0 TFM. There may even be an equivalent to HttpClient in Core 1.0, but I've not yet seen it. Also, it would likely mean including preprocessor directives which check for specific TFM's (something I had to do in Beta 7 when trying to use a Cryptography class in .NET 4.5.1, and a horrible Random Number Generator for .NET Core).

Thats enough about TFM's for now anyway. I just hope that Microsoft narrows it down to two TFM's by the full RTM release, hopefully something along the lines of "NetCore" and "Net451" to keep it simple.

XUnit test runner issues

I wanted to start my sandbox project off in a "best practices" cliche by doing TDD (which I love). But there's one issue, ASP.NET core 1.0 RC1 doesnt have out of the box unit testing support, so I am using XUnit instead. It's a really nice framework, and I have to say thanks to Brad Wilson and his team for making it compatible with the experimental .NET Core stuff so early. They've done a great job.

Getting XUnit added to a project through the project.json file is straightforward if you follow the advice on the XUnit site. But this is where it stop being straight forward. No matter how many cleans and builds and rebuilds I did of my new project, I could not get the Visual Studio test Runner to run any of the tests. However, after much fumbling and experimenting I did it, but it was a strange one.

I had to use the visual studio command prompt to navigate to my test Project folder and manually run the tests the first time through the dnx command i had added to my project.json file's "commands" section. As soon as I did this, my tests, existing and new started appearing in the test runner, and it's ran fine since then.

So this is just a bit of advice, if your tests aren't running inside VS, get into the console, use "cd ../YourProjectPath/Tests" (whatever you path is to your test folder) and the `dnx test` command. Hopefully the tests will run in the console and then appear in the test runner too.

Onam Chilwan

Software Engineer

8 年

I had some trouble with getting xunit runner running my tests via visual studio. Oddly, what I did was have the test explorer window pinned then hit build and my tests suddenly appeared and I was able to run them no problem. I do feel your pain on the TFMs very annoying. I can only gets very basic web API up and running targeting 451. I am going to be adding this to my git if you fancy a butchets

要查看或添加评论,请登录

Ben Millane的更多文章

社区洞察

其他会员也浏览了