Open Tyrrrz opened 8 months ago
I don't think we can reliably have that implemented. Sure, for ClassicDesktopLifetime it would be straightforward. But not all lifeimes have even a concept of being closed. And embedding scenarios (i.e. - without lifetime) won't have that either.
Promising that Application is going to be disposed on some platforms, will create similar expectations about other platforms.
Could that be an option that is specifically configured for classic desktop lifetime then? Something like:
[STAThread]
public static int Main(string[] args) =>
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args, disposeApplication: true);
@Tyrrrz here's the recipe. You should use a custom app lifetime to hook your DI container. It's easy.
// Program.cs Main
BuildAvaloniaApp().Start(App.AppMain, args);
// App.axaml.cs
public static void AppMain(Application app, string[] commandLineArguments)
{
// build your DI container
[proprietary code omitted]
app.Run(ShutdownRequestCts.Token); // Run the UI event loop, observing a CancellationTokenSource that breaks the event loop
// dispose your DI container
[proprietary code omitted]
}
Based on my research, Avalonia does not have an opinionated strategy for DI. The previewer depends on having a 0-parameter constructor on all your ViewModels. So if you want to use the previewer with real data, constructor injection is out.
I use explicit injection on most of my view models, having set up the DI container as a static property on App. Then, in the ViewModels, I set up field initializers that access App. [DI container property] .Resolve(...)
The previewer depends on having a 0-parameter constructor on all your ViewModels. So if you want to use the previewer with real data, constructor injection is out.
You can use constructor injection if you implement an approach as the one described in https://github.com/AvaloniaUI/Avalonia/discussions/13743#discussioncomment-7686499, for example.
Is your feature request related to a problem? Please describe.
My implementation of
Application
has aDispose
method that disposes the innerServiceProvider
that contains various domain-specific services and view models.Application
is not disposed automatically by Avalonia. So I have to run the app like this:This is a bit clunky. It would be much nicer if the underlying plumbing code automatically checked if the produced
Application
instance implementsIDisposable
and executed it if so. TheAppBuilder
is responsible for instantiating the object, so it makes sense it would also be responsible for its cleanup.Describe the solution you'd like
I want the following code to execute
Dispose()
on my application automatically:Describe alternatives you've considered
The current alternative I use is described above. I also looked into implementing a custom Lifetime, but it looks like it's not very easy.
Additional context
Applications may need a dispose methods to release native resources that are not tied to a specific process (and as such won't be released automatically when the process exits), or when the application needs to reset some global state to its default or previous value. In my case, I need to reset monitor gamma to its initial state when the application exits.