From 4a62138886b5b490ab12d6f87680e9913c47d71b Mon Sep 17 00:00:00 2001 From: Manfred Date: Tue, 10 Dec 2019 05:21:37 +1300 Subject: [PATCH] Migration to .NET Core 3.1 LTS (#122) * Updated package WebEssentials.AspNetCore.OutputCaching * Updated package WilderMinds.MetaWeblog which now requires an async implementation * Fold private non-sync into async methods * Updated package WebMarkupMin.AspNetCore2 * Updated package WebEssentials.AspNetCore.PWA * Updated package LigerShark.WebOptimizer.Core to latest version that works on .NET Core 2 * Changed target framework to .NET Core App 2.2. Updated package Microsoft.VisualStudio.Web.BrowserLink * Mmigrated to .NET Core 3.1 * Updated readme.md to reflect change to .NET Core 3.1 --- README.md | 4 +- src/Miniblog.Core.csproj | 17 ++- src/Services/FileBlogService.cs | 2 +- src/Services/MetaWeblogService.cs | 214 +++++++++++++++++++----------- src/Startup.cs | 40 +++--- 5 files changed, 171 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 96d40acc..56212c27 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Blog engine for ASP.NET Core 2.0 +# Blog engine for ASP.NET Core 3.1 -A full-featured yet simple blog engine built on ASP.NET Core 2.0. +A full-featured yet simple blog engine built on ASP.NET Core 3.1. [![Build status](https://ci.appveyor.com/api/projects/status/lwjrlpvmhg50wwbs?svg=true)](https://ci.appveyor.com/project/madskristensen/miniblog-core) [![NuGet](https://img.shields.io/nuget/v/MadsKristensen.AspNetCore.Miniblog.svg)](https://nuget.org/packages/MadsKristensen.AspNetCore.Miniblog/) diff --git a/src/Miniblog.Core.csproj b/src/Miniblog.Core.csproj index e8d6e97e..dec3dc86 100644 --- a/src/Miniblog.Core.csproj +++ b/src/Miniblog.Core.csproj @@ -1,22 +1,21 @@ - netcoreapp2.1 + netcoreapp3.1 - - - + + - + - - + + - - + + diff --git a/src/Services/FileBlogService.cs b/src/Services/FileBlogService.cs index 1234dd0b..9b58a796 100644 --- a/src/Services/FileBlogService.cs +++ b/src/Services/FileBlogService.cs @@ -23,7 +23,7 @@ public class FileBlogService : IBlogService private readonly IHttpContextAccessor _contextAccessor; private readonly string _folder; - public FileBlogService(IHostingEnvironment env, IHttpContextAccessor contextAccessor) + public FileBlogService(IWebHostEnvironment env, IHttpContextAccessor contextAccessor) { _folder = Path.Combine(env.WebRootPath, POSTS); _contextAccessor = contextAccessor; diff --git a/src/Services/MetaWeblogService.cs b/src/Services/MetaWeblogService.cs index b829d629..61e1c19d 100644 --- a/src/Services/MetaWeblogService.cs +++ b/src/Services/MetaWeblogService.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Security.Claims; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; @@ -23,136 +24,160 @@ public MetaWeblogService(IBlogService blog, IConfiguration config, IHttpContextA _context = context; } - public string AddPost(string blogid, string username, string password, WilderMinds.MetaWeblog.Post post, bool publish) + public Task AddPostAsync(string blogid, string username, string password, Post post, bool publish) { ValidateUser(username, password); - var newPost = new Models.Post + return Task.Run(() => { - Title = post.title, - Slug = !string.IsNullOrWhiteSpace(post.wp_slug) ? post.wp_slug : Models.Post.CreateSlug(post.title), - Content = post.description, - IsPublished = publish, - Categories = post.categories - }; + var newPost = new Models.Post + { + Title = post.title, + Slug = !string.IsNullOrWhiteSpace(post.wp_slug) ? post.wp_slug : Models.Post.CreateSlug(post.title), + Content = post.description, + IsPublished = publish, + Categories = post.categories + }; - if (post.dateCreated != DateTime.MinValue) - { - newPost.PubDate = post.dateCreated; - } + if (post.dateCreated != DateTime.MinValue) + { + newPost.PubDate = post.dateCreated; + } - _blog.SavePost(newPost).GetAwaiter().GetResult(); + _blog.SavePost(newPost).GetAwaiter().GetResult(); - return newPost.ID; + return newPost.ID; + }); } - public bool DeletePost(string key, string postid, string username, string password, bool publish) + public Task DeletePostAsync(string key, string postid, string username, string password, bool publish) { ValidateUser(username, password); - var post = _blog.GetPostById(postid).GetAwaiter().GetResult(); - - if (post != null) + return Task.Run(() => { - _blog.DeletePost(post).GetAwaiter().GetResult(); - return true; - } + var post = _blog.GetPostById(postid).GetAwaiter().GetResult(); - return false; + if (post != null) + { + _blog.DeletePost(post).GetAwaiter().GetResult(); + return true; + } + + return false; + }); } - public bool EditPost(string postid, string username, string password, WilderMinds.MetaWeblog.Post post, bool publish) + public Task EditPostAsync(string postid, string username, string password, Post post, bool publish) { - ValidateUser(username, password); - - var existing = _blog.GetPostById(postid).GetAwaiter().GetResult(); - - if (existing != null) + return Task.Run(() => { - existing.Title = post.title; - existing.Slug = post.wp_slug; - existing.Content = post.description; - existing.IsPublished = publish; - existing.Categories = post.categories; + ValidateUser(username, password); - if (post.dateCreated != DateTime.MinValue) + var existing = _blog.GetPostById(postid).GetAwaiter().GetResult(); + + if (existing != null) { - existing.PubDate = post.dateCreated; - } + existing.Title = post.title; + existing.Slug = post.wp_slug; + existing.Content = post.description; + existing.IsPublished = publish; + existing.Categories = post.categories; - _blog.SavePost(existing).GetAwaiter().GetResult(); + if (post.dateCreated != DateTime.MinValue) + { + existing.PubDate = post.dateCreated; + } - return true; - } + _blog.SavePost(existing).GetAwaiter().GetResult(); + + return true; + } - return false; + return false; + }); } - public CategoryInfo[] GetCategories(string blogid, string username, string password) + public Task GetCategoriesAsync(string blogid, string username, string password) { - ValidateUser(username, password); - - return _blog.GetCategories().GetAwaiter().GetResult() - .Select(cat => - new CategoryInfo - { - categoryid = cat, - title = cat - }) - .ToArray(); + return Task.Run(() => + { + ValidateUser(username, password); + + return _blog.GetCategories().GetAwaiter().GetResult() + .Select(cat => + new CategoryInfo + { + categoryid = cat, + title = cat + }) + .ToArray(); + }); } - public WilderMinds.MetaWeblog.Post GetPost(string postid, string username, string password) + public Task GetPostAsync(string postid, string username, string password) { - ValidateUser(username, password); + return Task.Run(() => + { + ValidateUser(username, password); - var post = _blog.GetPostById(postid).GetAwaiter().GetResult(); + var post = _blog.GetPostById(postid).GetAwaiter().GetResult(); - if (post != null) - { - return ToMetaWebLogPost(post); - } + if (post != null) + { + return ToMetaWebLogPost(post); + } - return null; + return null; + }); } - public WilderMinds.MetaWeblog.Post[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts) + public Task GetRecentPostsAsync(string blogid, string username, string password, int numberOfPosts) { - ValidateUser(username, password); + return Task.Run(() => + { + ValidateUser(username, password); - return _blog.GetPosts(numberOfPosts).GetAwaiter().GetResult().Select(ToMetaWebLogPost).ToArray(); + return _blog.GetPosts(numberOfPosts).GetAwaiter().GetResult().Select(ToMetaWebLogPost).ToArray(); + }); } - public BlogInfo[] GetUsersBlogs(string key, string username, string password) + public Task GetUsersBlogsAsync(string key, string username, string password) { - ValidateUser(username, password); + return Task.Run(() => + { + ValidateUser(username, password); - var request = _context.HttpContext.Request; - string url = request.Scheme + "://" + request.Host; + var request = _context.HttpContext.Request; + string url = request.Scheme + "://" + request.Host; - return new[] { new BlogInfo { - blogid ="1", - blogName = _config["blog:name"] ?? nameof(MetaWeblogService), - url = url - }}; + return new[] { new BlogInfo { + blogid ="1", + blogName = _config["blog:name"] ?? nameof(MetaWeblogService), + url = url + }}; + }); } - public MediaObjectInfo NewMediaObject(string blogid, string username, string password, MediaObject mediaObject) + public Task NewMediaObjectAsync(string blogid, string username, string password, MediaObject mediaObject) { - ValidateUser(username, password); - byte[] bytes = Convert.FromBase64String(mediaObject.bits); - string path = _blog.SaveFile(bytes, mediaObject.name).GetAwaiter().GetResult(); + return Task.Run(() => + { + ValidateUser(username, password); + byte[] bytes = Convert.FromBase64String(mediaObject.bits); + string path = _blog.SaveFile(bytes, mediaObject.name).GetAwaiter().GetResult(); - return new MediaObjectInfo { url = path }; + return new MediaObjectInfo { url = path }; + }); } - public UserInfo GetUserInfo(string key, string username, string password) + public Task GetUserInfoAsync(string key, string username, string password) { ValidateUser(username, password); throw new NotImplementedException(); } - public int AddCategory(string key, string username, string password, NewCategory category) + public Task AddCategoryAsync(string key, string username, string password, NewCategory category) { ValidateUser(username, password); throw new NotImplementedException(); @@ -187,5 +212,38 @@ private WilderMinds.MetaWeblog.Post ToMetaWebLogPost(Models.Post post) categories = post.Categories.ToArray() }; } + + public Task GetPageAsync(string blogid, string pageid, string username, string password) + { + throw new NotImplementedException(); + } + + public Task GetPagesAsync(string blogid, string username, string password, int numPages) + { + throw new NotImplementedException(); + } + + public Task GetAuthorsAsync(string blogid, string username, string password) + { + throw new NotImplementedException(); + } + + public Task AddPageAsync(string blogid, string username, string password, Page page, bool publish) + { + ValidateUser(username, password); + throw new NotImplementedException(); + } + + public Task EditPageAsync(string blogid, string pageid, string username, string password, Page page, bool publish) + { + ValidateUser(username, password); + throw new NotImplementedException(); + } + + public Task DeletePageAsync(string blogid, string username, string password, string pageid) + { + ValidateUser(username, password); + throw new NotImplementedException(); + } } } diff --git a/src/Startup.cs b/src/Startup.cs index c5eb4189..8d4aeafb 100644 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -1,13 +1,11 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Rewrite; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; using Miniblog.Core.Services; using WebEssentials.AspNetCore.OutputCaching; using WebMarkupMin.AspNetCore2; @@ -29,21 +27,28 @@ public Startup(IConfiguration configuration) public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseKestrel(a => a.AddServerHeader = false); + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder + .UseStartup() + .ConfigureKestrel(options => options.AddServerHeader = false) + ; + }); + } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc() - .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddControllersWithViews(); + services.AddRazorPages(); services.AddSingleton(); services.AddSingleton(); @@ -96,10 +101,11 @@ public void ConfigureServices(IServiceCollection services) pipeline.CompileScssFiles() .InlineImages(1); }); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { @@ -134,11 +140,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseOutputCaching(); app.UseWebMarkupMin(); - app.UseMvc(routes => + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => { - routes.MapRoute( - name: "default", - template: "{controller=Blog}/{action=Index}/{id?}"); + endpoints.MapControllerRoute("default", "{controller=Blog}/{action=Index}/{id?}"); }); } }