Skip to content

Update runtime versions and optimise dotnet implementation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11.2
FROM golang:1.14.3

WORKDIR /go/src/app
COPY . .
Expand Down
23 changes: 11 additions & 12 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ type Response struct {
}

func main() {
http.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) {
rsp := Response{
Id: "id_" + strconv.Itoa(rand.Int()),
Name: "name_" + strconv.Itoa(rand.Int()),
Time: time.Now().Unix(),
}

js, err := json.Marshal(rsp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rsp := Response{
Id: "id_" + strconv.Itoa(rand.Int()),
Name: "name_" + strconv.Itoa(rand.Int()),
Time: time.Now().Unix(),
}

js, err := json.Marshal(rsp)
if err != nil {
panic(err)
return
}
http.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(js); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11.2
FROM golang:1.14.3

WORKDIR /go/src/app
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion netcore/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM microsoft/dotnet:2.2-sdk
FROM mcr.microsoft.com/dotnet/core/sdk:3.1

WORKDIR /dotnetapp

Expand Down
28 changes: 8 additions & 20 deletions netcore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Utf8Json;

class Program
{
Expand All @@ -37,7 +31,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
}
}

class Response
public struct Response
{
public string Id { get; set; }
public string Name { get; set; }
Expand All @@ -60,22 +54,16 @@ private static void HandleTest(IApplicationBuilder app)
{
app.Run(async ctx =>
{
using (var rsp = await _http.GetAsync("/data"))
{
var str = await rsp.Content.ReadAsStringAsync();
await using var rsp = await _http.GetStreamAsync("/data");

// deserialize
var obj = JsonConvert.DeserializeObject<Response>(str);
// deserialize
var obj = await JsonSerializer.DeserializeAsync<Response>(rsp);

// serialize
var json = JsonConvert.SerializeObject(obj);

ctx.Response.ContentType = "application/json";
await ctx.Response.WriteAsync(json);
}
// serialize
ctx.Response.ContentType = "application/json";
await JsonSerializer.SerializeAsync(ctx.Response.Body, obj);
});
}

public void Configure(IApplicationBuilder app)
{
app.Map("/test", HandleTest);
Expand Down
7 changes: 3 additions & 4 deletions netcore/netcore.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

</Project>