> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/framework/core/services/locating.md).

# Service Resolution and Ownership

Service resolution scope and lifecycle, illustrated by dependencies between Discussions services.

Before resolving a service, establish which container provides it, which contract identifies it, and who owns its disposal. Discussions services usually resolve dependencies from their own ServiceProvider or from a template provider’s Services.

## Resolve Related Business Services

Source: [src/Services/ThreadService.cs](https://github.com/Zongsoft/Zongsoft.Discussions/blob/main/src/Services/ThreadService.cs#L54) (excerpt; see source for context).

{% code title="ThreadService.cs" %}

```csharp
public PostService Posting
{
	get
	{
		if(_posting == null)
			_posting = this.ServiceProvider.ResolveRequired<PostService>();

		return _posting;
	}
}
#endregion
```

{% endcode %}

ThreadService caches the PostService reference returned by the container. It neither creates a new service scope nor disposes the service after each business operation. A container alone cannot prevent state leaking between requests if a shared service stores mutable request state.

## Template Providers Use the Same Business Services

Source: [src/Features/Archiving/UserDataTemplateModelProvider.cs](https://github.com/Zongsoft/Zongsoft.Discussions/blob/main/src/Features/Archiving/UserDataTemplateModelProvider.cs#L61) (excerpt; see source for context).

{% code title="UserDataTemplateModelProvider.cs" %}

```csharp
var data = argument switch
{
	string key => this.Services.ResolveRequired<UserService>().Get(key, schema),
	IModel model => this.Services.ResolveRequired<UserService>().Select(Criteria.Transform(model), schema),
	_ => this.Services.ResolveRequired<UserService>().Select(null, schema),
};
```

{% endcode %}

Both key-based lookups and condition-based selections go through UserService. Template assembly reuses service-layer constraints instead of opening its own database connection. Callers must still check the user’s filtering scope and export permissions.

## Module Dependencies and Missing Services

MessageSendCommand’s ServiceDependency specifies Discussions as its Provider; the module obtains its accessor by name. Naming, attribute registration, assembly scanning, and plugin mounting are separate steps. Missing any one of them can cause service resolution to fail.

The framework also supports service resolution by name, matching parameters, and tags. Discussions does not demonstrate every form. See [Services](/documentation/en/framework/core/services.md) for the complete reference; there is no need to invent additional forum services just to demonstrate every overload.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/framework/core/services/locating.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
