Transient Service

Transient service is a service type in Blazor Server DI. Transient services are better for:

  1. Lightweight operations.
  2. No state required.
Download the example for a working example containing the code snippets in this guide.

Creating a transient service

  1. Navigate to the Data folder and create the TransientGuidProviderService.cs class:
using System;

namespace DependencyExampleBlazorSchool.Data
{
    public class TransientGuidProviderService
    {
        public Guid Id { get; set; } = Guid.NewGuid();
    }
}
  1. Navigate to Startup.cs and register the service as a transient service.
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTransient<TransientGuidProviderService>();
}
For injecting and using a service, please refer to Dependency Injection.

Lifetime

Transient services are created every time you inject them. There is also no state in transient service. They will use more memory and resources and can have a negative impact on performance.

In the example provided at the beginning of this context, you will see a transient service being injected 2 times and their values are different.

Transient Service Blazor Server

BLAZOR SCHOOL
Designed and built with care by our dedicated team, with contributions from a supportive community. We strive to provide the best learning experience for our users.
Docs licensed CC-BY-SA-4.0
Copyright © 2021-2025 Blazor School
An unhandled error has occurred. Reload 🗙