Skip to main content

Utilities

Is registered?

With the IsRegistered() function, you can find out whether a service is registered into the container or not.

It returns true only when the container has a registration with the given type (and name). It only checks the actual container's registrations. For every cases, you should use the CanResolve() method.

bool isIJobRegistered = container.IsRegistered<IJob>();

Can resolve?

There might be cases when you are more interested in whether a service is resolvable from the container's actual state rather than finding out whether it's registered.

CanResolve() returns true only when at least one of the following is true:

  • The requested type is registered in the current or one of the parent containers.
  • The requested type is a closed generic type, and its open generic definition is registered.
  • The requested type is a wrapper (IEnumerable<>, Lazy<>, Func<>, KeyValuePair<,>, ReadOnlyKeyValue<,>, Metadata<,>, ValueTuple<,>, or Tuple<,>), and the underlying type is registered.
  • The requested type is not registered, but it's resolvable, and unknown type resolution is enabled.
bool isIJobResolvable = container.CanResolve<IJob>();

Get all mappings

You can get all registrations in a key-value pair collection (where the key is the service type and the value is the actual registration) by calling the .GetRegistrationMappings() method.

IEnumerable<KeyValuePair<Type, ServiceRegistration>> mappings = 
container.GetRegistrationMappings();

Registration diagnostics

You can get a much more readable version of the registration mappings by calling the .GetRegistrationDiagnostics() method.

RegistrationDiagnosticsInfo has an overridden .ToString() method that returns the mapping details formatted in a human-readable form.

container.Register<IJob, DbBackup>("DbBackupJob");
container.Register(typeof(IEventHandler<>), typeof(EventHandler<>));

IEnumerable<RegistrationDiagnosticsInfo> diagnostics =
container.GetRegistrationDiagnostics();

diagnostics.ForEach(Console.WriteLine);
// output:
// IJob => DbBackup, name: DbBackupJob
// IEventHandler<> => EventHandler<>, name: null