1、分别修改类库和Web的项目文件,升级到.Net Core 2.0和.Net Standard 2.0,其中包括Nuget上的一些组件,或者直接通过右键项目->属性->目标框架进行修改,同时修改Program.cs和Startup.cs文件为2.0默认的方式,完成后他们的样子是酱紫的,是不是很简洁明了。
类库的csproj项目文件:
<Project Sdk=> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> </Project>
Web的csproj项目文件
<Project Sdk=>
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=Version=/>
<PackageReference Include=Version=/>
<PackageReference Include=Version=/>
<PackageReference Include=Version=/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include=Version=/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include=/>
<ProjectReference Include=/>
</ItemGroup>
</Project>
简洁的Program.cs
using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace Light.Web { public class Program { Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); } }
下面是我的Startup文件的相关配置项
using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Light.Web.DBContext; using Microsoft.EntityFrameworkCore; using Light.Web.Models; namespace Light.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<LightContext>(m => { m.UseSqlServer(Configuration.GetConnectionString()); }); services.AddSession(m => { m.IdleTimeout = TimeSpan.FromHours(1); }); services.AddMvc(m => { m.Filters.Add(typeof(LightActionFilter)); m.Filters.Add(new LightExceptionFilter()); }); var serviceProvider = services.BuildServiceProvider(); var dbContext = serviceProvider.GetService<LightContext>(); LightContextSeed.SeedAsync(dbContext).Wait(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } app.UseStaticFiles(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: , template: ); }); } } }
View Code微软官方的升级向导,供大家参考:https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/
2、使用cli命令发布项目,注意指定发布时的运行时标识
dotnet publish -r ubuntu.16.10-x64 -c Release -o ../Publish
四、使用Docker进行部署1、使用Winscp登陆到服务器上,并将发布之后的文件夹publish上传到服务器上
2、创建Dockerfile文件,并将其配置成以下内容
FROM microsoft/dotnet WORKDIR /app COPY ./publish . ENTRYPOINT [, ]
3、切换到Dockerfile所在的目录下,并执行构建命令,成功之后会有下图的提示
sudo docker build -t lightweb . //注意后边还有一个点呢
4、运行刚才构建成功,名字为lightweb的容器,直接绑定服务器的80端口
sudo docker run -p lightweb
运行成功之后是这样的(请暂时忽略Warning),表示该容器内的程序正在使用80端口监听着.......
5、试着用IP访问一下,不知道自己IP的请使用ifconfig命令进行查看
最后