Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Worker template #7401

Merged
merged 11 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ProjectTemplates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Templates

## Getting Started
These are project templates which are used in .NET Core for creating ASP.NET Core applications.

## Building Templates
- Run `build.cmd -Pack` in the repository root to build all of the dependencies.
- Run `build.cmd` in this directory will produce NuGet packages for each class of template in the artifacts directory. These can be installed via `dotnet new -i {nugetpackage path}`
- The ASP.NET localhost development certificate must also be installed and trusted or else you'll get a test error "Certificate error: Navigation blocked".
- You also need to get the packages these templates depend on into your package cache or else `dotnet new` restore will fail. The easiest way to get them to run is by letting the build run at least 1 test. Note currently some packages are missed. https://github.com/aspnet/AspNetCore/issues/7388
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MicrosoftEntityFrameworkCoreSqlitePackageVersion=$(MicrosoftEntityFrameworkCoreSqlitePackageVersion);
MicrosoftEntityFrameworkCoreSqlServerPackageVersion=$(MicrosoftEntityFrameworkCoreSqlServerPackageVersion);
MicrosoftEntityFrameworkCoreToolsPackageVersion=$(MicrosoftEntityFrameworkCoreToolsPackageVersion);
MicrosoftExtensionsHostingPackageVersion=$(MicrosoftExtensionsHostingPackageVersion);
MicrosoftNETCoreAppPackageVersion=$(MicrosoftNETCoreAppPackageVersion);
</GeneratedContentProperties>
</PropertyGroup>
Expand All @@ -35,6 +36,7 @@
</ItemGroup>

<ItemGroup>
<GeneratedContent Include="Worker-CSharp.csproj.in" OutputPath="content/Worker-CSharp/Company.Application1.csproj" />
<GeneratedContent Include="EmptyWeb-CSharp.csproj.in" OutputPath="content/EmptyWeb-CSharp/Company.WebApplication1.csproj" />
<GeneratedContent Include="EmptyWeb-FSharp.fsproj.in" OutputPath="content/EmptyWeb-FSharp/Company.WebApplication1.fsproj" />
<GeneratedContent Include="RazorComponentsWeb-CSharp.App.csproj.in" OutputPath="content/RazorComponentsWeb-CSharp/RazorComponentsWeb-CSharp.App/RazorComponentsWeb-CSharp.App.csproj" />
Expand Down
13 changes: 13 additions & 0 deletions src/ProjectTemplates/Web.ProjectTemplates/Worker-CSharp.csproj.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Exe</OutputType>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.Application1</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="${MicrosoftExtensionsHostingPackageVersion}" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json.schemastore.org/dotnetcli.host",
"symbolInfo": {
"Framework": {
"longName": "framework"
},
"skipRestore": {
"longName": "no-restore",
"shortName": ""
},
"ExcludeLaunchSettings": {
"longName": "exclude-launch-settings",
"shortName": ""
}
},
"usageExamples": [
""
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Microsoft",
"classifications": [
"Common",
"Worker"
],
"name": ".NET Worker",
"generatorVersions": "[1.0.0.0-*)",
"description": "A project template for creating a .NET Core worker application. This template does not have any content in it.",
"groupIdentity": "Microsoft.Net.Worker",
"precedence": "5000",
"identity": "Microsoft.WorkerTemplate.CSharp.3.0",
"shortName": "worker",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "Company.Application1",
"preferNameDirectory": true,
"guids": [
"a3bc9b9d-9d6a-45d4-8429-2a276177350a"
],
"sources": [
{
"modifiers": [
{
"condition": "(ExcludeLaunchSettings)",
"exclude": [
"Properties/launchSettings.json"
]
}
]
}
],
"symbols": {
"ExcludeLaunchSettings": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Whether to exclude launchSettings.json from the generated template."
},
"Framework": {
"type": "parameter",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "netcoreapp3.0",
"description": "Target netcoreapp3.0"
}
],
"replaces": "netcoreapp3.0",
"defaultValue": "netcoreapp3.0"
},
"copyrightYear": {
"type": "generated",
"generator": "now",
"replaces": "copyrightYear",
"parameters": {
"format": "yyyy"
}
},
"skipRestore": {
"type": "parameter",
"datatype": "bool",
"description": "If specified, skips the automatic restore of the project on create.",
"defaultValue": "false"
}
},
"primaryOutputs": [
{
"path": "Company.Application1.csproj"
}
],
"defaultName": "Application1",
"postActions": [
{
"condition": "(!skipRestore)",
"description": "Restore NuGet packages required by this project.",
"manualInstructions": [
{
"text": "Run 'dotnet restore'"
}
],
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
"continueOnError": true
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json.schemastore.org/vs-2017.3.host",
"name": {
"text": "Worker",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC9}",
"id": "1011"
},
"description": {
"text": "A project template for creating a .NET Worker application. This template does not have any content in it.",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC9}",
"id": "1012"
},
"order": 100,
"icon": "vs-2017.3/Empty.png",
"learnMoreLink": "https://go.microsoft.com/fwlink/?LinkID=784883",
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
"uiFilters": [ "oneaspnet" ],
"supportsDocker": true,
"legacyTemplateIdentity": "Microsoft.NetCore.CSharp.Worker",
"supportedAuthentications": [
{
"auth": "None",
"authenticationType": "NoAuth",
"allowUnsecured": true
}
],
"excludeLaunchSettings": false,
"minFullFrameworkVersion": "4.6.1",
"disableHttpsSymbol": "NoHttps"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Company.Application1
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services => {
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
services.AddHostedService<Worker>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Company.Application1
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation($"Worker running at: {DateTime.Now}");
await Task.Delay(1000);
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}