准备环境: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》