Last active
January 23, 2019 09:30
-
-
Save xt0rted/d15bc65617120e00e3e87c8388c8f3a2 to your computer and use it in GitHub Desktop.
.net core 2.0 webjob skeleton
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
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Debug", | |
"System": "Information", | |
"Microsoft": "Information" | |
} | |
}, | |
"ConnectionStrings": { | |
"AzureWebJobsDashboard": "UseDevelopmentStorage=true;", | |
"AzureWebJobsStorage": "UseDevelopmentStorage=true;" | |
} | |
} |
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
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Warning" | |
} | |
} | |
} |
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
namespace MyAwesomeProgram | |
{ | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Extensions.Logging; | |
[Disable("MYWEBJOB_DISABLED")] | |
public class Functions | |
{ | |
private readonly ILogger _logger; | |
public Functions(ILoggerFactory logger) | |
{ | |
_logger = logger.CreateLogger<Functions>(); | |
} | |
public async Task JobNumber1( | |
[QueueTrigger("job1")] string message) | |
{ | |
_logger.LogInformation("Running JobNumber1"); | |
await Task.CompletedTask; | |
} | |
public async Task JobNumber2( | |
[QueueTrigger("job2")] string blobName, | |
[Blob("job2/{queueTrigger}")] string blob) | |
{ | |
_logger.LogInformation("Running JobNumber2"); | |
await Task.CompletedTask; | |
} | |
} | |
} |
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
{ | |
"profiles": { | |
"MyWebjob": { | |
"commandName": "Project", | |
"environmentVariables": { | |
"ASPNETCORE_ENVIRONMENT": "Development" | |
} | |
} | |
} | |
} |
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
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<!-- Webjobs settings --> | |
<PropertyGroup> | |
<IsWebJobProject>true</IsWebJobProject> | |
<WebJobName>MyWebjob</WebJobName> | |
<WebJobType>Continuous</WebJobType> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta4" /> | |
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.0-beta4" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Update="appsettings*.json"> | |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |
</None> | |
</ItemGroup> | |
</Project> |
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
namespace MyAwesomeProgram | |
{ | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
public static class Program | |
{ | |
private static IConfigurationRoot Configuration { get; set; } | |
private static IServiceProvider Services { get; set; } | |
public static void Main(string[] args) | |
{ | |
SetupConfiguration(args); | |
SetupServices(); | |
var instrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY"); | |
using (ILoggerFactory loggerFactory = new LoggerFactory()) | |
{ | |
if (!string.IsNullOrEmpty(instrumentationKey)) | |
{ | |
loggerFactory.AddApplicationInsights(instrumentationKey, null); | |
} | |
loggerFactory.AddConsole(); | |
var hostConfig = new JobHostConfiguration(Configuration) | |
{ | |
JobActivator = new ServiceProviderJobActivator(Services), | |
LoggerFactory = loggerFactory | |
}; | |
hostConfig.Tracing.ConsoleLevel = TraceLevel.Off; | |
if (hostConfig.IsDevelopment) | |
{ | |
hostConfig.UseDevelopmentSettings(); | |
} | |
var host = new JobHost(hostConfig); | |
host.RunAndBlock(); | |
} | |
} | |
private static void SetupConfiguration(string[] args) | |
{ | |
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true) | |
.AddEnvironmentVariables(); | |
if (args != null) | |
{ | |
builder.AddCommandLine(args); | |
} | |
Configuration = builder.Build(); | |
} | |
private static void SetupServices() | |
{ | |
var services = new ServiceCollection() | |
.AddLogging(config => | |
{ | |
config.AddConfiguration(Configuration.GetSection("Logging")); | |
config.AddConsole(); | |
}) | |
.AddSingleton(Configuration); | |
ConfigureServices(services); | |
Services = services.BuildServiceProvider(); | |
} | |
private static void ConfigureServices(IServiceCollection services) | |
{ | |
// add your function dependencies here | |
services.AddTransient<Functions>(); | |
} | |
} | |
} |
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
namespace MyAwesomeProgram | |
{ | |
using System; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Extensions.DependencyInjection; | |
public class ServiceProviderJobActivator : IJobActivator | |
{ | |
private readonly IServiceProvider _services; | |
public ServiceProviderJobActivator(IServiceProvider services) | |
{ | |
_services = services; | |
} | |
public T CreateInstance<T>() | |
{ | |
return _services.GetService<T>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment