Search code examples
aspnetboilerplate

Resolve application service spent 1 second


We use abp in a really product for user. And there is api, it's so easy, but the response time for it is about 1 second. We think it shouldn't be, then we investigate it, found the execution time for the application service method is about 100 ms, and resolve the application service spent about 1 second.

We use IocManager.RegisterAssemblyByConvention(thisAssembly) for register.

Following screenshot is from the log file for investigation: enter image description here

My questions are:

  1. why the resolver spent so much time?
  2. What's the correct way to fix this issue.

Solution

  • Before, we use default DependencyLifeStyle as Transient, and we make some changes to set DependencyLifeStyle to per request. And the resolve is so fast now.

    The code changes as below:

    public override void PreInitialize()
    {
        IocManager.IocContainer.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
    }
    private void Kernel_ComponentRegistered(string key, IHandler handler)
    {
        foreach (var interceptor in interceptors)
        {
            if (interceptor.Key.IsAssignableFrom(handler.ComponentModel.Implementation))
            {
                handler.ComponentModel.Interceptors.Add(new InterceptorReference(interceptor.Value));
            }
        }   
        if (handler.ComponentModel.Implementation.Assembly.FullName.Contains(".Domain"))
        {
            if (handler.ComponentModel.LifestyleType == LifestyleType.Transient)
            {
                IocManager.IocContainer.Register(
                Component
                    .For(handler.ComponentModel.Implementation)
                    .ImplementedBy(handler.ComponentModel.Implementation)
                    .LifestyleCustom<MsScopedLifestyleManager>()
                    .IsDefault()
                    .Named($"{key}-1")
                    );
            }
        }
    }