mirror of https://github.com/knative/docs.git
Changed C# sample to use .NET Core 3.0 and more lean and secure alpine as base image (#1816)
* Changed C# sample to use more lean and secure alpine as base image * Updated sample to use .NET Core 3.0 * Fixed typos in README.md * Fixed indentation
This commit is contained in:
parent
1acf133068
commit
187ed5ae23
|
@ -1,6 +1,6 @@
|
||||||
# Use Microsoft's official build .NET image.
|
# Use Microsoft's official build .NET image.
|
||||||
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
|
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
|
||||||
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
|
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS build
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install production dependencies.
|
# Install production dependencies.
|
||||||
|
@ -18,7 +18,7 @@ RUN dotnet publish -c Release -o out
|
||||||
|
|
||||||
# Use Microsoft's official runtime .NET image.
|
# Use Microsoft's official runtime .NET image.
|
||||||
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
|
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
|
||||||
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
|
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=build /app/out ./
|
COPY --from=build /app/out ./
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace helloworld_csharp
|
namespace helloworld_csharp
|
||||||
{
|
{
|
||||||
|
@ -8,16 +8,19 @@ namespace helloworld_csharp
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
CreateWebHostBuilder(args).Build().Run();
|
CreateHostBuilder(args).Build().Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
|
public static IHostBuilder CreateHostBuilder(string[] args)
|
||||||
{
|
{
|
||||||
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
|
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
|
||||||
string url = String.Concat("http://0.0.0.0:", port);
|
string url = String.Concat("http://0.0.0.0:", port);
|
||||||
|
|
||||||
return WebHost.CreateDefaultBuilder(args)
|
return Host.CreateDefaultBuilder(args)
|
||||||
.UseStartup<Startup>().UseUrls(url);
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.UseStartup<Startup>().UseUrls(url);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ weight: 1
|
||||||
type: "docs"
|
type: "docs"
|
||||||
---
|
---
|
||||||
|
|
||||||
A simple web app written in C# using .NET Core 2.2 that you can use for testing.
|
A simple web app written in C# using .NET Core 3.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.
|
||||||
|
|
||||||
|
@ -25,16 +25,16 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-csharp
|
||||||
create one.
|
create one.
|
||||||
- [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 2.2](https://www.microsoft.com/net/core).
|
- You have installed [.NET Core SDK 3.0](https://www.microsoft.com/net/core).
|
||||||
|
|
||||||
## Recreating the sample code
|
## Recreating the sample code
|
||||||
|
|
||||||
1. First, make sure you have
|
1. First, make sure you have
|
||||||
[.NET Core SDK 2.2](https://www.microsoft.com/net/core) installed:
|
[.NET Core SDK 3.0](https://www.microsoft.com/net/core) installed:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
dotnet --version
|
dotnet --version
|
||||||
2.2.102
|
3.0.100
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
||||||
|
@ -43,29 +43,35 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-csharp
|
||||||
dotnet new web -o helloworld-csharp
|
dotnet new web -o helloworld-csharp
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Update the `CreateWebHostBuilder` definition in `Program.cs` by adding
|
1. Update the `CreateHostBuilder` definition in `Program.cs` by adding
|
||||||
`.UseUrls()` to define the serving port:
|
`.UseUrls()` to define the serving port:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
|
public static IHostBuilder CreateHostBuilder(string[] args)
|
||||||
{
|
{
|
||||||
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
|
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
|
||||||
string url = String.Concat("http://0.0.0.0:", port);
|
string url = String.Concat("http://0.0.0.0:", port);
|
||||||
|
|
||||||
return WebHost.CreateDefaultBuilder(args)
|
return Host.CreateDefaultBuilder(args)
|
||||||
.UseStartup<Startup>().UseUrls(url);
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.UseStartup<Startup>().UseUrls(url);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Update the `app.Run(...)` statement in `Startup.cs` to read and return the
|
1. Update the `app.UseEndpoints(...)` statement in `Startup.cs` to read and return the
|
||||||
TARGET environment variable:
|
TARGET environment variable:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
app.Run(async (context) =>
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapGet("/", async context =>
|
||||||
{
|
{
|
||||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
||||||
await context.Response.WriteAsync($"Hello {target}!\n");
|
await context.Response.WriteAsync($"Hello {target}!\n");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
1. In your project directory, create a file named `Dockerfile` and copy the code
|
1. In your project directory, create a file named `Dockerfile` and copy the code
|
||||||
|
@ -76,7 +82,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-csharp
|
||||||
```docker
|
```docker
|
||||||
# Use Microsoft's official build .NET image.
|
# Use Microsoft's official build .NET image.
|
||||||
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
|
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
|
||||||
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
|
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS build
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install production dependencies.
|
# Install production dependencies.
|
||||||
|
@ -91,10 +97,9 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-csharp
|
||||||
# Build a release artifact.
|
# 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.
|
# Use Microsoft's official runtime .NET image.
|
||||||
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
|
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
|
||||||
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
|
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=build /app/out ./
|
COPY --from=build /app/out ./
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace helloworld_csharp
|
namespace helloworld_csharp
|
||||||
{
|
{
|
||||||
|
@ -15,18 +16,23 @@ namespace helloworld_csharp
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// 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())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Run(async (context) =>
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapGet("/", async context =>
|
||||||
{
|
{
|
||||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
||||||
await context.Response.WriteAsync($"Hello {target}!\n");
|
await context.Response.WriteAsync($"Hello {target}!\n");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,15 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
<RootNamespace>helloworld_csharp</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="wwwroot\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue