Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does adding a dependency in my Web API (ASP.NET v5) project not work fully?

I'm using Visual Studio 2015 CTP 6 on Windows 8.1.

I'm trying to write a Web API using ASP.NET v5, with its new project file format. I've added a reference to Noda Time v1.3.0 to my project.json file, and the editor in Visual Studio picks it up, but the build process fails.

Repro recipe, right from scratch:

  • Open VS 2015 CTP 6
  • Create new project in a new solution:
    • Select ASP.NET Web Application project template
    • Select "ASP.NET 5 Preview Web API" in the template dialog
  • Build the project, just to confirm everything is correct
  • Open project.json, and in the (badly formatted) "dependencies" section, add an extra line at the start (to avoid having to add a comma to another line):

    "NodaTime": "1.3.0", 
  • Open Controllers\ValuesController.cs
  • Edit the parameterless Get() method so that the body is:

    return DateTimeZoneProviders.Tzdb.Ids; 
    • DateTimeZoneProviders will have red squiggles, which is reasonable - we don't have a using directive yet.
  • Put the cursor in DateTimeZoneProviders and hit Ctrl+. - you should be offered "using NodaTime;" as a potential fix... so Intellisense (and thus Roslyn) definitely knows about the dependency.
  • Accept the fix. The squiggles will go away - all is well, right?
  • Try to build the solution: you should get two errors, basically indicating that the dependency hasn't been resolved.

In Explorer, if you look in the BugDemo solution directory, you'll find an artifacts\obj\BugDemo\Debug\ProjectRawReferences directory containing "ASP.NET Core 5.0" and "ASP.NET 5.0" directories, both of which have a lot of DLLs in... but not Noda Time.

Right-clicking on the project and selecting "Restore packages" doesn't fix this.

When I build the same project using Project K, a kpm restore does pick up Noda Time, and if you add a section to project.json as below, then k web works and visiting http://localhost:5001/api/values will show you all the TZDB time zone IDs:

"commands": {   "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001" }, 

So, what am I doing wrong? Or is it just a bug?

like image 862
Jon Skeet Avatar asked Mar 05 '15 20:03

Jon Skeet


People also ask

What is dependency injection in Web API?

Dependency Injection (often called just DI) is a software design pattern that helps us create loosely coupled applications. It is an implementation of the Inversion of Control (IoC) principle, and Dependency Inversion Principle (D in SOLID).

What is the difference between .NET 5 and .NET 6?

. NET 6.0 has more advantages than working with the . NET 5.0 as . NET 6.0 has more improvement on the performance which also includes some major advantage as it has intelligent code editing and also it is called the fastest full-stack web framework.


1 Answers

When you build, check out the "Project" column - it notes that the build that is failing is "ASP.NET Core 5.0" (not "ASP.NET 5.0"). In the upper left dropdown of the code editor, you can choose different views - if you select the "ASP.NET Core 5.0" one, you'll see that the NodaTime namespace is undefined.

It looks like the new ASP.NET project templates are creating multi-targeted apps, both aspnet50 and aspnetcore50.

ASP.NET 5.0 is (currently) based on .NET 4.5.x, so the NodaTime portable (net4) satisfies that platform. ASP.NET Core 5.0 is based on the new CoreClr (aspnetcore50), and there's no binaries in the NodaTime library that support it.

To resolve, you can just drop support for CoreClr in your application by removing the "aspnetcore50" entry in project.json under "frameworks":

"frameworks": {     "aspnet50": { }     //"aspnetcore50": { } }, 

Now your app should build just targeting ASP.NET 5.0 and not the CoreClr.

like image 101
Stephen Cleary Avatar answered Oct 20 '22 08:10

Stephen Cleary