-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
104 additions
and
26 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
uid: generic-host | ||
--- | ||
# Using Generic Host | ||
|
||
The McMaster.Extensions.Hosting.CommandLine package provides support for integrating command line parsing with | ||
.NET's [generic host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host). | ||
|
||
## Get started | ||
|
||
To get started, install the `McMaster.Extensions.Hosting.CommandLine` package. | ||
The main usage for generic host is `RunCommandLineApplicationAsync<TApp>(args)`, where `TApp` is a class | ||
which will be bound to command line arguments and options using attributes and `CommandLineApplication.Execute<T>`. | ||
|
||
### Sample | ||
|
||
This minimal sample shows how to take advantage of generic host features, such as `IHostingEnvironment`, | ||
as well as command line parsing options with this library. | ||
|
||
[!code-csharp[](../../samples/generic-host/Program.cs)] | ||
|
||
## Dependency injection | ||
|
||
Generic host integration allows you to use the most current DI configuration approach indicated by the aspnet project. The basic approach starts by creating the builder: | ||
|
||
[!code-csharp[](../../samples/dependency-injection/generic-host/Program.cs?name=Program&range=26-26)] | ||
|
||
Then you can configure your features: | ||
|
||
[!code-csharp[](../../samples/dependency-injection/generic-host/Program.cs?name=Program&range=27-34)] | ||
|
||
And finally, run your program: | ||
|
||
[!code-csharp[](../../samples/dependency-injection/generic-host/Program.cs?name=Program&range=35-35)] | ||
|
||
Below is the full source code for the generic host services example. Notice that instance of `IGreeter` will be injected into the `Program` constructor thanks to the dependency injection. | ||
|
||
[!code-csharp[](../../samples/dependency-injection/custom/Program.cs?name=Program&highlight=32-32)] | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<LangVersion>7.3</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Hosting.CommandLine\McMaster.Extensions.Hosting.CommandLine.csproj" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
class Program | ||
{ | ||
static Task<int> Main(string[] args) | ||
=> new HostBuilder() | ||
.RunCommandLineApplicationAsync<Program>(args); | ||
|
||
[Option] | ||
public int Port { get; } = 8080; | ||
|
||
private IHostingEnvironment _env; | ||
|
||
public Program(IHostingEnvironment env) | ||
{ | ||
_env = env; | ||
} | ||
|
||
private void OnExecute() | ||
{ | ||
Console.WriteLine($"Starting on port {Port}, env = {_env.EnvironmentName}"); | ||
} | ||
} |
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
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
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