Skip to main content

IServiceCollectionExt

Trait IServiceCollectionExt 

Source
pub trait IServiceCollectionExt: Sized {
Show 15 methods // Required methods fn add_mediator(self) -> Self; fn add_request_endpoints(self) -> Self; fn add_controllers(self) -> Self; fn add_middleware<T>(self) -> Self where T: IMiddleware + Default + Send + Sync + 'static; fn add_typed_pipeline<B, Req, Rsp>(self) -> Self where B: IPipelineBehavior<Req, Rsp> + Default + 'static, Req: Send + 'static, Rsp: Send + 'static; fn add_typed_pipeline_with<F, Req, Rsp>(self, factory: F) -> Self where F: Fn(&dyn IServiceResolver) -> Arc<dyn IPipelineBehavior<Req, Rsp>> + Send + Sync + 'static, Req: Send + 'static, Rsp: Send + 'static; fn add_hosted_service<T>(self) -> Self where T: IHostedService + Default + Send + Sync + 'static; fn add_hosted_service_with<T, F>(self, factory: F) -> Self where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static; fn add_hosted_service_ordered<T>(self, order: i32) -> Self where T: IHostedService + Default + Send + Sync + 'static; fn add_hosted_service_with_ordered<T, F>( self, order: i32, factory: F, ) -> Self where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static; fn add_hosted_service_if<T>(self, enabled: bool) -> Self where T: IHostedService + Default + Send + Sync + 'static; fn add_hosted_service_with_if<T, F>(self, enabled: bool, factory: F) -> Self where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static; fn add_background_service<B>(self) -> Self where B: BackgroundService + Default; fn add_background_service_with<B, F>(self, factory: F) -> Self where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static; fn add_background_service_with_if<B, F>( self, enabled: bool, factory: F, ) -> Self where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static;
}
Expand description

Extension methods for rust_dix::ServiceCollection to enable framework-level service registration patterns.

Required Methods§

Source

fn add_mediator(self) -> Self

Register the mediator in the DI container.

The actual IRequestHandler and IEventHandler implementations are registered via #[rust_dix::module] blocks (re-exported as webx::module).

Source

fn add_request_endpoints(self) -> Self

Signal that endpoints should be auto-discovered from #[get] / #[endpoint] annotations at compile time. The actual route table is built during Host::build() by scanning the inventory for RouteEntry items.

Source

fn add_controllers(self) -> Self

Register controller types scoped per request.

Source

fn add_middleware<T>(self) -> Self
where T: IMiddleware + Default + Send + Sync + 'static,

Register a middleware type in the DI container as a singleton.

Source

fn add_typed_pipeline<B, Req, Rsp>(self) -> Self
where B: IPipelineBehavior<Req, Rsp> + Default + 'static, Req: Send + 'static, Rsp: Send + 'static,

Register a typed pipeline behavior in the DI container as a singleton.

B declares the request/response types by implementing IPipelineBehavior<Req, Rsp>; register those same types here:

ⓘ
#[derive(Default)]
struct AuditBehavior;

#[async_trait::async_trait]
impl IPipelineBehavior<CreateOrderRequest, OrderView> for AuditBehavior {
    async fn handle(&self, req: CreateOrderRequest, next: Next<CreateOrderRequest, OrderView>)
        -> ActionResult<OrderView> { next.run(req).await }
}

svc.add_typed_pipeline::<AuditBehavior, CreateOrderRequest, OrderView>();

The behavior only runs for that request/response pair. Short-circuiting is type-checked: return Rsp without calling Next::run.

Source

fn add_typed_pipeline_with<F, Req, Rsp>(self, factory: F) -> Self
where F: Fn(&dyn IServiceResolver) -> Arc<dyn IPipelineBehavior<Req, Rsp>> + Send + Sync + 'static, Req: Send + 'static, Rsp: Send + 'static,

Register a typed pipeline behavior built from a factory (singleton).

The factory receives the DI resolver at construction time, so the behavior can take constructor dependencies. It runs once per provider; the resolver is not available inside handle.

Source

fn add_hosted_service<T>(self) -> Self
where T: IHostedService + Default + Send + Sync + 'static,

Register a hosted service in the DI container as a singleton.

Hosted services are started when the host starts (before accepting requests) and stopped during graceful shutdown.

Analogous to ASP.NET Core’s AddHostedService<T>().

§Example
ⓘ
Host::builder()
    .register(|svc| {
        svc.add_hosted_service::<DbInitService>()
    })
    .build()
    .run()
    .await
