在ASP.NET 5(Core)中添加对类库项目的引用的问题

时间:2021-08-14 11:59:15

Hi firstly i know vaguely similar questions have been asked before, but they are outdated now, I am using Visual Studio 2015 rtm and ASP.NET 5 beta 6.

嗨首先我知道之前已经提到了类似的问题,但它们现在已经过时了,我使用的是Visual Studio 2015 rtm和ASP.NET 5 beta 6。

I'm trying to add a reference to a normal (i.e. not vnext) class library project to my vnext web application. If I follow these steps:

我正在尝试向我的vnext Web应用程序添加对普通(即非vnext)类库项目的引用。如果我按照以下步骤操作:

  1. Create a new web app project

    创建一个新的Web应用程序项目

  2. Remove the "dnxcore50" framework from project.json

    从project.json中删除“dnxcore50”框架

  3. Add a new project for a normal class library

    为普通类库添加新项目

  4. Manually move the class library project into the /src folder (otherwise I get error "The dependency MyClassLibrary1 >= 1.0.0-* could not be resolved.")

    手动将类库项目移动到/ src文件夹中(否则我收到错误“依赖MyClassLibrary1> = 1.0.0- *无法解析。”)

  5. Add a reference to this class library

    添加对此类库的引用

Now it builds OK, but if I try and add "using MyClassLibrary1" it says MyClassLibrary1 doesn't exist in current context.

现在它构建正常,但如果我尝试添加“使用MyClassLibrary1”,它说MyClassLibrary1在当前上下文中不存在。

If I then change the class library to target .NET 4 Client profile (by default it was 4.6) it does work correctly, however .NET 4 full or 4.5 does not work. I need it to be 4.5 or higher as I need to reference various packages that require this. Ideally everything would just target 4.6.

如果我然后将类库更改为目标.NET 4客户端配置文件(默认情况下为4.6)它可以正常工作,但.NET 4完全或4.5不起作用。我需要它是4.5或更高,因为我需要引用需要它的各种包。理想情况下,一切都只是针对4.6。

This is my project.json file:

这是我的project.json文件:

{
  "webroot": "wwwroot",
  "userSecretsId": "aspnet5-WebApplication2-6767111e-0eba-42a4-9d68-4b6c20767518",
  "version": "1.0.0-*",

  "dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta6",
    "EntityFramework.Commands": "7.0.0-beta6",
    "Microsoft.AspNet.Mvc": "6.0.0-beta6",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta6",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Facebook": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Google": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.MicrosoftAccount": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Twitter": "1.0.0-beta6",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta6",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta6",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta6",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.Abstractions": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta6",
    "Microsoft.Framework.Logging": "1.0.0-beta6",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta6",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta6"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --config hosting.ini",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
        "MyClassLibrary1": "1.0.0-*"
      }

    }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }
}

And my global.json file:

我的global.json文件:

{
  "projects": [
    "src",
    "test",
    "wrap"
  ],
  "sdk": {
    "version": "1.0.0-beta6"
  }
}

6 个解决方案

#1


23  

