Skip to content

Commit

Permalink
windows service sample
Browse files Browse the repository at this point in the history
  • Loading branch information
christiannagel committed Sep 16, 2019
1 parent be5067e commit 37fe0ff
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DotnetCore/WindowsServiceSample/WindowsServiceSample.sln
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.61
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsServiceSample", "WindowsServiceSample\WindowsServiceSample.csproj", "{26B73191-83FD-4BC4-BEFE-184A742EE0F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26B73191-83FD-4BC4-BEFE-184A742EE0F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26B73191-83FD-4BC4-BEFE-184A742EE0F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26B73191-83FD-4BC4-BEFE-184A742EE0F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26B73191-83FD-4BC4-BEFE-184A742EE0F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A34D350-B0F2-4A67-9222-9442881FF13C}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions DotnetCore/WindowsServiceSample/WindowsServiceSample/Program.cs
@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.EventLog;

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

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>()
.Configure<EventLogSettings>(config =>
{
config.LogName = "My Log";
config.SourceName = "My Source";
});
}).UseWindowsService();
}
}
@@ -0,0 +1,10 @@
{
"profiles": {
"WindowsServiceSample": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>dotnet-WindowsServiceSample-86566739-9A6B-44BC-9776-B39865E786CF</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0-preview9.19423.4" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.0.0-preview9.19423.4" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions DotnetCore/WindowsServiceSample/WindowsServiceSample/Worker.cs
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WindowsServiceSample
{
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: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

0 comments on commit 37fe0ff

Please sign in to comment.