Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException in ServiceGraph.AllInstances() #340

Closed
jwoodman510 opened this issue May 23, 2022 · 2 comments
Closed

NullReferenceException in ServiceGraph.AllInstances() #340

jwoodman510 opened this issue May 23, 2022 · 2 comments

Comments

@jwoodman510
Copy link
Contributor

jwoodman510 commented May 23, 2022

public IEnumerable<Instance> AllInstances() { return _families.Values.ToArray().SelectMany(x => x.All).ToArray(); }

NullReferenceException is thrown any time the above method is called when a value in _families.Values is null.

@jeremydmiller
Copy link
Member

@jwoodman510 How did you configure the container to make that happen?

@jwoodman510
Copy link
Contributor Author

I've inherited a legacy codebase with some extremely large service registries, and said registries are used in tests.
There is a test registry that removes some specific service registrations as some of the logic shouldn't run in the tests.
There is one test written to assert that the test registry is removing the services as expected (this is where the exception is thrown).

e.g.

class ClassA {}
class ClassB {}
interface IGenericService<T> {}
class ServiceA : IGenericService<ClassA> {}
class ServiceB : IGenericService<ClassB> {}

class AppRegistry : ServiceRegistry
{
  // a huge number of nested registries and services here

  For<IGenericService<ClassA>>().Use<ServiceA>();
  For<IGenericService<ClassB>>().Use<ServiceB>();

  // a huge number of nested registries and services here
}

class TestRegistry : ServiceRegistry
{
  IncludeRegistry<AppRegistry>();
  
  RemoveAll(x => x.ServiceType == typeof(IGenericService<>);
}

// the "registry" test
void EnsureServicesAreNotRegistered()
{
  var container = new Container(x => x.IncludeRegistry<TestRegistry>());

  // Exception thrown here
  var hasRegistrations = container.Model.HasRegistrationsFor(typeof(IGenericService<>));

  Assert.False(hasRegistrations);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants