I downloaded a ktor setup with koin enabled, but I can't seem to get it work. Not sure if I am doing something wrong or not, as it on the surface looks pretty OK to me.
so my Applikation.kt
file looks like this
fun main(args: Array<String>) {
io.ktor.serverty.EngineMain.main(args)
}
fun Application.module() {
configureFrameworks()
configureSerialization()
configureDatabases()
configureMonitoring()
configureSecurity()
configureRouting()
configureUserRoutes()
}
the configure frameworks function:
fun Application.configureFrameworks() {
install(Koin) {
slf4jLogger()
modules(module {
single<Connection> {
connectToPostgres(embedded = true)
}
single<UserService> {
UserService(get())
}
})
}
}
and configureUserRoutes()
fun Application.configureUserRoutes() {
routing {
val koin = getKoin()
val userService: UserService = koin.get()
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}
have I missunderstood something or shouldn't that inject of UserService
work?
I am just getting this error
Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface io.ktor.server.routing.Routing, but class was expected
at .koin.ktor.ext.RoutingExtKt.getKoin(RoutingExt.kt:74)
I downloaded a ktor setup with koin enabled, but I can't seem to get it work. Not sure if I am doing something wrong or not, as it on the surface looks pretty OK to me.
so my Applikation.kt
file looks like this
fun main(args: Array<String>) {
io.ktor.serverty.EngineMain.main(args)
}
fun Application.module() {
configureFrameworks()
configureSerialization()
configureDatabases()
configureMonitoring()
configureSecurity()
configureRouting()
configureUserRoutes()
}
the configure frameworks function:
fun Application.configureFrameworks() {
install(Koin) {
slf4jLogger()
modules(module {
single<Connection> {
connectToPostgres(embedded = true)
}
single<UserService> {
UserService(get())
}
})
}
}
and configureUserRoutes()
fun Application.configureUserRoutes() {
routing {
val koin = getKoin()
val userService: UserService = koin.get()
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}
have I missunderstood something or shouldn't that inject of UserService
work?
I am just getting this error
Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface io.ktor.server.routing.Routing, but class was expected
at .koin.ktor.ext.RoutingExtKt.getKoin(RoutingExt.kt:74)
Share
asked Mar 6 at 20:38
munHungermunHunger
3,0617 gold badges43 silver badges73 bronze badges
1 Answer
Reset to default 0aha, so I can't use koin inside the routing scope
this works (but not entirely sure why I can't inject inside routing):
fun Application.configureUserRoutes() {
val koin = getKoin()
val userService: UserService = koin.get()
routing {
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}