RpcHostService
A service whose methods may return other @KsService sub-services.
Extend this instead of RpcService when any @KsMethod returns a type annotated with @KsService. The compiler plugin enforces this at compile time and produces a clear error if the wrong tier is used.
HTTP transports can host RpcHostService instances (sub-service outputs are multiplexed over the same connection), but cannot host RpcBidiService.
Samples
// A @KsMethod can return another @KsService type. The returned service
// is hosted as a sub-service on the same connection, with its own
// channel id managed by the framework.
val factory = object : WorkerFactory {
override suspend fun createWorker(name: String): ScopedWorker = object : ScopedWorker {
override suspend fun execute(command: String): String = "[$name] executed: $command"
}
}
// Serialize and use through a stub -- sub-services work transparently.
val env = ksrpcEnvironment { }
val serialized = factory.serialized(env)
val stub = serialized.toStub<WorkerFactory, String>()
val worker = stub.createWorker("build")
val result = worker.execute("compile")Content copied to clipboard