Updated HelloWorld C# sample to the latest 6.0 version #4643 (#4648)

* Updated HelloWorld C# sample to the latest 6.0 version #4643

* Fixed a lint problem

* Updated README.md with .NET 6 instructions for #4643
This commit is contained in:
Mete Atamel 2022-01-20 21:25:35 +00:00 committed by GitHub
parent 7756bafd58
commit 14509f3fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 137 deletions

View File

@ -1,24 +1,19 @@
# Use Microsoft's official build .NET image.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Install production dependencies.
# Copy csproj and restore as distinct layers.
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy local code to the container image.
# Copy everything else and build
COPY . ./
WORKDIR /app
# Build a release artifact.
RUN dotnet publish -c Release -o out
# Use Microsoft's official runtime .NET image.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app/out ./
COPY --from=build-env /app/out .
# Run the web service on container startup.
ENTRYPOINT ["dotnet", "helloworld-csharp.dll"]

View File

@ -1,26 +1,11 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
namespace helloworld_csharp
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
var url = $"http://0.0.0.0:{port}";
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
public static IHostBuilder CreateHostBuilder(string[] args)
{
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
string url = String.Concat("http://0.0.0.0:", port);
var app = builder.Build();
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls(url);
});
}
}
}
app.MapGet("/", () => $"Hello {target}!");
app.Run(url);

View File

@ -1,27 +0,0 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39053",
"sslPort": 44372
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"helloworld_csharp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -1,6 +1,6 @@
# Hello world - .NET Core
A simple web app written in C# using .NET Core 3.1 that you can use for testing.
A simple web app written in C# using .NET 6.0 that you can use for testing.
It reads in an env variable `TARGET` and prints "Hello \${TARGET}!". If TARGET
is not specified, it will use "World" as the TARGET.
@ -19,16 +19,16 @@ cd knative-docs/code-samples/serving/hello-world/helloworld-csharp
[Install Knative Serving](https://knative.dev/docs/install/serving/install-serving-with-yaml).
- [Docker](https://www.docker.com) installed and running on your local machine,
and a Docker Hub account configured (we'll use it for a container registry).
- You have installed [.NET Core SDK 3.1](https://www.microsoft.com/net/core).
- You have installed [.NET Core SDK 6.0](https://www.microsoft.com/net/).
## Recreating the sample code
1. First, make sure you have
[.NET Core SDK 3.1](https://www.microsoft.com/net/core) installed:
[.NET SDK 6.0](https://www.microsoft.com/net/) installed:
```bash
dotnet --version
3.1.100
6.0.101
```
1. From the console, create a new empty web project using the dotnet command:
@ -37,35 +37,21 @@ cd knative-docs/code-samples/serving/hello-world/helloworld-csharp
dotnet new web -o helloworld-csharp
```
1. Update the `CreateHostBuilder` definition in `Program.cs` by adding
`.UseUrls()` to define the serving port:
1. Update the `Program.cs` to read the port and define the serving url:
```csharp
public static IHostBuilder CreateHostBuilder(string[] args)
{
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
string url = String.Concat("http://0.0.0.0:", port);
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
var url = $"http://0.0.0.0:{port}";
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls(url);
});
}
app.Run(url);
```
1. Update the `app.UseEndpoints(...)` statement in `Startup.cs` to read and return the
TARGET environment variable:
1. Update the `Program.cs` to read and return the `TARGET` environment variable:
```csharp
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
await context.Response.WriteAsync($"Hello {target}!\n");
});
});
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
app.MapGet("/", () => $"Hello {target}!");
```
1. In your project directory, create a file named `Dockerfile` and copy the following code
@ -75,25 +61,21 @@ cd knative-docs/code-samples/serving/hello-world/helloworld-csharp
```docker
# Use Microsoft's official build .NET image.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Install production dependencies.
# Copy csproj and restore as distinct layers.
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy local code to the container image.
# Copy everything else and build
COPY . ./
WORKDIR /app
# Build a release artifact.
RUN dotnet publish -c Release -o out
# Use Microsoft's official runtime .NET image.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app/out ./
COPY --from=build-env /app/out .
# Run the web service on container startup.
ENTRYPOINT ["dotnet", "helloworld-csharp.dll"]

View File

@ -1,38 +0,0 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace helloworld_csharp
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
await context.Response.WriteAsync($"Hello {target}!\n");
});
});
}
}
}

View File

@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>helloworld_csharp</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>