Source

fn add_hosted_service_with<T, F>(self, factory: F) -> Self
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Register a hosted service built by factory (singleton).

Unlike add_hosted_service this does not require Default: the factory receives the DI resolver when the service is constructed, so configuration and constructor dependencies are injected normally.

Analogous to ASP.NET Core’s AddHostedService(sp => new T(sp.GetRequiredService<...>())).

ⓘ
svc
    .add_options::<OutboxOptions>("Outbox")
    .add_hosted_service_with(|resolver| {
        let options = resolve::<OutboxOptions>(resolver);
        Arc::new(OutboxConsumer::new(options))
    })
Source

fn add_hosted_service_ordered<T>(self, order: i32) -> Self
where T: IHostedService + Default + Send + Sync + 'static,

Register a hosted service with an explicit start/stop order.

Lower order starts first and stops last; equal orders keep the registration order. See IHostedService::order.

Source

fn add_hosted_service_with_ordered<T, F>(self, order: i32, factory: F) -> Self
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Register a factory-built hosted service with an explicit start/stop order.

Source

fn add_hosted_service_if<T>(self, enabled: bool) -> Self
where T: IHostedService + Default + Send + Sync + 'static,

Register a hosted service only when enabled is true.

Conditions belong to configuration (if options.Outbox.Enabled), so a deployment can switch a migration/consumer off without a code change:

ⓘ
svc.add_hosted_service_if::<SeedService>(options.seed.enabled)
Source

fn add_hosted_service_with_if<T, F>(self, enabled: bool, factory: F) -> Self
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Register a factory-built hosted service only when enabled is true.

Source

fn add_background_service<B>(self) -> Self

Register a BackgroundService worker (singleton, Default constructor).

The host starts the worker without blocking startup and cancels its CancellationToken on shutdown; business code only implements execute.

Source

fn add_background_service_with<B, F>(self, factory: F) -> Self
where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static,

Register a factory-built BackgroundService worker (singleton).

The factory receives the DI resolver at construction time, so the worker can take configuration and Arc<dyn ScopeFactory> for its per-tick scopes — the pattern that used to require a hand-rolled start/stop/JoinHandle hosted service.

Source

fn add_background_service_with_if<B, F>(self, enabled: bool, factory: F) -> Self
where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static,

Register a factory-built background worker only when enabled is true.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl IServiceCollectionExt for ServiceCollection

Source§

fn add_mediator(self) -> ServiceCollection

Source§

fn add_request_endpoints(self) -> ServiceCollection

Source§

fn add_controllers(self) -> ServiceCollection

Source§

fn add_middleware<T>(self) -> ServiceCollection
where T: IMiddleware + Default + Send + Sync + 'static,

Source§

fn add_typed_pipeline<B, Req, Rsp>(self) -> ServiceCollection
where B: IPipelineBehavior<Req, Rsp> + Default + 'static, Req: Send + 'static, Rsp: Send + 'static,

Source§

fn add_typed_pipeline_with<F, Req, Rsp>(self, factory: F) -> ServiceCollection
where F: Fn(&dyn IServiceResolver) -> Arc<dyn IPipelineBehavior<Req, Rsp>> + Send + Sync + 'static, Req: Send + 'static, Rsp: Send + 'static,

Source§

fn add_hosted_service<T>(self) -> ServiceCollection
where T: IHostedService + Default + Send + Sync + 'static,

Source§

fn add_hosted_service_with<T, F>(self, factory: F) -> ServiceCollection
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Source§

fn add_hosted_service_ordered<T>(self, order: i32) -> ServiceCollection
where T: IHostedService + Default + Send + Sync + 'static,

Source§

fn add_hosted_service_with_ordered<T, F>( self, order: i32, factory: F, ) -> ServiceCollection
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Source§

fn add_hosted_service_if<T>(self, enabled: bool) -> ServiceCollection
where T: IHostedService + Default + Send + Sync + 'static,

Source§

fn add_hosted_service_with_if<T, F>( self, enabled: bool, factory: F, ) -> ServiceCollection
where T: IHostedService + 'static, F: Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,

Source§

fn add_background_service<B>(self) -> ServiceCollection

Source§

fn add_background_service_with<B, F>(self, factory: F) -> ServiceCollection
where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static,

Source§

fn add_background_service_with_if<B, F>( self, enabled: bool, factory: F, ) -> ServiceCollection
where B: BackgroundService, F: Fn(&dyn IServiceResolver) -> Arc<B> + Send + Sync + 'static,

Implementors§