Skip to content

Instantly share code, notes, and snippets.

@pksorensen
Created May 6, 2019 19:24
Show Gist options
  • Save pksorensen/620f55efb69d2ef85dc91bdb7b5e8d32 to your computer and use it in GitHub Desktop.
Save pksorensen/620f55efb69d2ef85dc91bdb7b5e8d32 to your computer and use it in GitHub Desktop.
using DotNETDevOps.Extensions.AzureFunctions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
[assembly: WebJobsStartup(typeof(AspNetCoreWebHostStartUp))]
[assembly: WebJobsStartup(typeof(HostedServiceTestFunction.WebjobStartup))]
namespace HostedServiceTestFunction
{
public class WebjobStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddHostedService<HostedServiceA>();
}
}
public class HostedServiceA : BackgroundService
{
private readonly ILogger<HostedServiceA> logger;
public HostedServiceA(ILogger<HostedServiceA> logger)
{
this.logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000);
logger.LogInformation($"Running {nameof(HostedServiceA)} service");
}
}
}
public class HostedServiceB : BackgroundService
{
private readonly ILogger<HostedServiceA> logger;
public HostedServiceB(ILogger<HostedServiceA> logger)
{
this.logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000);
logger.LogInformation($"Running {nameof(HostedServiceB)} service");
}
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedService<HostedServiceB>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
app.Run(k => k.Response.WriteAsync("hello world"));
}
}
public class ServerlessApi
{
private readonly IAspNetCoreRunner<ServerlessApi> aspNetCoreRunner;
public ServerlessApi(IAspNetCoreRunner<ServerlessApi> aspNetCoreRunner)
{
this.aspNetCoreRunner = aspNetCoreRunner;
}
[FunctionName("AspNetCoreHost")]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, Route = "{*all}")]HttpRequest req, Microsoft.Azure.WebJobs.ExecutionContext executionContext)
=> aspNetCoreRunner.RunAsync<Startup>(req, executionContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment