-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
250 additions
and
8 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
12 changes: 10 additions & 2 deletions
12
Runtime/Bsr.Microsoft.Extensions.Hosting.Unity.Runtime.asmdef
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 |
---|---|---|
@@ -1,14 +1,22 @@ | ||
{ | ||
"name": "Microsoft.Extensions.Hosting.Unity", | ||
"rootNamespace": "", | ||
"references": [], | ||
"references": [ | ||
"GUID:f51ebe6a0ceec4240a699833d6309b23" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"versionDefines": [ | ||
{ | ||
"name": "com.cysharp.unitask", | ||
"expression": "", | ||
"define": "UNITASK_ENABLED" | ||
} | ||
], | ||
"noEngineReferences": false | ||
} |
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,88 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.EventLog; | ||
using UnityEngine; | ||
using UnityEngine.Scripting; | ||
|
||
namespace Microsoft.Extensions.Hosting.Unity | ||
{ | ||
[Preserve] | ||
public static class UnityHost | ||
{ | ||
[Preserve] | ||
public static IHostBuilder CreateDefaultBuilder() => CreateDefaultBuilder(args: null); | ||
|
||
[Preserve] | ||
public static IHostBuilder CreateDefaultBuilder(string[] args) | ||
{ | ||
HostBuilder builder = new(); | ||
return builder.ConfigureDefaults(args); | ||
} | ||
|
||
private static IHostBuilder ConfigureDefaults(this IHostBuilder builder, string[] args) | ||
{ | ||
builder.UseContentRoot(Application.dataPath); | ||
builder.ConfigureHostConfiguration(config => | ||
{ | ||
config.AddEnvironmentVariables(prefix: "DOTNET_"); | ||
if (args is { Length: > 0 }) | ||
{ | ||
config.AddCommandLine(args); | ||
} | ||
}); | ||
|
||
builder.ConfigureAppConfiguration((hostingContext, config) => | ||
{ | ||
IHostEnvironment env = hostingContext.HostingEnvironment; | ||
config.AddJsonFile("appsettings.json", optional: true) | ||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); | ||
if (env.IsDevelopment() && env.ApplicationName is { Length: > 0 }) | ||
{ | ||
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); | ||
if (appAssembly is not null) | ||
{ | ||
config.AddUserSecrets(appAssembly, optional: true); | ||
} | ||
} | ||
config.AddEnvironmentVariables(); | ||
if (args is { Length: > 0 }) | ||
{ | ||
config.AddCommandLine(args); | ||
} | ||
}) | ||
.ConfigureLogging((hostingContext, logging) => | ||
{ | ||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); | ||
logging.AddDebug(); | ||
logging.AddEventSourceLogger(); | ||
logging.Configure(options => | ||
{ | ||
options.ActivityTrackingOptions = | ||
ActivityTrackingOptions.SpanId | | ||
ActivityTrackingOptions.TraceId | | ||
ActivityTrackingOptions.ParentId; | ||
}); | ||
}) | ||
.UseDefaultServiceProvider((context, options) => | ||
{ | ||
bool isDevelopment = context.HostingEnvironment.IsDevelopment(); | ||
options.ValidateScopes = isDevelopment; | ||
options.ValidateOnBuild = isDevelopment; | ||
}); | ||
|
||
builder.ConfigureServices(services => services.AddSingleton<IHostLifetime, UnityLifetime>()); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,111 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
using UnityEngine; | ||
using ILogger = Microsoft.Extensions.Logging.ILogger; | ||
|
||
namespace Microsoft.Extensions.Hosting.Unity | ||
{ | ||
public class UnityLifetime : IHostLifetime, IDisposable | ||
{ | ||
private CancellationTokenRegistration _applicationStartedRegistration; | ||
private CancellationTokenRegistration _applicationStoppingRegistration; | ||
|
||
private readonly ManualResetEvent _shutdownBlock = new ManualResetEvent(false); | ||
|
||
public UnityLifetime(IOptions<ConsoleLifetimeOptions> options, IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, IOptions<HostOptions> hostOptions) | ||
: this(options, environment, applicationLifetime, hostOptions, NullLoggerFactory.Instance) | ||
{ | ||
} | ||
|
||
public UnityLifetime(IOptions<ConsoleLifetimeOptions> options, IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, IOptions<HostOptions> hostOptions, ILoggerFactory loggerFactory) | ||
{ | ||
Options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
Environment = environment ?? throw new ArgumentNullException(nameof(environment)); | ||
ApplicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime)); | ||
HostOptions = hostOptions?.Value ?? throw new ArgumentNullException(nameof(hostOptions)); | ||
Logger = loggerFactory.CreateLogger("Microsoft.Hosting.Lifetime"); | ||
} | ||
|
||
private ConsoleLifetimeOptions Options { get; } | ||
|
||
private IHostEnvironment Environment { get; } | ||
|
||
private IHostApplicationLifetime ApplicationLifetime { get; } | ||
|
||
private HostOptions HostOptions { get; } | ||
|
||
private ILogger Logger { get; } | ||
|
||
public Task WaitForStartAsync(CancellationToken cancellationToken) | ||
{ | ||
if (!Options.SuppressStatusMessages) | ||
{ | ||
_applicationStartedRegistration = ApplicationLifetime.ApplicationStarted.Register(state => { ((UnityLifetime)state).OnApplicationStarted(); }, | ||
this); | ||
_applicationStoppingRegistration = ApplicationLifetime.ApplicationStopping.Register(state => { ((UnityLifetime)state).OnApplicationStopping(); }, | ||
this); | ||
} | ||
|
||
RegisterShutdownHandlers(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void RegisterShutdownHandlers() | ||
{ | ||
Application.quitting += OnProcessExit; | ||
} | ||
|
||
private void OnApplicationStarted() | ||
{ | ||
Logger.LogInformation("Unity host started"); | ||
Logger.LogInformation("Hosting environment: {envName}", Environment.EnvironmentName); | ||
Logger.LogInformation("Content root path: {contentRoot}", Environment.ContentRootPath); | ||
} | ||
|
||
private void OnProcessExit() | ||
{ | ||
ApplicationLifetime.StopApplication(); | ||
if (!_shutdownBlock.WaitOne(HostOptions.ShutdownTimeout)) | ||
{ | ||
Logger.LogInformation("Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks"); | ||
} | ||
|
||
// wait one more time after the above log message, but only for ShutdownTimeout, so it doesn't hang forever | ||
_shutdownBlock.WaitOne(HostOptions.ShutdownTimeout); | ||
|
||
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code. | ||
// Suppress that since we shut down gracefully. https://github.com/dotnet/aspnetcore/issues/6526 | ||
System.Environment.ExitCode = 0; | ||
} | ||
|
||
private void OnApplicationStopping() | ||
{ | ||
Logger.LogInformation("Unity host is shutting down..."); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
// There's nothing to do here | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UnregisterShutdownHandlers(); | ||
|
||
_applicationStartedRegistration.Dispose(); | ||
_applicationStoppingRegistration.Dispose(); | ||
} | ||
|
||
private void UnregisterShutdownHandlers() | ||
{ | ||
_shutdownBlock.Set(); | ||
Application.quitting -= OnProcessExit; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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