How to reference an ASP.NET 5 project from another project?

泄露秘密 提交于 2019-12-29 08:47:13

问题


I have a Web Application project created from ASP.NET 5 Preview Templates in VS2015. It is running on dnx 1.0.0-beta7, but I'm using full .NET framework rather than .NET Core.

Within the same solution I have a NUnit class library project, but when trying to add the web project as a reference here, Visual Studio says "A reference to '...' could not be added."

Is there a workaround for this? Or what is the preferred way of unit testing ASP.NET vNext applications?


回答1:


The easiest way is to make sure that your ASP.NET 5 web project and the test project share a common parent directory. For example:

Solution
|-- Web
|   |-- project.json
|-- Tests
|   |-- project.json

That way Roslyn will be able to resolve the symbols across different subdirectories.

Of course, you'll also need to add a dependency from the Tests project to the Web project in the Tests\project.json file:

{
    "webroot": "wwwroot",
    "version": "1.0.0-*",

    "dependencies": {
        "Web": "1.0.0-*"
    },

    "frameworks": {
        "dnx451": { },
        "dnxcore50": { }
    }
}

If they can't have common parent directory, you can always add a global.json file in the root directory with the list of subdirectories that contain source code.

Solution
|-- global.json
|-- Web
|   |-- project.json
|-- Tests
|   |-- project.json

where global.json contains:

{
    "sources": ["Web", "Tests"]
}

Using "Add Reference" in Visual Studio 2015

If when you reference a class library project from an ASP.NET 5 Web project using the Add Reference dialog in Visual Studio 2015 you get this error:

A reference to <ProjectName> could not be added.

It means that the project you're trying to reference isn't a DNX project but rather a normal Class Library project. You need to make sure that the class library is an ASP.NET Class Library (Package) project in order to reference it.




回答2:


My experience was slightly different so I wanted to explain a 'work-around' i found.

Specifically when I try to add a "Project Reference" (aka from one project to another project in the same solution), I get the "A reference to '...' could not be added" error.

What I found is that if you add reference, and instead of adding a "project reference" but instead browse to and add the DLL (from the bin folder of the project being referenced) it correctly creates the necessary folder in the "wrap" directory.

You can then remove the reference to the DLL, and go back and add the "project reference" and it works!

We have a collection of "shared" libraries that we do not want to copy/move to the same folder structure of our primary application folders, so this works better for us.



来源:https://stackoverflow.com/questions/32886853/how-to-reference-an-asp-net-5-project-from-another-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!