.NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2
ASP.NET Core RC2 终于发布了( Announcing ASP.NET Core RC2 )。为了庆祝这次发布,我们将运行在 Ubuntu 服务器上的示例站点 about.cnblogs.com 升级到了 ASP.NET Core RC2 ,在这篇博文中分享一下我们在升级过程中遇到的问题。
一、安装.NET Core SDK
删除旧版 dotnet cli:
rm -rf /usr/share/dotnet
安装 .NET Core SDK:
curl -sSL https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/dotnet-install.sh | bash /dev/stdin --version 1.0.0-preview1-002702 --install-dir ~/dotnet
sudo ln -s ~/dotnet/dotnet /usr/local/bin
二、运行 dotnet restore 命令
遇到2个错误:
1) Unable to resolve 'Microsoft.AspNetCore.IISPlatformHandler (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'
解决方法:在 project.json 中将 Microsoft.AspNetCore.IISPlatformHandler 改为 Microsoft.AspNetCore.Server.IISIntegration 。
2) Package Newtonsoft.Json 7.0.1 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0)
解决方法:在 project.json 中将
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-*"
}
改为
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools":{
"version":"1.0.0-*",
"imports": "portable-net45+win8+dnxcore50"
}
}
详见博问:http://q.cnblogs.com/q/82384/
三、运行 dotnet run 命令
遇到了不少错误:
1)
The type or namespace name 'IApplicationEnvironment' could not be found (are you missing a using directive or an assembly reference?)
解决方法:将 Startup.cs 中的 IApplicationEnvironment 改为 IHostingEnvironment,详见 http://q.cnblogs.com/q/82388/ 。
2)
'IWebHostBuilder' does not contain a definition for 'UseDefaultHostingConfiguration' and no extension method 'UseDefaultHostingConfiguration' accepting a first argument of type 'IWebHostBuilder' could be found (are you missing a using directive or an assembly reference?)
解决方法:在 Program.cs 中删除 .UseDefaultHostingConfiguration(args)
3)
'IWebHostBuilder' does not contain a definition for 'UseIISPlatformHandlerUrl' and no extension method 'UseIISPlatformHandlerUrl' accepting a first argument of type 'IWebHostBuilder' could be found
解决方法:在 Program.cs 中将 .UseIISPlatformHandlerUrl() 改为 UseIISIntegration()
4)
'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found
解决方法:在 project.json 的 dependencies 中添加配置: "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*" ,详见 http://q.cnblogs.com/q/82391/
5)
'IConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no extension method 'AddJsonFile' accepting a first argument of type 'IConfigurationBuilder' could be found
解决方法:在 project.json 的 dependencies 中添加配置: "Microsoft.Extensions.Configuration.Json": "1.0.0-*" ,详见 http://q.cnblogs.com/q/82395/
6)
The type or namespace name 'IRuntimeEnvironment' does not exist in the namespace 'Microsoft.Extensions.PlatformAbstractions'
解决方法:在 _Layout.cshtml 中删除 @inject Microsoft.Extensions.PlatformAbstractions.IRuntimeEnvironment env ,添加命名空间 @using Microsoft.Extensions.PlatformAbstractions 与代码 @{ var env = PlatformServices.Default.Runtime; } 。
四、代码改进
在 Program.cs 中将 .UseServer("Microsoft.AspNetCore.Server.Kestrel") 改为 .UseKestrel() 。
五、project.json、Program.cs、Startup.cs中的完整代码
1)project.json
{
"compilationOptions": {
"preserveCompilationContext": true,
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*",
"Microsoft.AspNetCore.HttpOverrides": "1.0.0-*",
"Microsoft.AspNetCore.Mvc": "1.0.0-*",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-*"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"portable-net45+wp80+win8+wpa81+dnxcore50",
"portable-net45+win8+wp8+wpa81",
"portable-net45+win8+wp8"
]
}
}, "tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools":{
"version": "1.0.0-*",
"imports": "portable-net45+win8+dnxcore50"
}
}
}
2)Program.cs
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder; namespace CNBlogs.AboutUs.Web
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:8001")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
}
3)Startup.cs
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using CNBlogs.AboutUs.Data;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using Microsoft.Extensions.Logging;
using CNBlogs.AboutUs.Application;
using Microsoft.AspNetCore.Hosting; namespace CNBlogs.AboutUs.Web
{
public class Startup
{
public Startup(IHostingEnvironment hostingEnv)
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(hostingEnv.ContentRootPath)
.AddJsonFile("config.json", false);
Configuration = builder.Build();
} public IConfiguration Configuration { get; set; } public void Configure(IApplicationBuilder app,
ILoggerFactory loggerFactory,
IHostingEnvironment env)
{
if(env.IsDevelopment())
{
loggerFactory.AddConsole(LogLevel.Debug);
}
else
{
loggerFactory.AddConsole(LogLevel.Error);
} app.UseDeveloperExceptionPage();
app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
app.UseRuntimeInfoPage(); if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
} public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddEntityFrameworkSqlServer()
.AddDbContext<EfDbContext>(options =>
{
options.UseSqlServer(Configuration["data:ConnectionString"]);
}); services.AddTransient<ITabNavRepository, TabNavRepository>();
services.AddTransient<ITabNavService, TabNavService>();
}
}
}
.NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2的更多相关文章
- .NET跨平台之旅:将示例站点升级至ASP.NET Core 1.0
北京时间6月28日凌晨,微软发布了 .NET Core 1.0,详见新闻 .NET Core 1.0 正式发布了 ,ASP.NET Core 1.0 也随之一起发布了. 紧跟这次发布,我们将跑在 Li ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- .NET跨平台之旅:将示例站点从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0
终于将“.NET跨平台之旅”的示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 ,经历了不少周折,在这篇博文中记录一下. 从 AS ...
- .NET跨平台之旅:将示例站点从ASP.NET 5 Beta5升级至Beta7
9月2日,微软发布了ASP.NET 5 Beta7(详见Announcing Availability of ASP.NET 5 Beta7).其中最大的亮点是dnx已经可以完全基于CoreCLR运行 ...
- .NET跨平台之旅:将示例站点升级至 .NET Core 1.1 Preview 1
今天微软发布了 .NET Core 1.1 Preview 1(详见 Announcing .NET Core 1.1 Preview 1 ),紧跟 .NET Core 前进的步伐,我们将示例站点 h ...
- .NET跨平台之旅:将示例站点从ASP.NET 5 Beta7升级至RC1
今天,我们将示例站点(about.cnblogs.com,服务器操作系统是Ubuntu)从ASP.NET 5 Beta7升级到了RC1,在升级过程中只遇到了一个问题. 在运行 dnvm upgrade ...
- .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序
自从用 dotnet run 成功运行第一个 "Hello world" .NET Core 应用程序后,一直有个好奇心:dotnet run 究竟是如何运行一个 .NET Cor ...
- .NET跨平台之旅:博问站点迁移至ASP.NET Core on Linux并发布上线
Powered by ASP.NET Core on Linux! 我们全站的 .NET Core 迁移工作如火如荼,这是我们今年上半年的重要工作. 今天我们终于完成了博问产品(q.cnblogs.c ...
- .NET跨平台之旅:在Linux上以本地机器码(native)运行ASP.NET Core站点
在将“.NET跨平台之旅”示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 (博文链接)之后,我们有一个难以抗拒的冲动 —— 体验 ...
随机推荐
- ASP.NET Core 中文文档 第三章 原理(10)依赖注入
原文:Dependency Injection 作者:Steve Smith 翻译:刘浩杨 校对:许登洋(Seay).高嵩 ASP.NET Core 的底层设计支持和使用依赖注入.ASP.NET Co ...
- Zip 压缩和解压技术在 HTML5 中的应用
JSZip 是一款可以创建.读取.修改 .zip 文件的 javaScript 工具.在 web 应用中,免不了需要从 web 服务器中获取资源,如果可以将所有的资源都合并到一个 .zip 文件中,这 ...
- Create an offline installation of Visual Studio 2017 RC
Create an offline installation of Visual Studio 2017 RC 2016年12月7日 ...
- C++_系列自学课程_第_12_课_结构体
#include <iostream> #include <string> using namespace std; struct CDAccount { double bal ...
- 数据结构:二叉树 基于list实现(python版)
基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
- jQuery实现DOM加载方法源码分析
传统的判断dom加载的方法 使用 dom0级 onload事件来进行触发所有浏览器都支持在最初是很流行的写法 我们都熟悉这种写法: window.onload=function(){ ... } 但 ...
- iOS -- 轮播图
UIScrollView + 多张 ImageView 实现轮播 实现原理: 将所有图片的名字储存在数组 imageAry 中,imageAry 的元素个数为 num,在 scrollView 上添加 ...
- 阶段一:AsyncTask的三个属性值和四个步骤
“阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 最近学到用AsyncTask来处理有关网络的操作.虽然代码看上去不是很复杂,但仍有很多地方有疑惑.所以研读了一下A ...
- android基于口令加密快速搞懂(一)
import java.util.Random; import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypt ...