docker-netcore-httpserver學習筆記

準備環境:docker-netcore-firstapp學習筆記

最終效果
啓動服務

# dotnet run 
Hosting environment: Production
Content root path: /home/hwapp/bin/Debug/netcoreapp2.0/
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

客戶端訪問

# docker exec -it mad_noether curl http://localhost:5000
hello world!

#docker exec -it mad_noether curl http://localhost:5000/will/any/url/work?
hello world!

編輯Program.cs,Startup.cs,hwapp.csproj
1.cat Program.cs

using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;

namespace hwapp
{
    class Program
    {
        static void Main(string[] args)
        {
        var config = new ConfigurationBuilder().AddCommandLine(args).Build();
        var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().UseConfiguration(config).Build();
        host.Run();
        }
    }
}

2.cat Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;

namespace hwapp{
    public class Startup{
        public Startup(IHostingEnvironment env){
        }

        public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerFactory){
            app.Run(async (context) => {
                    await context.Response.WriteAsync("hello world!");
                    });
        }
    }
}

3.cat hwapp.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0"/>
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0"/>
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0"/>
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0"/>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0"/>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.0.0"/>
</ItemGroup>
</Project>

參考資料:
《building microservice with asp.net core》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章