From 7ed678cda892b7530ad1906eb5085874dbf95c69 Mon Sep 17 00:00:00 2001 From: Mattias Cibien Date: Thu, 23 Jun 2022 09:55:30 +0200 Subject: [PATCH] Upgrade to .NET 6.0 (#13) --- Dockerfile | 4 ++-- ImageSearch/BingImageSearch.cs | 11 +++++----- ImageSearch/IImageSearch.cs | 7 ++++--- ImgBot.cs | 2 +- Program.cs | 12 +++++------ image-search-bot.csproj | 37 +++++++++++++++++----------------- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/Dockerfile b/Dockerfile index 27a5350..570a556 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build +FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build WORKDIR /app # copy csproj and restore as distinct layers @@ -13,7 +13,7 @@ WORKDIR /app/image-search-bot RUN dotnet publish -c Release -o out # run application -FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine AS runtime +FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine AS runtime WORKDIR /app COPY --from=build /app/image-search-bot/out ./ CMD dotnet image-search-bot.dll \ No newline at end of file diff --git a/ImageSearch/BingImageSearch.cs b/ImageSearch/BingImageSearch.cs index c40fdd1..fefccb0 100644 --- a/ImageSearch/BingImageSearch.cs +++ b/ImageSearch/BingImageSearch.cs @@ -1,5 +1,7 @@ using System; using System.Net; +using System.Net.Http; +using System.Threading.Tasks; using ImageSearchBot.Config; using Microsoft.Azure.CognitiveServices.Search.ImageSearch; @@ -22,7 +24,7 @@ public override void Dispose() _client.Dispose(); } - public override byte[] GetImage(int index) + public override async Task GetImageAsync(int index) { var imageResults = _client.Images.SearchAsync( Config.Query, @@ -32,10 +34,9 @@ public override byte[] GetImage(int index) var clampedIndex = Math.Clamp(index, 0, imageResults.Value.Count); var imageUrl = imageResults.Value[clampedIndex].ContentUrl; - using (var webClient = new WebClient()) - { - return webClient.DownloadData(imageUrl); - } + using var webClient = new HttpClient(); + + return await webClient.GetByteArrayAsync(imageUrl); } } } \ No newline at end of file diff --git a/ImageSearch/IImageSearch.cs b/ImageSearch/IImageSearch.cs index 308e463..8873f5f 100644 --- a/ImageSearch/IImageSearch.cs +++ b/ImageSearch/IImageSearch.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using ImageSearchBot.Config; namespace ImageSearchBot.ImageSearch @@ -8,8 +9,8 @@ public interface IImageSearch : IDisposable string BotPrefix { get; } int MaxImages { get; } - - byte[] GetImage(int index); + + Task GetImageAsync(int index); } public abstract class ImageSearch : IImageSearch @@ -28,6 +29,6 @@ protected ImageSearch(string botPrefix, ImageSearchConfig config) public abstract void Dispose(); - public abstract byte[] GetImage(int index); + public abstract Task GetImageAsync(int index); } } \ No newline at end of file diff --git a/ImgBot.cs b/ImgBot.cs index 8aeb155..72b4007 100644 --- a/ImgBot.cs +++ b/ImgBot.cs @@ -83,7 +83,7 @@ await _bot.SendTextMessageAsync(message.Chat.Id, var random = new Random((int)DateTime.Now.Ticks); if (hasKeyword) { - var image = _imageSearch.GetImage(random.Next(_imageSearch.MaxImages)); + var image = await _imageSearch.GetImageAsync(random.Next(_imageSearch.MaxImages)); using (var memoryStream = new MemoryStream(image)) { diff --git a/Program.cs b/Program.cs index 4c1b3ae..cddb909 100644 --- a/Program.cs +++ b/Program.cs @@ -3,13 +3,13 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; using dotenv.net; using ImageSearchBot.Config; using ImageSearchBot.ImageSearch; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; namespace ImageSearchBot { @@ -58,11 +58,11 @@ public static void Main(string[] args) private static void CreateAndRunBot(string config) { var contents = File.ReadAllText(config); - var cfg = JsonConvert.DeserializeObject(contents, - new JsonSerializerSettings() + var cfg = JsonSerializer.Deserialize(contents, + new JsonSerializerOptions() { - ContractResolver = new CamelCasePropertyNamesContractResolver(), - NullValueHandling = NullValueHandling.Ignore + PropertyNameCaseInsensitive = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); Assert.Check(cfg != null, "Root configuration must be set"); diff --git a/image-search-bot.csproj b/image-search-bot.csproj index eda8bdb..edd9510 100644 --- a/image-search-bot.csproj +++ b/image-search-bot.csproj @@ -1,25 +1,24 @@  - - Exe - netcoreapp2.2 - ImageSearchBot - + + Exe + net6.0 + ImageSearchBot + - - - - - - + + + + + - - - PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + + + PreserveNewest + +