This commit is contained in:
bytewizer 2022-07-10 09:09:35 -07:00
Родитель 5d8e27ae8a
Коммит b321d35df3
21 изменённых файлов: 201 добавлений и 28 удалений

32
.github/workflows/update-dependencies.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,32 @@
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.
# This workflow will periodically check .NET nanoFramework dependencies and updates them in the repository it's running.
name: Daily update dependencies
on:
schedule:
# At 00:00 UTC every day.
- cron: '00 00 * * *'
repository_dispatch:
types: update-dependencies
defaults:
run:
shell: pwsh
jobs:
update-dotnet-preview:
name: Update .NET nanoFramework dependencies
timeout-minutes: 15
runs-on: windows-latest
env:
GITHUB_TOKEN: ${{ github.token }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Update dependencies
uses: nanoframework/nanodu@v1
with:
solutionsToCheck: 'nanoFramework.Hosting.sln'

Просмотреть файл

@ -0,0 +1,13 @@
user=nanoframework
project=nanoFramework.Hosting
issues=true
add_issues_wo_labels=false
add_pr_wo_labels=false
add_issues_wo_labels=false
filter_issues_by_milestone=false
exclude_labels=Area: Config-and-Build,Area: Infrastructure-and-Organization,reverted
enhancement_labels=Type: enhancement
bug_labels=Type: bug
merge_prefix=**Documentation and other chores:**
unreleased_label=**Changes available only in 'Preview' NuGet packages:**
author=false

7
NuGet.Config Normal file
Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="NuGet" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Azure Artifacts nanoFramework dev" value="https://pkgs.dev.azure.com/nanoframework/feed/_packaging/sandbox/nuget/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

Просмотреть файл

@ -5,7 +5,7 @@
-----
# Welcome to the .NET nanoFramework Generic Host Library repository
The .NET nanoFramework Generic Host provides convenience methods for creating [dependency injection (DI)](https://github.com/nanoframework/nanoFramework.DependencyInjection/tree/main) hosted application container services with preconfigured defaults.
The .NET nanoFramework Generic Host provides convenience methods for creating [dependency injection (DI)](https://github.com/nanoframework/nanoFramework.DependencyInjection/tree/main) application containers with preconfigured defaults.
## Build status
@ -19,10 +19,8 @@ The .NET nanoFramework Generic Host provides convenience methods for creating [d
[Hosting Unit Tests](https://github.com/nanoframework/nanoFramework.Hosting/tree/main/tests)
## What is a Generic Host
The Generic Host sets up a default DI application container as well as provides a few services in the DI container which handle the the application lifetime.
When a host starts it calls *Start()* on each implementation of IHostedService registered in the service container's collection of hosted services. In the application container all IHostedService implementations that contain BackgroundService or SchedulerService instances have their *ExecuteAsync* methods called.
## Generic Host
A Generic Host configures a DI application container as well as provides services in the DI container which handle the the application lifetime. When a host starts it calls *Start* on each implementation of IHostedService registered in the service container's collection of hosted services. In the application container all IHostedService object that inherent BackgroundService or SchedulerService have their *ExecuteAsync* methods called.
This API mirrors as close as possible the official .NET
[Generic Host](https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host).
@ -56,7 +54,7 @@ namespace Hosting
## BackgroundService base class
Is a base class for implementing a long running IHostedService. The method *ExecuteAsync* is called asynchronously to run the background service. Your implementation of *ExecuteAsync* should finish promptly when the *CancellationRequested* is fired in order to gracefully shut down the service.
Provides a base class for implementing a long running IHostedService. The method *ExecuteAsync* is called asynchronously to run the background service. Your implementation of *ExecuteAsync* should finish promptly when the *CancellationRequested* is fired in order to gracefully shut down the service.
```csharp
public class SensorService : BackgroundService
@ -74,7 +72,7 @@ public class SensorService : BackgroundService
```
## SchedulerService base class
A timed background task makes use of the [Timer](https://docs.nanoframework.net/api/System.Threading.Timer.html) class. The timer triggers at specified interval the 'ExecuteAsync' method. The timer is disabled on Stop and disposed when the service container is disposed.
Provides a base class to schedule a thread making use of the [Timer](https://docs.nanoframework.net/api/System.Threading.Timer.html) running IHostedServce. The timer triggers at a specified time and interval the 'ExecuteAsync' method. The timer is disabled on Stop and disposed when the service container is disposed.
```csharp
public class DisplayService : SchedulerService

Двоичные данные
assets/nf-logo.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 9.0 KiB

21
assets/readme.txt Normal file
Просмотреть файл

@ -0,0 +1,21 @@
_____ _
_ __ __ _ _ __ ___ | ___| __ __ _ _ __ ___ _____ _____ _ __| | __
| '_ \ / _` | '_ \ / _ \| |_ | '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| | | | (_| | | | | (_) | _|| | | (_| | | | | | | __/\ V V / (_) | | | <
|_| |_|\__,_|_| |_|\___/|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
===================================================================================
API docs: https://docs.nanoframework.net/api/nanoFramework.DependencyInjection.html
Browse our samples repository: https://github.com/nanoframework/samples
Check our documentation online: https://docs.nanoframework.net/
Join our lively Discord community: https://discord.gg/gCyBu8T
Report issues: https://github.com/nanoframework/Home/issues
Follow us on Twitter: https://twitter.com/nanoframework
Follow our YouTube channel: https://www.youtube.com/c/nanoFramework

61
azure-pipelines.yml Normal file
Просмотреть файл

@ -0,0 +1,61 @@
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.
trigger:
branches:
include:
- main
- develop
- release-*
paths:
exclude:
- .github_changelog_generator
- .gitignore
- CHANGELOG.md
- CODE_OF_CONDUCT.md
- LICENSE.md
- README.md
- NuGet.Config
- assets/*
- config/*
- .github/*
# PR always trigger build
pr:
autoCancel: true
# add nf-tools repo to resources (for Azure Pipelines templates)
resources:
repositories:
- repository: templates
type: github
name: nanoframework/nf-tools
endpoint: nanoframework
pool:
vmImage: 'windows-latest'
variables:
DOTNET_NOLOGO: true
solution: 'nanoFramework.Hosting.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
nugetPackageName: 'nanoFramework.Hosting'
steps:
# step from template @ nf-tools repo
# all build, update and publish steps
- template: azure-pipelines-templates/class-lib-build.yml@templates
parameters:
sonarCloudProject: 'nanoframework_nanoFramework.Hosting'
runUnitTests: true
unitTestRunsettings: '$(System.DefaultWorkingDirectory)\tests\nano.runsettings'
# step from template @ nf-tools repo
# report error
- template: azure-pipelines-templates/discord-webhook-task.yml@templates
parameters:
status: 'failure'
webhookUrl: '$(DiscordWebhook)'
message: ''

Просмотреть файл

@ -5,6 +5,8 @@ VisualStudioVersion = 17.2.32519.379
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5CA8DDDF-50C5-4044-A78C-170228780423}"
ProjectSection(SolutionItems) = preProject
nanoframework.Hosting.nuspec = nanoframework.Hosting.nuspec
README.md = README.md
version.json = version.json
EndProjectSection
EndProject

Просмотреть файл

@ -16,8 +16,7 @@ namespace nanoFramework.Hosting
/// <returns>The initialized <see cref="IHostBuilder"/>.</returns>
public static IHostBuilder CreateDefaultBuilder()
{
var builder = new HostBuilder();
return builder;
return new HostBuilder();
}
}
}

Просмотреть файл

@ -32,7 +32,7 @@ namespace nanoFramework.Hosting
if (Debugger.IsAttached)
{
// enables validation as default when debugger is attached
// enables di validation as default when debugger is attached
_providerOptions = new ServiceProviderOptions()
{
ValidateOnBuild = true

Просмотреть файл

@ -20,11 +20,13 @@ namespace nanoFramework.Hosting
/// <summary>
/// Start the program.
/// </summary>
/// <exception cref="AggregateException">One or more hosted services failed to start.</exception>
void Start();
/// <summary>
/// Attempts to gracefully stop the program.
/// </summary>
/// <exception cref="AggregateException">One or more hosted services failed to stop.</exception>
void Stop();
}
}

Просмотреть файл

@ -39,7 +39,6 @@ namespace nanoFramework.Hosting
/// Run the given actions to initialize the host. This can only be called once.
/// </summary>
/// <returns>An initialized <see cref="IHost"/>.</returns>
/// <exception cref="InvalidOperationException">The <see cref="BuildServiceProvider"/> returned a <see langword="null"/> ServiceProvider.</exception>
/// <exception cref="InvalidOperationException">"Build can only be called once."</exception>
IHost Build();
}

Просмотреть файл

@ -10,10 +10,16 @@ using nanoFramework.DependencyInjection;
namespace nanoFramework.Hosting.Internal
{
/// <summary>
/// Default implementation of <see cref="IHost"/>.
/// </summary>
internal class Host : IHost, IDisposable
{
private object[] _hostedServices;
/// <summary>
/// Initializes a new instance of <see cref="Host"/>.
/// </summary>
public Host(IServiceProvider services)
{
if (services == null)
@ -54,7 +60,7 @@ namespace nanoFramework.Hosting.Internal
if (exceptions != null)
{
throw new AggregateException("One or more hosted services failed to start.", exceptions);
throw new AggregateException(string.Empty, exceptions);
}
}
@ -82,7 +88,7 @@ namespace nanoFramework.Hosting.Internal
if (exceptions != null)
{
throw new AggregateException("One or more hosted services failed to stop.", exceptions); ;
throw new AggregateException(string.Empty, exceptions); ;
}
}

Просмотреть файл

@ -4,7 +4,6 @@
//
using System;
using nanoFramework.DependencyInjection;
namespace nanoFramework.Hosting
@ -20,6 +19,8 @@ namespace nanoFramework.Hosting
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
/// <param name="implementationType">The implementation type of the service.</param>
/// <returns>The original <see cref="IServiceCollection"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="implementationType"/> can't be null</exception>
/// <exception cref="ArgumentException">Implementation type must inherit <see cref="IHostedService"/> interface.</exception>
public static IServiceCollection AddHostedService(this IServiceCollection services, Type implementationType)
{
if (services == null)
@ -27,6 +28,21 @@ namespace nanoFramework.Hosting
throw new ArgumentNullException();
}
if (implementationType == null)
{
throw new ArgumentNullException();
}
foreach (Type interfaceType in implementationType.GetInterfaces())
{
if (interfaceType.Equals(typeof(IHostedService)))
{
break;
}
throw new ArgumentException();
}
return services.AddSingleton(typeof(IHostedService), implementationType);
}
}

Двоичные данные
nanoFramework.Hosting/key.snk Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -15,6 +15,17 @@
<RootNamespace>nanoFramework.Hosting</RootNamespace>
<AssemblyName>nanoFramework.Hosting</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<NF_IsCoreLibrary>True</NF_IsCoreLibrary>
<DocumentationFile>bin\$(Configuration)\nanoFramework.Hosting.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>false</DelaySign>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
@ -53,4 +64,11 @@
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>

Просмотреть файл

@ -2,4 +2,5 @@
<packages>
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
<package id="nanoFramework.DependencyInjection" version="1.0.0.11" targetFramework="netnano1.0" />
<package id="Nerdbank.GitVersioning" version="3.5.107" developmentDependency="true" targetFramework="netnano1.0" />
</packages>

Просмотреть файл

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>nanoframework.Hosting</id>
<id>nanoFramework.Hosting</id>
<version>$version$</version>
<title>nanoframework.Hosting</title>
<title>nanoFramework.Hosting</title>
<authors>nanoframework</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="file">LICENSE.md</license>
@ -13,7 +13,7 @@
<developmentDependency>false</developmentDependency>
<projectUrl>https://github.com/nanoframework/nanoframework.Hosting</projectUrl>
<icon>images\nf-logo.png</icon>
<repository type="git" url="https://github.com/nanoframework/nanoframework.Hosting" commit="$commit$" />
<repository type="git" url="https://github.com/nanoFramework/nanoFramework.Hosting" commit="$commit$" />
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
<description>This package includes the nanoframework.Hosting assembly for .NET nanoFramework C# projects.</description>
<tags>nanoFramework C# csharp netmf netnf hosting</tags>
@ -22,11 +22,11 @@
</dependencies>
</metadata>
<files>
<file src="WebSockets.Server\bin\Release\nanoframework.Hosting.dll" target="lib\nanoframework.Hosting.dll" />
<file src="WebSockets.Server\bin\Release\nanoframework.Hosting.pdb" target="lib\nanoframework.Hosting.pdb" />
<file src="WebSockets.Server\bin\Release\nanoframework.Hosting.pdbx" target="lib\nanoframework.Hosting.pdbx" />
<file src="WebSockets.Server\bin\Release\nanoframework.Hosting.pe" target="lib\nanoframework.Hosting.pe" />
<file src="WebSockets.Server\bin\Release\nanoframework.Hosting.xml" target="lib\nanoframework.Hosting.xml" />
<file src="WebSockets.Server\bin\Release\nanoFramework.Hosting.dll" target="lib\nanoFramework.Hosting.dll" />
<file src="WebSockets.Server\bin\Release\nanoFramework.Hosting.pdb" target="lib\nanoFramework.Hosting.pdb" />
<file src="WebSockets.Server\bin\Release\nanoFramework.Hosting.pdbx" target="lib\nanoFramework.Hosting.pdbx" />
<file src="WebSockets.Server\bin\Release\nanoFramework.Hosting.pe" target="lib\nanoFramework.Hosting.pe" />
<file src="WebSockets.Server\bin\Release\nanoFramework.Hosting.xml" target="lib\nanoFramework.Hosting.xml" />
<file src="assets\readme.txt" target="" />
<file src="README.md" target="docs\" />
<file src="assets\nf-logo.png" target="images\" />

Просмотреть файл

@ -3,7 +3,6 @@ using System;
using nanoFramework.TestFramework;
using nanoFramework.Hosting.UnitTests.Fakes;
namespace nanoFramework.Hosting.UnitTests
{
[TestClass]

Просмотреть файл

@ -16,7 +16,7 @@ namespace nanoFramework.Hosting.UnitTests
}
[TestMethod]
public void StopWithoutCancellation()
public void StartStopHostBuilder()
{
var builder = new HostBuilder();
using (var host = builder.Build())
@ -29,8 +29,7 @@ namespace nanoFramework.Hosting.UnitTests
[TestMethod]
public void DefaultServicesAreAvailable()
{
using (var host = new HostBuilder()
.Build())
using (var host = new HostBuilder().Build())
{
Assert.NotNull(host.Services.GetRequiredService(typeof(IHost)));
Assert.NotNull(host.Services.GetRequiredService(typeof(IHostBuilder)));

Просмотреть файл

@ -64,7 +64,7 @@
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- MANUAL UPDATE HERE -->
<Import Project="..\packages\nanoFramework.TestFramework.1.0.205\build\nanoFramework.TestFramework.targets" Condition="Exists('..\packages\nanoFramework.TestFramework.1.0.205\build\nanoFramework.TestFramework.targets')" />
<Import Project="..\packages\nanoFramework.TestFramework.1.0.209\build\nanoFramework.TestFramework.targets" Condition="Exists('..\packages\nanoFramework.TestFramework.1.0.209\build\nanoFramework.TestFramework.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
@ -74,6 +74,6 @@
<PropertyGroup>
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
</PropertyGroup>
<Warning Condition="!Exists('..\packages\nanoFramework.TestFramework.1.0.205\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
<Warning Condition="!Exists('..\packages\nanoFramework.TestFramework.1.0.209\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
</Target>
</Project>