Last active
April 28, 2022 14:27
-
-
Save rkttu/638efdb182a70dd8c9ec785255fce87e to your computer and use it in GitHub Desktop.
Running ASP.NET Web API in Windows Container with LINQPad 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2022 AS build | |
RUN powershell.exe -Command \ | |
Invoke-WebRequest -UseBasicParsing 'https://www.linqpad.net/GetFile.aspx?LINQPad7Setup.exe' -OutFile "$home/LINQPad7Setup.exe"; \ | |
Start-Process -FilePath "$home/LINQPad7Setup.exe" -ArgumentList "/SP-","/VERYSILENT","/SUPPRESSMSGBOXES","/NORESTART","/NOICONS" -NoNewWindow -Wait; \ | |
rm -Force "$home/LINQPad7Setup.exe"; && \ | |
setx.exe /M PATH "%PROGRAMFILES%\LINQPad7;%PATH%" | |
FROM build | |
ADD Program.linq . | |
ENV ASPNETCORE_URLS=http://+:5000 | |
CMD lprun7.exe Program.linq | |
EXPOSE 5000/tcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Namespace>System.Threading.Tasks</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>Microsoft.Extensions.Hosting</Namespace> | |
<Namespace>Microsoft.Extensions.Configuration</Namespace> | |
<Namespace>Microsoft.Extensions.DependencyInjection</Namespace> | |
<Namespace>Microsoft.AspNetCore.Builder</Namespace> | |
<Namespace>Microsoft.AspNetCore.Hosting</Namespace> | |
<Namespace>Microsoft.AspNetCore.Mvc</Namespace> | |
<Namespace>Microsoft.Extensions.Logging</Namespace> | |
<Namespace>System.Net.Http</Namespace> | |
<IncludeUncapsulator>false</IncludeUncapsulator> | |
<IncludeLinqToSql>true</IncludeLinqToSql> | |
<IncludeAspNet>true</IncludeAspNet> | |
<RuntimeVersion>6.0</RuntimeVersion> | |
</Query> | |
// Source excerpted from - https://share.linqpad.net/q7cpkj.linq | |
// You can reference the ASP.NET Core Framework (if installed) - just press F4 and tick the checkbox. | |
Task Main() | |
{ | |
// This query uses soft cancellation to shut down the server - see query://..\Runtime_Services\Soft_cancellation | |
var serverTask = CreateHostBuilder().Build().RunAsync(QueryCancelToken); | |
new WebClient().DownloadString("http://localhost:5000/weatherforecast").Dump("Test request"); | |
return serverTask; | |
} | |
public static IHostBuilder CreateHostBuilder() => | |
Host.CreateDefaultBuilder() | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) => Configuration = configuration; | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) => services.AddControllers(); | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
app.UseRouting(); | |
app.UseEndpoints(endpoints => endpoints.MapControllers()); | |
} | |
} | |
namespace Controllers | |
{ | |
[ApiController] | |
[Route("[controller]")] | |
public class WeatherForecastController : ControllerBase | |
{ | |
private static readonly string[] Summaries = new[] | |
{ | |
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
}; | |
private readonly ILogger<WeatherForecastController> _logger; | |
public WeatherForecastController(ILogger<WeatherForecastController> logger) => _logger = logger; | |
[HttpGet] | |
public IEnumerable<WeatherForecast> Get() | |
{ | |
var rng = new Random(); | |
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | |
{ | |
Date = DateTime.Now.AddDays(index), | |
TemperatureC = rng.Next(-20, 55), | |
Summary = Summaries [rng.Next(Summaries.Length)] | |
}) | |
.ToArray(); | |
} | |
} | |
} | |
public class WeatherForecast | |
{ | |
public DateTime Date { get; set; } | |
public int TemperatureC { get; set; } | |
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |
public string Summary { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
pushd "%~dp0" | |
REM You can run this example in Windows 11 or Windows Server 2022 environment. | |
docker.exe run --name linqpad-container-test --isolation=process --rm -d -p 80:5000 linqpad-container:latest | |
REM Then, send web request to http://localhost/weatherforecast. | |
:exit | |
popd | |
@echo on |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment