Created
June 23, 2020 03:11
-
-
Save RyanHill-MSFT/066751d6ff18bded2b7b47d3056da485 to your computer and use it in GitHub Desktop.
Webjobs SDK using ConfigureServices
This file contains hidden or 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
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