Skip to content

[Regression] 9.2.0 leads to strange InvalidCastException when injecting in constructor #81

@anonymous2585

Description

@anonymous2585

Describe the bug

After updating from Reflex 9.1.1 to 9.2.0, one of my service registered as singleton fail to inject due to a FieldInjectorException: Specified cast is not valid. exception.

The original exception is:

System.InvalidCastException: Specified cast is not valid.
  at (wrapper dynamic-method) System.Object.lambda_method(System.Runtime.CompilerServices.Closure,object[])
  at Reflex.Injectors.ConstructorInjector.Construct (System.Type concrete, Reflex.Core.Container container) [0x00094] in .\Packages\com.gustavopsantos.reflex\Injectors\ConstructorInjector.cs:29
  at Reflex.Core.Container.Construct (System.Type concrete) [0x00001] in .\Packages\com.gustavopsantos.reflex\Core\Container.cs:69 
  ...

It happens in a big project with ~140 services registered/injected as singleton.
When debugging (added logs in reflex code), I see that the exeption happen when one service (let say ServiceG) is injected in another by constructor:

public class ServiceG : IServiceG {

}
private IServiceB _serviceB;
private IServiceC _serviceC;
private IServiceD _serviceD;
private IServiceE _serviceE;
private IServiceF _serviceF;
private IServiceG _serviceG;

public ServiceA(
    IServiceB serviceB,
    IServiceC serviceC,
    IServiceD serviceD,
    IServiceE serviceE,
    IServiceF serviceF,
    IServiceG serviceG)
{
    _serviceB = serviceB;
    _serviceC = serviceC;
    _serviceD = serviceD;
    _serviceE = serviceE;
    _serviceF = serviceF;
    _serviceG = serviceG;
}
  • When I remove the argument from my constructor and use [Inject] attribute, I don't have the exception:
[Inject] private IServiceG _serviceG;

public ServiceA(
    IServiceB serviceB,
    IServiceC serviceC,
    IServiceD serviceD,
    IServiceE serviceE,
    IServiceF serviceF)
{
    _serviceB = serviceB;
    _serviceC = serviceC;
    _serviceD = serviceD;
    _serviceE = serviceE;
    _serviceF = serviceF;
}

The exception happen here.

This is strange, because this service (the ServiceG) has already been constructed and injected in others services, the instance is not null and just has to be injected in the service. When I debug the arguments variable, it contains all the services to inject in the right order.

When I replace

var arguments = ArrayPool.Rent(constructorParametersLength);

with

var arguments = new object[constructorParametersLength];

here, I don't have the exception anymore!
I think something is wrong with ThreadStaticArrayPool, but hard to tell precisly what.

Expected behavior
Update from 9.1.1 to 9.2.0+ happen without breaking change.

Desktop:

  • Unity editor 6000.0.37f1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions