Skip to content

Commit

Permalink
Maui 中使用统一的 IServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jun 6, 2022
1 parent 11c1320 commit 12393aa
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 267 deletions.
11 changes: 11 additions & 0 deletions src/Common.CoreLib/DI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public static partial class DI

internal static bool IsConfigured => value != null;

public static void Dispose()
{
if (value is IDisposable disposable) disposable.Dispose();
}

public static async ValueTask DisposeAsync()
{
if (value is IAsyncDisposable disposable) await disposable.DisposeAsync();
else Dispose();
}

/// <summary>
/// 初始化依赖注入服务组(通过配置服务项的方式)
/// </summary>
Expand Down
44 changes: 38 additions & 6 deletions src/ST.Client.CommandLine/CommandLineHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ namespace System.Application.CommandLine;
/// <summary>
/// 命令行工具(Command Line Tools/CLT)
/// </summary>
public abstract class CommandLineHost
public abstract class CommandLineHost : IDisposable
{
public const string command_main = "main";
bool disposedValue;

protected abstract IApplication.IDesktopProgramHost Host { get; }

protected abstract void ConfigureServices(IApplication.IStartupArgs args, DILevel level);
protected abstract void ConfigureServices(DILevel level);

#if StartWatchTrace
protected abstract void StartWatchTraceRecord(string? mark = null, bool dispose = false);
Expand Down Expand Up @@ -44,7 +45,7 @@ void MainHandler_(Action? onInitStartuped)
#if StartWatchTrace
StartWatchTraceRecord("ProcessCheck");
#endif
ConfigureServices(Host, Host.IsMainProcess ? DILevel.MainProcess : DILevel.Min);
ConfigureServices(Host.IsMainProcess ? DILevel.MainProcess : DILevel.Min);
#if StartWatchTrace
StartWatchTraceRecord("Startup.Init");
#endif
Expand Down Expand Up @@ -166,7 +167,7 @@ void MainHandlerByCLT_(Action? onInitStartuped)
{
if (!string.IsNullOrEmpty(account))
{
ConfigureServices(Host, DILevel.Steam);
ConfigureServices(DILevel.Steam);
var steamService = ISteamService.Instance;
Expand Down Expand Up @@ -198,13 +199,13 @@ void MainHandlerByCLT_(Action? onInitStartuped)
if (id <= 0) return;
if (!silence)
{
ConfigureServices(Host, DILevel.GUI | DILevel.Steam | DILevel.HttpClientFactory);
ConfigureServices(DILevel.GUI | DILevel.Steam | DILevel.HttpClientFactory);
IViewModelManager.Instance.InitUnlockAchievement(id);
StartApplication(args);
}
else
{
ConfigureServices(Host, DILevel.Steam);
ConfigureServices(DILevel.Steam);
SteamConnectService.Current.Initialize(id);
TaskCompletionSource tcs = new();
await tcs.Task;
Expand All @@ -220,4 +221,35 @@ void MainHandlerByCLT_(Action? onInitStartuped)
var r = rootCommand.InvokeAsync(args).GetAwaiter().GetResult();
return r;
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: 释放托管状态(托管对象)
AppInstance?.Dispose();
Application?.Dispose();
}

// TODO: 释放未托管的资源(未托管的对象)并重写终结器
// TODO: 将大型字段设置为 null
disposedValue = true;
}
}

// // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
// ~CommandLineHost()
// {
// // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
// Dispose(disposing: false);
// }

public void Dispose()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
19 changes: 1 addition & 18 deletions src/ST.Client.Desktop.Avalonia.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,8 @@ static int Main(string[] args)
}
finally
{
try
{
host.Application?.Dispose();
}
catch
{

}
try
{
host.AppInstance?.Dispose();
}
catch
{

}
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
LogManager.Shutdown();
ArchiSteamFarm.LogManager.Shutdown();
DI.Dispose();
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/ST.Client.Desktop.Avalonia.App/Program.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ partial class ProgramHost : IApplication.IDesktopProgramHost

public bool IsTrayProcess { get; set; }

public void ConfigureServices(DILevel level) => Program.ConfigureServices(this, level);
void IApplication.IProgramHost.ConfigureServices(DILevel level) => ConfigureServices(level);

public void InitVisualStudioAppCenterSDK()
{
Expand All @@ -57,8 +57,19 @@ partial class ProgramHost : CommandLineHost
{
protected override IApplication.IDesktopProgramHost Host => this;

protected override void ConfigureServices(IApplication.IStartupArgs args, DILevel level)
=> Program.ConfigureServices(args, level);
public Action<DILevel>? ConfigureServicesDelegate { get; set; }

protected override void ConfigureServices(DILevel level)
{
if (ConfigureServicesDelegate != null)
{
ConfigureServicesDelegate.Invoke(level);
}
else
{
Program.ConfigureServices(this, level);
}
}

#if StartWatchTrace
protected override void StartWatchTraceRecord(string? mark = null, bool dispose = false)
Expand All @@ -67,7 +78,7 @@ protected override void StartWatchTraceRecord(string? mark = null, bool dispose

protected override void StartApplication(string[] args) =>
#if MAUI
StartMauiApp();
StartMauiApp(args);
#else
StartAvaloniaApp(args);
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using System.Application.UI;
using System;
using System.Logging;
using System.Application.UI;
using Microsoft.Extensions.Logging;
using _ThisAssembly = System.Properties.ThisAssembly;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -35,7 +36,44 @@ public static IServiceCollection AddGeneralLogging(this IServiceCollection servi
{
o.MinLevel = minLevel;
});
services.AddSingleton(new NLogDisposable());
return services;
}

sealed class NLogDisposable : IDisposable
{
bool disposedValue;

private void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: 释放托管状态(托管对象)
NLog.LogManager.Shutdown();
ArchiSteamFarm.LogManager.Shutdown();
}

// TODO: 释放未托管的资源(未托管的对象)并重写终结器
// TODO: 将大型字段设置为 null
disposedValue = true;
}
}

// // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
// ~LogManagerShutdown()
// {
// // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
// Dispose(disposing: false);
// }

public void Dispose()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
}
Loading

0 comments on commit 12393aa

Please sign in to comment.