Why does adding a dependency in my Web API (ASP.NE

2020-06-30 04:43发布

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?

1条回答
Evening l夕情丶
2楼-- · 2020-06-30 05:11

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.

查看更多
登录 后发表回答