构建一个 ASP.NET 核心项目以引用 .Net 标准库

Structuring an ASP.NET Core project to reference .Net Standard libraries

我无法理解 ASP.NET Core Web API 和 .NET Standard PCLs.

的框架依赖性

我正在创建一个将用作 API 网关的新网站 API(微服务),我在 VS2015(更新 3,最新的 .net 核心工具)中创建了一个新项目,并且它编译并运行良好。

然后我添加了一些 .NET Standard PCL 项目用于分离 POCO、存储库层等。这些都很好地相互引用,但是当我尝试从我的 [=40= 添加引用时] Core Web API 我不能使用我的 PCL 中定义的任何 类。

经过一些研究似乎归结为我在项目中针对不同的框架这一事实,project.json 的 Web API 的框架部分显示:

"frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "portable-net45+win8"
    ]
  }
},

对于第 PCL 个,它最初显示为:

frameworks": {
  "netstandard1.6": {
    "imports": "dnxcore50"
  }
}

很明显这就是不兼容的地方。

为了纠正这个问题,我试图让 .Net 核心项目引用 .Net 标准框架,这样它就可以使用 PCLs,但是我尝试的一切都无法让我引用这些项目。

我让这个工作的唯一方法是做相反的事情并使 PCL 库引用 netcoreapp1.0,这感觉很不对。

我的完整 .Net 标准 project.json 现在是:

{
  "version": "1.0.0",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    },
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  }
}

我完整的 .Net Core Web Api 项目读取

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Company.Product.Microservices.Gateway.Business": "1.0.0",
    "Company.Product.Microservices.Gateway.DomainObjects": "1.0.0" 
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

正如我之前所说,此处引用的框架似乎不正确。我假设基于 netcoreapp 框架的项目可以直接引用 .NET Standard 项目编译的项目,而 PCL 本身不必知道任何关于 netcoreapp 的信息,毕竟它是一个 PCL。

构建这样的项目的正确方法是什么,感觉这应该是任何 ASP.NET 核心项目的 "default" 设置,但是到目前为止,设置和设置并不简单我找不到与这种常见情况相关的任何文档。也许那是因为我对 Core 和 Standard 的理解不正确并且我没有做对,或者也许我的配置是错误的。请让我知道我应该如何正确地实现这样的结构。

我有一个 ASP.NET 核心应用程序,它分为许多项目。我的网络应用程序是唯一一个 netcoreapp1.1。其余都是netstandard1.6.

仔细查看我的 project.json 文件(特别是框架和依赖项部分)以与您的文件进行比较。你会看到细微的差别。我的解决方案构建没有问题,智能感知也能正常工作。

我的网络应用程序 project.json 是这样的(它相当长,但我把所有内容都留在那里以防您需要查看其他内容:

{
  "version": "2.0.1.0",

  "dependencies": {
    "DT.Common": "2.*",
    "DT.Configuration": "2.*",
    "DT.Services": "2.*",
    "DT.Web.ViewModels": "2.*",
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
    "Microsoft.AspNetCore.Authentication": "1.1.0",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.1.0",
    "Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.1.0-preview4-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.1.0-preview4-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel.Https": "1.1.0",
    "Microsoft.AspNetCore.Session": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.Extensions.Caching.SqlServer": "1.0.0",
    "Microsoft.Extensions.Configuration.Abstractions": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.Graph": "1.1.1",
    "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.6"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.1.0-preview4-final",
      "imports": "portable-net45+win8+dotnet5.6"
    },
    "Microsoft.Extensions.Caching.SqlConfig.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0",
          "type": "platform"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "views/**/*.cshtml",
      "appsettings.json",
      "appsettings.*.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "gulp buildprod" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

我的服务层project.json是这样的:

{
  "version": "2.0.1.0",

  "dependencies": {
    "DT.Common": "2.*",
    "DT.Configuration": "2.*",
    "DT.Data": "2.*",
    "DT.Models": "2.*" ,
    "DT.Web.ViewModels": "2.*",
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.1.0",
    "Microsoft.AspNetCore.Http.Abstractions": "1.1.0",
    "Microsoft.Extensions.Caching.Abstractions": "1.0.0",
    "Microsoft.Extensions.Options": "1.1.0",
    "Microsoft.Graph": "1.1.1",
    "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.6"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ],
      "dependencies": {
        "NETStandard.Library": "1.6.1"
      }
    }
  }
}

我的数据层project.json是这样的:

{
  "version": "2.0.1.0",

  "dependencies": {
    "DT.Configuration": "2.*",
    "DT.Data.Domain": "2.*",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.Extensions.Configuration.Abstractions": "1.1.0",
    "Microsoft.Extensions.DependencyInjection.Abstractions": "1.1.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dotnet5.6",
      "dependencies": {
        "NETStandard.Library": "1.6.1"
      }
    }
  }
}

在 M.Ob 的 project.json 的进一步调查和帮助下,我发现我的项目实际上构建正确,问题来自 ReSharper(10.0.2,我应该升级).

为此解决方案禁用 resharper 现在可以让我的项目构建和 Intellisense 为解决方案正常工作。