Skip to content

Commit

Permalink
Merge pull request #181 from Beyley/optional-service-dependency-injec…
Browse files Browse the repository at this point in the history
…tion

Dont throw a missing dependency exception if the parameter is nullable
  • Loading branch information
jvyden authored Sep 12, 2024
2 parents 99277fb + 79689bf commit 73ca848
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Bunkum.Core/BunkumServer.Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,16 @@ public partial class BunkumServer // Services
if (!info.ParameterType.IsAssignableTo(typeof(TInjected))) return null;

TInjected? injected = pool.FirstOrDefault(s => s!.GetType() == info.ParameterType);
if (injected == null)
throw new Exception($"Could not find {info.ParameterType}, which {typeof(TObject).Name} depends on.");
if (injected == null)
{
// NullabilityInfoContext isn't thread-safe, so it cant be re-used
// https://stackoverflow.com/a/72586919
// TODO: do benchmarks of this call to see if we should optimize
bool isNullable = new NullabilityInfoContext().Create(info).WriteState == NullabilityState.Nullable;

if(!isNullable)
throw new Exception($"Could not find {info.ParameterType}, which {typeof(TObject).Name} depends on.");
}

return injected;
}
Expand Down

0 comments on commit 73ca848

Please sign in to comment.