有奖征文|投稿上云技术实践,赢取价值5000元大奖> HOT
文档中心 > 云托管 CloudBase Run > 快速入门 > 构建并部署 C#(.NET)应用

步骤1:编写基础应用

  1. 安装 .NET Core SDK 3.1。在 Console 中,使用 dotnet 命令新建一个空 Web 项目:
    dotnet new web -o helloworld-csharp
    cd helloworld-csharp
  2. 更新 Program.cs 中的 CreateHostBuilder 定义,侦听 80 端口:
    using System;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;

    namespace helloworld_csharp
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    CreateHostBuilder(args).Build().Run()
    ;
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
    string port = "80";
    string url = String.Concat("http://0.0.0.0:", port);

    return Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
    webBuilder.UseStartup<Startup>().UseUrls(url);
    });
    }
    }
    }
  3. Startup.cs 的内容更新为如下:
    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 =>
    {
    await context.Response.WriteAsync("Hello World!\n");
    });
    });
    }
    }
    }
    说明

    以上代码会创建一个基本的 Web 服务器,并监听 80 端口。

步骤2:将应用容器化

  1. 在项目根目录下,创建一个名为 Dockerfile 的文件,内容如下:
    FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
    RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories
    WORKDIR /source

    # copy csproj and restore as distinct layers
    COPY *.sln .
    COPY aspnetapp/*.csproj ./aspnetapp/
    RUN dotnet restore -r linux-musl-x64 /p:PublishReadyToRun=true

    # copy everything else and build app
    COPY aspnetapp/. ./aspnetapp/
    WORKDIR /source/aspnetapp
    RUN dotnet publish -c release -o /app -r linux-musl-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true /p:PublishSingleFile=true

    # final stage/image
    FROM mcr.microsoft.com/dotnet/runtime-deps:6.0-alpine-amd64

    # 容器默认时区为UTC,如需使用上海时间请启用以下时区设置命令
    # RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

    RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories
    WORKDIR /app
    COPY --from=build /app ./

    # See: https://github.com/dotnet/announcements/issues/20
    # Uncomment to enable globalization APIs (or delete)
    # ENV \
    # DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \
    # LC_ALL=en_US.UTF-8 \
    # LANG=en_US.UTF-8
    # RUN apk add --no-cache icu-libs

    ENTRYPOINT ["./aspnetapp"]
  2. 添加一个 .dockerignore 文件,以从容器映像中排除文件:
    **/obj/
    **/bin/

步骤3(可选):本地构建镜像

  1. 如果您本地已经安装了 Docker,可以运行以下命令,在本地构建 Docker 镜像:
    docker build -t helloworld-csharp
  2. 构建成功后,运行 docker images,可以看到构建出的镜像,随后您可以将此镜像上传至您的镜像仓库。
    REPOSITORY          TAG       IMAGE ID         CREATED            SIZE
    helloworld-csharp latest 1c8dfb88c823 8 seconds ago 105MB

步骤4:部署到云托管

详情请参见 部署服务版本配置说明

目录