dotnet core项目升级到 .net core 2.0
这几天无疑我们已经让.net core 2.0正式版发布的消息刷屏,这次发布整整提前了一个月的时间,关于具体的发布信息,可以去看善友大神的博客,.NET Core 2.0 正式发布信息汇总,废话不多说,已经迫不及待的想把自己的项目从1.0变成2.0了,还不知道会不会出现意想不到的坑,拭目以待。
我们需要将所有的类库和项目都要升级到.Net Core2.0,右键项目->属性->目标框架,选择.net core 2.0,如图所示:
以此类推将所有的项目都改成.net core 2.0版本,接着我们将web项目中所有的Microsoft.*的依赖项全部删除,直接Nuget万能的Microsoft.AspNetCore.All ,删除掉之前的引用后,变成酱紫,瞬间清爽许多,再也不用为Nuget什么包而发愁,一个All包含了所有。
Nuget包修改完后,下面修改的Program.cs文件,下面看下两个版本的对比:
相对于1.x版本,2.0版本的Program.cs文件和StartUp.cs文件也简化了许多,很明显的是,appsetting.json配置文件以及日志,都在这里看不到了,他们都被放到了WebHost.CreateDefaultBuilder中,看下这个函数的源码,
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
} config.AddEnvironmentVariables(); if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.UseConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
})
.ConfigureServices(services =>
{
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
}); return builder;
}
日志已经不再作为中间件使用,而是直接放到了host中,这意味着,它可以记录更底层的日志,你可以在WebHost中配置
Ok,到这里,我们的项目就升级完成了,快来测试下是否运行正常,
本篇内容就写这么多了。给出微软官方的升级向导地址:https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/
大家晚安。
扫描二维码关注我的公众号,共同学习,共同进步!
最新文章
- [LeetCode] 3Sum 三数之和
- VC++ 对话框程序响应键盘消息的处理方法的说明(非常重要)
- Makefile 中:= ?= += =的区别
- [转]jni数据类型映射、域描述符说明
- animation 的属性一共有 6 个值,详细介绍在此
- jQuery plugin: Autocomplete 参数及实例
- [ZZ] HDR the bungie way
- 经典SQL语句大全.doc
- Today I Cooked the Sun Yat-Sen University [2007-09-25 12:37:39]
- Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).
- PHP版本替换, phpinfo和php -v显示版本信息不一致
- virtualbox 安装centos系统,设置双网卡实现虚拟机上网及主宿互访
- kubernetes nginx ingress 使用记录
- ansible-playbook相关
- 排序算法Java实现(归并排序)
- Web Api HttpWebRequest 请求 Api 及 异常处理
- 戏说程序猿之cannot find the object
- 放下技术,是PM迈出的第一步
- 12、Grafan 4.3升级到Grafana 5.0
- python数据结构与算法学习自修第二天【时间复杂度与大O表示法】