Skip to content

Commit 15e3fb2

Browse files
authored
Merge pull request #92 from KuraiAndras/develop
8.0.0
2 parents 1c43890 + a7b4158 commit 15e3fb2

File tree

11 files changed

+41
-11
lines changed

11 files changed

+41
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 8.0.0
2+
- Unity lifetime clears scopes
3+
14
# 7.1.0
25
- Add appsettings file generation template buttons
36

MainProject/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<LangVersion>9.0</LangVersion>
44
<Nullable>enable</Nullable>
55

6-
<CurrentVersion>7.1.0</CurrentVersion>
6+
<CurrentVersion>8.0.0</CurrentVersion>
77

88
<Version>$(CurrentVersion)</Version>
99
<PakcageVersion>$(CurrentVersion)</PakcageVersion>

MainProject/Injecter/IScopeStore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public interface IScopeStore
77
IServiceScope CreateScope<T>(T owner) where T : notnull;
88
void DisposeScope<T>(T owner) where T : notnull;
99
IServiceScope? GetScope<T>(T owner) where T : notnull;
10+
void ClearAllScopes();
1011
}
1112
}

MainProject/Injecter/ScopeStore.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,15 @@ public void DisposeScope<T>(T owner) where T : notnull
4040

4141
_scopes.Remove(owner);
4242
}
43+
44+
public void ClearAllScopes()
45+
{
46+
foreach (var scope in _scopes)
47+
{
48+
scope.Value.Dispose();
49+
}
50+
51+
_scopes.Clear();
52+
}
4353
}
4454
}

UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/AppInstallerTemplateItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void OnQuitting()
5454
{
5555
Log.CloseAndFlush();
5656
57+
host.Dispose();
5758
host = null!;
5859
5960
Application.quitting -= OnQuitting;
@@ -137,6 +138,7 @@ void OnQuitting()
137138
{
138139
Log.CloseAndFlush();
139140
141+
host.Dispose();
140142
host = null!;
141143
142144
Application.quitting -= OnQuitting;

UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/Injecter.Hosting.Unity.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Injecter.Hosting.Unity",
3+
"rootNamespace": "",
34
"references": [
45
"Injecter.Unity"
56
],
@@ -16,7 +17,8 @@
1617
"Microsoft.Extensions.Configuration.Abstractions.dll",
1718
"Microsoft.Extensions.Configuration.EnvironmentVariables.dll",
1819
"Microsoft.Extensions.Configuration.CommandLine.dll",
19-
"Microsoft.Extensions.Configuration.Json.dll"
20+
"Microsoft.Extensions.Configuration.Json.dll",
21+
"Injecter.dll"
2022
],
2123
"autoReferenced": true,
2224
"defineConstraints": [],

UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/UnityLifetime.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,30 @@ namespace Injecter.Hosting.Unity
2121
public sealed class UnityLifetime : IHostLifetime, IDisposable
2222
{
2323
private readonly ManualResetEvent _shutdownBlock = new ManualResetEvent(false);
24+
private readonly IScopeStore _scopeStore;
25+
2426
private CancellationTokenRegistration _applicationStartedRegistration;
2527
private CancellationTokenRegistration _applicationStoppingRegistration;
2628

2729
public UnityLifetime(
30+
IScopeStore scopeStore,
2831
IOptions<UnityLifeTimeOptions> options,
2932
IHostEnvironment environment,
3033
IHostApplicationLifetime applicationLifetime,
3134
IOptions<HostOptions> hostOptions)
32-
: this(options, environment, applicationLifetime, hostOptions, NullLoggerFactory.Instance)
35+
: this(scopeStore, options, environment, applicationLifetime, hostOptions, NullLoggerFactory.Instance)
3336
{
3437
}
3538

3639
public UnityLifetime(
40+
IScopeStore scopeStore,
3741
IOptions<UnityLifeTimeOptions> options,
3842
IHostEnvironment environment,
3943
IHostApplicationLifetime applicationLifetime,
4044
IOptions<HostOptions> hostOptions,
4145
ILoggerFactory loggerFactory)
4246
{
47+
_scopeStore = scopeStore;
4348
Options = options?.Value ?? throw new ArgumentNullException(nameof(options));
4449
Environment = environment ?? throw new ArgumentNullException(nameof(environment));
4550
ApplicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
@@ -103,7 +108,11 @@ private void OnProcessExit(object sender, EventArgs e)
103108
System.Environment.ExitCode = 0;
104109
}
105110

106-
private void OnUnityQuitting() => ApplicationLifetime.StopApplication();
111+
private void OnUnityQuitting()
112+
{
113+
_scopeStore.ClearAllScopes();
114+
ApplicationLifetime.StopApplication();
115+
}
107116

108117
#if UNITY_EDITOR
109118
private void OnUnityPlayModeStateChanged(PlayModeStateChange state)

UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.injecter.hosting.unity",
3-
"version": "7.1.0",
3+
"version": "8.0.0",
44
"displayName": "Injecter.Hosting.Unity",
55
"license": "MIT",
66
"licensesUrl": "https://github.com/KuraiAndras/Injecter/blob/master/LICENSE.md",
@@ -13,6 +13,6 @@
1313
"url": "https://github.com/KuraiAndras/Injecter"
1414
},
1515
"dependencies": {
16-
"com.injecter.unity": "7.1.0"
16+
"com.injecter.unity": "8.0.0"
1717
}
1818
}

UnityProject/Injecter.Unity/Assets/Injecter.Unity/InjectedMonoBehaviour.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public abstract class InjectedMonoBehaviour : MonoBehaviour
1111
protected InjectedMonoBehaviour(bool createScopes = false) => _createScopes = createScopes;
1212

1313
protected IServiceScope? Scope { get; private set; }
14+
protected IScopeStore? ScopeStore { get; private set; }
1415

1516
protected virtual void Awake()
1617
{
@@ -21,15 +22,17 @@ protected virtual void Awake()
2122
}
2223

2324
Scope = CompositionRoot.ServiceProvider.GetRequiredService<IInjecter>().InjectIntoType(GetType(), this, _createScopes);
25+
ScopeStore = Scope?.ServiceProvider.GetService<IScopeStore>();
2426
}
2527

2628
protected virtual void OnDestroy()
2729
{
28-
if (Scope is null) return;
30+
if (Scope is null || ScopeStore is null) return;
2931

30-
Scope.ServiceProvider.GetRequiredService<IScopeStore>().DisposeScope(this);
32+
ScopeStore.DisposeScope(this);
3133

3234
Scope = null;
35+
ScopeStore = null;
3336
}
3437
}
3538
}

UnityProject/Injecter.Unity/Assets/Injecter.Unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.injecter.unity",
3-
"version": "7.1.0",
3+
"version": "8.0.0",
44
"displayName": "Injecter.Unity",
55
"license": "MIT",
66
"licensesUrl": "https://github.com/KuraiAndras/Injecter/blob/master/LICENSE.md",

0 commit comments

Comments
 (0)