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

View File

@ -1,26 +1,11 @@
using System; var builder = WebApplication.CreateBuilder(args);
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace helloworld_csharp var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
{ var url = $"http://0.0.0.0:{port}";
public class Program var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) var app = builder.Build();
{
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
string url = String.Concat("http://0.0.0.0:", port);
return Host.CreateDefaultBuilder(args) app.MapGet("/", () => $"Hello {target}!");
.ConfigureWebHostDefaults(webBuilder =>
{ app.Run(url);
webBuilder.UseStartup<Startup>().UseUrls(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 # 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 It reads in an env variable `TARGET` and prints "Hello \${TARGET}!". If TARGET
is not specified, it will use "World" as the 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). [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, - [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). 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 ## Recreating the sample code
1. First, make sure you have 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 ```bash
dotnet --version dotnet --version
3.1.100 6.0.101
``` ```
1. From the console, create a new empty web project using the dotnet command: 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 dotnet new web -o helloworld-csharp
``` ```
1. Update the `CreateHostBuilder` definition in `Program.cs` by adding 1. Update the `Program.cs` to read the port and define the serving url:
`.UseUrls()` to define the serving port:
```csharp ```csharp
public static IHostBuilder CreateHostBuilder(string[] args) var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
{ var url = $"http://0.0.0.0:{port}";
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
string url = String.Concat("http://0.0.0.0:", port);
return Host.CreateDefaultBuilder(args) app.Run(url);
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls(url);
});
}
``` ```
1. Update the `app.UseEndpoints(...)` statement in `Startup.cs` to read and return the 1. Update the `Program.cs` to read and return the `TARGET` environment variable:
TARGET environment variable:
```csharp ```csharp
app.UseEndpoints(endpoints => var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
{
endpoints.MapGet("/", async context => app.MapGet("/", () => $"Hello {target}!");
{
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
await context.Response.WriteAsync($"Hello {target}!\n");
});
});
``` ```
1. In your project directory, create a file named `Dockerfile` and copy the following code 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 ```docker
# Use Microsoft's official build .NET image. # 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 WORKDIR /app
# Install production dependencies. # Copy csproj and restore as distinct layers
# Copy csproj and restore as distinct layers.
COPY *.csproj ./ COPY *.csproj ./
RUN dotnet restore RUN dotnet restore
# Copy local code to the container image. # Copy everything else and build
COPY . ./ COPY . ./
WORKDIR /app
# Build a release artifact.
RUN dotnet publish -c Release -o out RUN dotnet publish -c Release -o out
# Use Microsoft's official runtime .NET image. # Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app WORKDIR /app
COPY --from=build /app/out ./ COPY --from=build-env /app/out .
# Run the web service on container startup. # Run the web service on container startup.
ENTRYPOINT ["dotnet", "helloworld-csharp.dll"] 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"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<RootNamespace>helloworld_csharp</RootNamespace> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
</Project> </Project>