首页 > 代码库 > Net Core 多项目开发(net core 类库项目使用)
Net Core 多项目开发(net core 类库项目使用)
在这之前一直是单个web项目支撑整个.NET Coer Web项目,但是我们都知道在.NET中其实我们一直都是多个项目结合使用的,但是当我们尝试给解决方案中添加Library项目之后,再在web项目中引用的时候一直会有异常,也不知道是nuget版本冲突还是本身类库的项目结构导致的问题。其实这一段时间的实验证明这两者都会有问题,这可能是目前微软需要解决的主要问题,主要是前者。因为本地执行dotnet restore命令的时候不成功的机会远远大于成功的机会,再就是本身想添加nuget的时候会有异常,可能网络环境主宰了这一切。再就是类库本身,可能微软的出发点想是做个通用功能的类库项目,所以在dot net web项目下需要修改类库的project.json文件,同时需要修改引用类库的dot net web项目的project.json文件,文件配置如下:
dot net web 项目的project.json文件结构如下:
{ "userSecretsId": "aspnet-NETCore.Web-dd329bc3-d2ee-4592-8ab6-a9eee6aa5b0a", "dependencies": { "Microsoft.AspNetCore.Authentication.Cookies": "1.1.0", "Microsoft.AspNetCore.Diagnostics": "1.1.0", "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0", "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0", "Microsoft.AspNetCore.Mvc": "1.1.0", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "Microsoft.AspNetCore.StaticFiles": "1.1.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0", "Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.Extensions.Configuration.Json": "1.1.0", "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0", "Microsoft.Extensions.Logging": "1.1.0", "Microsoft.Extensions.Logging.Console": "1.1.0", "Microsoft.Extensions.Logging.Debug": "1.1.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.0.0-preview1-final", "type": "build" }, "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": { "version": "1.0.0-preview1-final", "type": "build" }, "NetCore.IBLL": { "“version": "1.0.0-*", "target": "project" }, "NetCore.Utity": { "“version": "1.0.0-*", "target": "project" }, "NetCore.ViewModel": { "“version": "1.0.0-*", "target": "project" } }, "tools": { "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.AspNetCore.Server.IISIntegration.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview1-final", "imports": [ "portable-net45+win8+dnxcore50", "portable-net45+win8" ] }, "Microsoft.Extensions.SecretManager.Tools": { "version": "1.0.0-preview1-final", "imports": "portable-net45+win8+dnxcore50" }, "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.0.0-preview1-final", "imports": [ "portable-net45+win8+dnxcore50", "portable-net45+win8" ] } }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" } }, "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "gcServer": true }, "publishOptions": { "include": [ "wwwroot", "Views", "appsettings.json", "web.config" ] }, "runtimes": { "win10-x64": { }, "win81-x64": { }, "osx.10.10-x64": { }, "osx.10.11-x64": { }, "ubuntu.14.04-x64": { } }, "scripts": { "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
这个配置文件需要注意的点就是引用的类库文件配置项需要加入 "target": "project",例如:
"NetCore.IBLL": {
"“version": "1.0.0-*",
"target": "project"
}
如果没有添加 "target": "project",则最终的服务无法启动并且会抛出异常
再就是web项目本身的引用项
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
},
最后就是需要注意runtimes节点相关的信息
"runtimes": {
"win10-x64": { },
"win81-x64": { },
"osx.10.10-x64": { },
"osx.10.11-x64": { },
"ubuntu.14.04-x64": { }
},
我这里就不一一罗列所有的平台项了,有用到的可以到网上查查,如果平台越多则dotnet restore需要的时间则越长。
然后就是类库项目的project.json文件了
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable" }, "dependencies": { "NetCore.DI": "1.0.0-*", "NetCore.ViewModel": "1.0.0-*" }, "frameworks": { "netstandard1.6": { "dependencies": { "NETStandard.Library": "1.6.1" } } } }
这个节点的配置信息能少点,因为作为演示项目并没有添加过多的引用项,如果有必要,则引用项 "NetCore.DI": "1.0.0-*", "NetCore.ViewModel": "1.0.0-*"也需要加上"target": "project",则最终的配置文件应该是这个样子的
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable" }, "dependencies": { "NetCore.DI": { "“version": "1.0.0-*", "target": "project" }, "NetCore.ViewModel": { "“version": "1.0.0-*", "target": "project" } }, "frameworks": { "netstandard1.6": { "dependencies": { "NETStandard.Library": "1.6.1" } } } }
那么到此你的添加了类库项目的NET Core Web项目则能够正常运行。
Net Core 多项目开发(net core 类库项目使用)