---
lang: en
title: 'API docs: core.application.service'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/core
permalink: /doc/en/lb4/apidocs.core.application.service.html
---

<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@loopback/core](./core.md) &gt; [Application](./core.application.md) &gt; [service](./core.application.service.md)

## Application.service() method

Add a service to this application.

**Signature:**

```typescript
service<S>(cls: ServiceOrProviderClass<S>, nameOrOptions?: string | ServiceOptions): Binding<S>;
```

## Parameters

<table><thead><tr><th>

Parameter


</th><th>

Type


</th><th>

Description


</th></tr></thead>
<tbody><tr><td markdown="1">

cls


</td><td markdown="1">

[ServiceOrProviderClass](./core.serviceorproviderclass.md)<!-- -->&lt;S&gt;


</td><td markdown="1">

The service or provider class


</td></tr>
<tr><td markdown="1">

nameOrOptions


</td><td markdown="1">

string \| [ServiceOptions](./core.serviceoptions.md)


</td><td markdown="1">

_(Optional)_


</td></tr>
</tbody></table>

**Returns:**

[Binding](./context.binding.md)<!-- -->&lt;S&gt;

## Example


```ts
// Define a class to be bound via ctx.toClass()
@injectable({scope: BindingScope.SINGLETON})
export class LogService {
  log(msg: string) {
    console.log(msg);
  }
}

// Define a class to be bound via ctx.toProvider()
import {v4 as uuidv4} from 'uuid';
export class UuidProvider implements Provider<string> {
  value() {
    return uuidv4();
  }
}

// Register the local services
app.service(LogService);
app.service(UuidProvider, 'uuid');

export class MyController {
  constructor(
    @inject('services.uuid') private uuid: string,
    @inject('services.LogService') private log: LogService,
  ) {
  }

  greet(name: string) {
    this.log(`Greet request ${this.uuid} received: ${name}`);
    return `${this.uuid}: ${name}`;
  }
}
```


