这几天无疑我们已经让.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/
大家晚安。
扫描二维码关注我的公众号,共同学习,共同进步!