Skip to content

Commit

Permalink
Add info on discovery and disabling auto loading
Browse files Browse the repository at this point in the history
  • Loading branch information
guardrex committed Dec 12, 2017
1 parent 9c04718 commit 76db1ec
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
19 changes: 19 additions & 0 deletions aspnetcore/hosting/ihostingstartup.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ ASPNETCORE\_HOSTINGSTARTUPASSEMBLIES

Only hosting startup assemblies are scanned for the `HostingStartupAttribute`. The assembly name of the implementation is provided in this environment variable. The sample app sets this value to `StartupDiagnostics`.

The value can also be set using the [Hosting Startup Assemblies](xref:fundamentals/hosting#hosting-startup-assemblies) host configuration setting.

DOTNET\_ADDITIONAL\_DEPS

The location of the implementation's *\*.deps.json* file.
Expand All @@ -119,6 +121,23 @@ The sample app sets this value to:

For examples of how to set environment variables for various operating systems, see [Working with multiple environments](xref:fundamentals/environments).

## Discover loaded hosting startup assemblies

To discover hosting startup assemblies loaded by the app or libraries added to the app, read the value of the [HostingStartupAssembliesKey](/dotnet/api/microsoft.aspnetcore.hosting.webhostdefaults.hostingstartupassemblieskey). The sample app reads the key into a `string` array and displays the result in the app's Index page:

[!code-csharp[Main](ihostingstartup/sample/HostingStartupSample/Pages/Index.cshtml.cs?name=snippet1&highlight=14-16)]

## Disable automatic loading of hosting startup assemblies

There are two ways to disable the automatic loading of hosting startup assemblies:

* Set the [Prevent Hosting Startup](xref:fundamentals/hosting#prevent-hosting-startup) host configuration setting.
* Set the `ASPNETCORE_preventHostingStartup` environment variable.

When either the host setting or the environment variable is set to `true` or `1`, hosting startup assemblies aren't automatically loaded. If both are set, the host setting controls the behavior.

It isn't currently possible to selectively disable a hosting startup assembly added by a library unless the library offers its own configuration option. Disabling hosting startup assemblies using the host setting or environment variable disables them globally and may disable several features of an app.

## Sample app

The [sample app](https://github.com/aspnet/Docs/tree/master/aspnetcore/hosting/ihostingstartup/sample/) ([how to download](xref:tutorials/index#how-to-download-a-sample)) uses `IHostingStartup` to create a diagnostics tool. The tool adds two middlewares to the app at startup that provide diagnostic information:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@
</div>
</div>
</div>

<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Loaded Hosting Startup Assemblies</h3>
</div>
<div class="panel-body">
<h2>Assemblies</h2>
<ul>
@foreach (var assembly in Model.LoadedHostingStartupAssemblies)
{
<li>@assembly</li>
}
</ul>
</div>
</div>
</div>
</div>


Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace HostingStartupSample.Pages
{
#region snippet1
public class IndexModel : PageModel
{
private readonly IConfiguration _config;

public IndexModel(IConfiguration config)
{
_config = config;
}

public string[] LoadedHostingStartupAssemblies { get; private set; }

public void OnGet()
{
LoadedHostingStartupAssemblies =
_config[WebHostDefaults.HostingStartupAssembliesKey]
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
}
}
#endregion
}

0 comments on commit 76db1ec

Please sign in to comment.