Skip to content

Instantly share code, notes, and snippets.

@RyanHill-MSFT
Created June 23, 2020 03:11
Show Gist options
  • Save RyanHill-MSFT/066751d6ff18bded2b7b47d3056da485 to your computer and use it in GitHub Desktop.
Save RyanHill-MSFT/066751d6ff18bded2b7b47d3056da485 to your computer and use it in GitHub Desktop.
Webjobs SDK using ConfigureServices
class Program
{
private static IConfiguration Configuration { get; set; }
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices()
.AddAzureStorage()
.AddTimers();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey))
{
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
}
});
builder.ConfigureServices((context, s) =>
{
ConfigureServices(s);
s.BuildServiceProvider();
});
var tokenSource = new CancellationTokenSource();
var ct = tokenSource.Token;
var host = builder.Build();
using (host)
{
await host.RunAsync(ct);
}
}
private static void ConfigureServices(IServiceCollection services)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddEnvironmentVariables()
.Build();
services.AddSingleton<IService, SimpleService>();
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<SuccessfulDependencyFilter>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment