-
Notifications
You must be signed in to change notification settings - Fork 0
Service Registration
santiago edited this page Jan 29, 2026
·
1 revision
The framework supports two ways to register services in your registry: simple registration and lambda registration.
Use simple registration when the service class can be instantiated automatically by the container. This works when all dependencies are registered services with type annotations:
from azfunc_boot import BaseServiceRegistry, DependencyContainer, register_service
class ServicesRegistry(BaseServiceRegistry):
def __init__(self, container: DependencyContainer):
self.container = container
super().__init__()
@register_service
def register_services(self):
self.container.add_scoped(ExampleService)
self.container.add_singleton(Configuration)
self.container.add_transient(SomeService)The container will automatically resolve dependencies using type annotations:
class ExampleService:
def __init__(self, repository: IUserRepository, logger: ILogger):
self.repository = repository
self.logger = loggerUse a lambda function when:
- The service implements an interface/abstraction
- The constructor requires primitive types (strings, numbers, etc.)
- You need custom instantiation logic
When registering a service that implements an interface:
@register_service
def register_services(self):
self.container.add_scoped(
IExampleService,
lambda: ExampleService(self.container.get_service(ExampleDisposableClient)),
)When your service constructor requires primitive types (strings, numbers, etc.):
@register_service
def register_services(self):
self.container.add_scoped(
ExampleService,
lambda: ExampleService(
connection_string="your-connection-string",
timeout=30,
client=self.container.get_service(ExampleDisposableClient)
),
)All registration methods support the three service lifetimes:
-
add_singleton()- Single instance for the application lifetime -
add_scoped()- New instance per scope (typically per request) -
add_transient()- New instance every time it's requested
See Service Lifetimes for more details.