构建ASP.NET Core项目以引用.Net标准库

时间:2017-01-07 18:06:40

标签: asp.net-core .net-core asp.net-core-webapi .net-standard

我无法解决ASP.NET Core Web API和.NET Standard PCL的框架依赖关系。

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

然后我添加了一些.NET标准PCL项目用于分离出POCO,Repository层等。这些项目都互相引用,但是当我尝试从ASP.NET Core Web API添加引用时,我无法使用任何在我的PCL中定义的类。

经过一些研究似乎可以归结为我在项目中针对不同的框架,用于Web API的project.json的框架部分读取:

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

对于最初阅读的PCL:

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

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

为了解决这个问题,我尝试使.Net核心项目引用.Net标准框架,以便它可以使用PCL,但是我尝试的所有内容都未能允许我参考这些项目。

我让这个工作的唯一方法就是做反向并使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 Core项目的“默认”设置,但到目前为止它设置起来并不简单,我找不到任何文档与这种肯定常见的情况有关。也许那是因为我对Core和Standard的理解是不正确的,我做得不对,或者我的配置可能是错的。请让我知道如何正确地实现这样的结构。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

我有一个分为许多项目的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"
      }
    }
  }
}