This is how I did it using beta6 (UPDATE: It's still valid for the RC1 UPDATE 1).

这就是我使用beta6的方式(UPDATE:它对RC1 UPDATE 1仍然有效)。

  1. Remove frameworks => dnxcore from your project.json (you can't target it anyway using full .net class libraries)
  2. 从project.json中删除frameworks => dnxcore(无论如何都不能使用完整的.net类库来定位它)
  3. In your target project right click on References => Add Reference
  4. 在目标项目中,右键单击References => Add Reference
  5. Navigate and select reference dll you want to add.
  6. 导航并选择要添加的参考dll。

This will add a reference entry to your project.json file. Behind the scenes the dll is copied over to /lib directory in your solution and a "wrapper project" with only a project.json file is created in /wrap folder. The wrapper project is documented here (not well enough though): https://github.com/aspnet/Home/wiki/Project.json-file#bin-syntax-wrapping-a-dll

这将为project.json文件添加一个引用条目。在幕后,将dll复制到解决方案中的/ lib目录中,并在/ wrap文件夹中创建仅包含project.json文件的“包装器项目”。这里记录了包装器项目(尽管不够好):https://github.com/aspnet/Home/wiki/Project.json-file#bin-syntax-wrapping-a-dll

That's it! I've just tested this scenario. Hope this helps.

而已!我刚刚测试了这个场景。希望这可以帮助。

#2


15  

If anyone else is struggling with this particular error, the key is to add the reference in by "browsing" to the actual file when adding the reference, and not using the "project" tab.

如果其他人正在努力解决这个特定的错误,关键是在添加引用时通过“浏览”添加引用到实际文件,而不是使用“项目”选项卡。

This doesn't seem to store the path, but adds the reference to the project.json as per normal. Obviously a bug (beta7) at the time of writing this.

这似乎不存储路径,但按正常方式添加对project.json的引用。在写这篇文章时,显然是一个错误(beta7)。

#3


2  

Here is how I did it in ASPT.NET 5 RC 1 Update 1:

以下是我在ASPT.NET 5 RC 1 Update 1中的做法:

  1. Add the project reference
  2. 添加项目引用
  3. Open project.json, inside "frameworks": node, delete "dnxcore50": { } line (JSON does not allow comment). When you save the json file, the DNX Core 5 reference should be removed immediately.

    打开project.json,在“frameworks”里面:node,删除“dnxcore50”:{}行(JSON不允许注释)。保存json文件时,应立即删除DNX Core 5引用。

  4. Build the project. It should build successfully.

    建立项目。它应该成功构建。

  5. Put "dnxcore50": { } back. It does not bring DNX 5 Core back.

    把“dnxcore50”:{}放回去。它没有带回DNX 5 Core。

#4


1  

To add normal .NET project in version greater then 4.5.1 just remove dnxcore and rename dnx451 to dnx461

要在版本大于4.5.1的版本中添加正常的.NET项目,只需删除dnxcore并将dnx451重命名为dnx461

#5


1  

These answers really helped me figure this out. In my case cleaning things up in the solution's "wrap" folder resolved the build errors. I had renamed some class libraries and the old ones were still in there. Removing the old projects and updating the project.json files in the existing projects did the trick. Make sure the dependencies in the project.json files reverence the correct projects.

这些答案真的帮助我解决了这个问题。在我的情况下,解决方案的“wrap”文件夹中的清理解决了构建错误。我已经重命名了一些类库,旧的库仍在那里。删除旧项目并更新现有项目中的project.json文件就可以了。确保project.json文件中的依赖项尊重正确的项目。

Here's an example:

这是一个例子:

  "dependencies": {
    "DomainRepository": "1.0.0-*",
    "Domain": "1.0.0-*",
    "DomainContract": "1.0.0-*"
  }

#6


0  

This is a currently open bug. Here's the link so you can see when it closes: https://github.com/aspnet/Tooling/issues/245

这是一个当前开放的bug。这是链接,因此您可以看到它何时关闭:https://github.com/aspnet/Tooling/issues/245

#1


23  

This is how I did it using beta6 (UPDATE: It's still valid for the RC1 UPDATE 1).

这就是我使用beta6的方式(UPDATE:它对RC1 UPDATE 1仍然有效)。

  1. Remove frameworks => dnxcore from your project.json (you can't target it anyway using full .net class libraries)
  2. 从project.json中删除frameworks => dnxcore(无论如何都不能使用完整的.net类库来定位它)
  3. In your target project right click on References => Add Reference
  4. 在目标项目中,右键单击References => Add Reference
  5. Navigate and select reference dll you want to add.
  6. 导航并选择要添加的参考dll。

This will add a reference entry to your project.json file. Behind the scenes the dll is copied over to /lib directory in your solution and a "wrapper project" with only a project.json file is created in /wrap folder. The wrapper project is documented here (not well enough though): https://github.com/aspnet/Home/wiki/Project.json-file#bin-syntax-wrapping-a-dll

这将为project.json文件添加一个引用条目。在幕后,将dll复制到解决方案中的/ lib目录中,并在/ wrap文件夹中创建仅包含project.json文件的“包装器项目”。这里记录了包装器项目(尽管不够好):https://github.com/aspnet/Home/wiki/Project.json-file#bin-syntax-wrapping-a-dll

That's it! I've just tested this scenario. Hope this helps.

而已!我刚刚测试了这个场景。希望这可以帮助。

#2


15  

If anyone else is struggling with this particular error, the key is to add the reference in by "browsing" to the actual file when adding the reference, and not using the "project" tab.

如果其他人正在努力解决这个特定的错误,关键是在添加引用时通过“浏览”添加引用到实际文件,而不是使用“项目”选项卡。

This doesn't seem to store the path, but adds the reference to the project.json as per normal. Obviously a bug (beta7) at the time of writing this.

这似乎不存储路径,但按正常方式添加对project.json的引用。在写这篇文章时,显然是一个错误(beta7)。

#3


2  

Here is how I did it in ASPT.NET 5 RC 1 Update 1:

以下是我在ASPT.NET 5 RC 1 Update 1中的做法:

  1. Add the project reference
  2. 添加项目引用
  3. Open project.json, inside "frameworks": node, delete "dnxcore50": { } line (JSON does not allow comment). When you save the json file, the DNX Core 5 reference should be removed immediately.

    打开project.json,在“frameworks”里面:node,删除“dnxcore50”:{}行(JSON不允许注释)。保存json文件时,应立即删除DNX Core 5引用。

  4. Build the project. It should build successfully.

    建立项目。它应该成功构建。

  5. Put "dnxcore50": { } back. It does not bring DNX 5 Core back.

    把“dnxcore50”:{}放回去。它没有带回DNX 5 Core。

#4


1  

To add normal .NET project in version greater then 4.5.1 just remove dnxcore and rename dnx451 to dnx461

要在版本大于4.5.1的版本中添加正常的.NET项目,只需删除dnxcore并将dnx451重命名为dnx461

#5


1  

These answers really helped me figure this out. In my case cleaning things up in the solution's "wrap" folder resolved the build errors. I had renamed some class libraries and the old ones were still in there. Removing the old projects and updating the project.json files in the existing projects did the trick. Make sure the dependencies in the project.json files reverence the correct projects.

这些答案真的帮助我解决了这个问题。在我的情况下,解决方案的“wrap”文件夹中的清理解决了构建错误。我已经重命名了一些类库,旧的库仍在那里。删除旧项目并更新现有项目中的project.json文件就可以了。确保project.json文件中的依赖项尊重正确的项目。

Here's an example:

这是一个例子:

  "dependencies": {
    "DomainRepository": "1.0.0-*",
    "Domain": "1.0.0-*",
    "DomainContract": "1.0.0-*"
  }

#6


0  

This is a currently open bug. Here's the link so you can see when it closes: https://github.com/aspnet/Tooling/issues/245

这是一个当前开放的bug。这是链接,因此您可以看到它何时关闭:https://github.com/aspnet/Tooling/issues/245