Skip to main content

Register Instance

Register Instance

// ...
var obj = new ServiceA();
// ...

builder.RegisterInstance(obj);
note

RegisterInstance always has a Singleton lifetime, so it has no arguments.

It can resolve like this:

class ClassA
{
public ClassA(ServiceA serviceA) { /* ... */ }
}
note

Instances registered with RegisterInstance are not managed by the container.

  • Dispose will not be executed automatically.
  • Method Injection will not be executed automatically

If you want the container to manage the created instances, consider using the following instead

Register Instance as Interface

Use As* declarations.

builder.RegisterInstance<IInputPort>(serviceA);

builder.RegisterInstance(serviceA)
.As<IServiceA, IInputPort>();

builder.RegisterInstance(serviceA)
.AsImplementedInterfaces();