闽公网安备 35020302035485号
Singleton(单例): 在整个应用程序的生命周期中共享一个服务实例。
public interface ITransientService
{
// 堆代码 duidaima.com
Guid GetOperationId();
}
public class TransientService : ITransientService
{
private readonly Guid _operationId;
public TransientService()
{
_operationId = Guid.NewGuid();
}
public Guid GetOperationId() => _operationId;
}
在 Startup.cs 中注册服务:public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ITransientService, TransientService>();
}
在控制器中使用该服务:[ApiController]
[Route("api/[controller]")]
public class ExampleController : ControllerBase
{
private readonly ITransientService _transientService1;
private readonly ITransientService _transientService2;
public ExampleController(
ITransientService transientService1,
ITransientService transientService2)
{
_transientService1 = transientService1;
_transientService2 = transientService2;
}
[HttpGet]
public IActionResult Get()
{
return Ok(new
{
Service1 = _transientService1.GetOperationId(),
Service2 = _transientService2.GetOperationId()
});
}
}
每次访问该端点时,你会看到每个服务实例的不同 GUID。public interface IScopedService
{
Guid GetOperationId();
}
public class ScopedService : IScopedService
{
private readonly Guid _operationId;
public ScopedService()
{
_operationId = Guid.NewGuid();
}
public Guid GetOperationId() => _operationId;
}
在 Startup.cs 中注册服务:public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IScopedService, ScopedService>();
}
在控制器中使用该服务:[ApiController]
[Route("api/[controller]")]
public class ExampleController : ControllerBase
{
private readonly IScopedService _scopedService1;
private readonly IScopedService _scopedService2;
public ExampleController(IScopedService scopedService1, IScopedService scopedService2)
{
_scopedService1 = scopedService1;
_scopedService2 = scopedService2;
}
[HttpGet]
public IActionResult Get()
{
return Ok(new
{
Service1 = _scopedService1.GetOperationId(),
Service2 = _scopedService2.GetOperationId()
});
}
}
每次访问该端点时,在单个请求中你会看到相同的 GUID,但不同请求之间会有不同的 GUID。public interface ISingletonService
{
Guid GetOperationId();
}
public class SingletonService : ISingletonService
{
private readonly Guid _operationId;
public SingletonService()
{
_operationId = Guid.NewGuid();
}
public Guid GetOperationId() => _operationId;
}
在 Startup.cs 中注册服务:public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISingletonService, SingletonService>();
}
在控制器中使用该服务:[ApiController]
[Route("api/[controller]")]
public class ExampleController : ControllerBase
{
private readonly ISingletonService _singletonService1;
private readonly ISingletonService _singletonService2;
public ExampleController(ISingletonService singletonService1, ISingletonService singletonService2)
{
_singletonService1 = singletonService1;
_singletonService2 = singletonService2;
}
[HttpGet]
public IActionResult Get()
{
return Ok(new
{
Service1 = _singletonService1.GetOperationId(),
Service2 = _singletonService2.GetOperationId()
});
}
}
每次访问该端点时,你会看到相同的 GUID,在所有请求中都保持一致。