.NET Core development actual combat (Lesson 6: Scope and target release behavior) - Study Notes (a)

06 | Scope and target release behavior

The main scope of this interface to carry IServiceScope

For instance object implements IDisposable class, the container will be responsible for managing its life cycle, after use, he will release these objects

Implement the IDisposable interface type of release:

  • 1, the container will be responsible for objects created by it, if the object is created out of their own and put the container, the container is not responsible for the release of the object
  • 2, sub-containers in the container and release, the container will go to free these objects, that object's life cycle life cycle of its container is created corresponding relationship

Two suggestions:

  • 1, at the root of the container, it is best not to create an instantaneous service implements IDisposable
  • 2, avoid manually create implements IDisposable object, and then stuffed inside a container, the container should be used as much as possible to manage the creation and release of our object

Demo code:
https://github.com/witskeeper/geektime/tree/master/samples/DependencyInjectionScopeAndDisposableDemo

Look at service

namespace DependencyInjectionScopeAndDisposableDemo.Services
{

    public interface IOrderService
    {

    }

    public class DisposableOrderService : IOrderService, IDisposable
    {
        public void Dispose()
        {
            Console.WriteLine($"DisposableOrderService Disposed:{this.GetHashCode()}");
        }
    }
}

First define IOrderService

Then define IOrderService implementation DisposableOrderService, and implements this interface IDisposable

When releasing the print release information, and outputs the object HashCode

Followed by a service registry (Startup)

services.AddTransient<IOrderService,DisposableOrderService>();

Here to register an instantaneous service, will be registered into IOrderService

Then look at the controller (WeatherForecastController)

[HttpGet]
public int Get([FromServices] IOrderService orderService,
    [FromServices] IOrderService orderService2)
{
    return 1;
}

Here FromServices get twice IOrderService

There is no need to write any code to operate it, because the whole life cycle is managed by the container to

Start the program, the output is as follows:

DisposableOrderService Disposed:10579059
DisposableOrderService Disposed:47945396

As can be seen, after finished, DisposableOrderService will be freed, and the two objects will be freed

HashCode two different objects

Immediate service at the time of acquisition of each will receive a new object

Then, add a line of code indicates Services

[HttpGet]
public int Get([FromServices] IOrderService orderService,
    [FromServices] IOrderService orderService2)
{
    Console.WriteLine("接口请求处理结束");
    return 1;
}

Output bit, means that we have access interface is complete, look at where the timing of the release

Start the program, the output is as follows:

接口请求处理结束
DisposableOrderService Disposed:35023218
DisposableOrderService Disposed:13943705

This shows that the interface after the request processing, the object is not released

Then look at the Scoped mode

Service Registration

services.AddScoped<IOrderService>(p => new DisposableOrderService());

Controller

[HttpGet]
public int Get([FromServices] IOrderService orderService,
    [FromServices] IOrderService orderService2)
{
    Console.WriteLine("=======1==========");
    // HttpContext.RequestServices
    // 是当前请求的一个根容器
    // 应用程序根容器的一个子容器
    // 每个请求会创建一个容器
    using (IServiceScope scope = HttpContext.RequestServices.CreateScope())
    {
        // 在这个子容器下面再创建一个子容器来获取服务
        var service = scope.ServiceProvider.GetService<IOrderService>();
    }
    Console.WriteLine("=======2==========");

    Console.WriteLine("接口请求处理结束");

    return 1;
}

Start the program, the output is as follows:

=======1==========
DisposableOrderService Disposed:31307802
=======2==========
接口请求处理结束
DisposableOrderService Disposed:31614998

Every request will get two releases, meaning that each create a Scoped scope, the role of each domain can be a single case

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/12339892.html