Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Static IocManager.Instance isn't resolving dependencies. #4835


User avatar
0
gunpal5 created

Hello,

I have a static class in my codes. I need to resolve a dependency using IocManager.Instance. but it doesn't seem to be resolving the dependency. It seems this instance is not the same as the application IocManager.

Is there any suggestion for the workaround?

public static class TradeFactory
    {
        private static ITradeIdGenerator TradeIdGenerator
        {
            get
            {
                return IocManager.Instance.Resolve<ITradeIdGenerator>();
            }
        }

        public static Trade GenerateTrade(string currencyPair, Price executionPrice, Volume executedQuantity,Order matchedOrder, Order inboundOrder)
        {
            Trade trade=new Trade(TradeIdGenerator.GenerateTradeId(),currencyPair, executionPrice, executedQuantity, DateTime.Now,
            matchedOrder, inboundOrder);
            return trade;
        }
    }

Regards, Gunpal Jain


10 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    1. Does ITradeIdGenerator have an implementation that also implements ISingletonDependency (or ITransientDependency)?
    2. Does it work when ITradeIdGenerator is constructor-injected into a non-static class?
    3. Can you post the stack trace in Logs.txt?
  • User Avatar
    0
    DennisAhlin created

    I get this problem too.

    public class ComponentsInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store){
            container.Register(Component.For&lt;ICustomerConnector&gt;().ImplementedBy&lt;CustomerConnector&gt;().LifestyleTransient());
        }
    }
    
    MyCoreModule:
    public override void Initialize()
    ...
    //Here it works
    var connector = IocManager.Resolve&lt;ICustomerConnector&gt;();
    }
    
    CustomerConnectorFactory : MyServiceBase
        readonly IWindsorContainer _container;
    
        public CustomerConnectorFactory(IWindsorContainer container)
        {
            _container = container;
        }
    
        public ICustomerConnector Create() {
            //This works!
             var conn = _container.Resolve&lt;ICustomerConnector&gt;();
        
            //This breaks with ComponentNotFoundException
            var connector = IocManager.Instance.IocContainer.Resolve&lt;ICustomerConnector&gt;();
            return connector;
        }
        
        
        
        
        
    
  • User Avatar
    0
    aaron created
    Support Team

    Why use IocManager.Instance?

  • User Avatar
    0
    DennisAhlin created

    Because this Factory is overridden by multiple other factories, so there is much more code in form of constructors to add and maintain. Why would I not use it (except that it doesn't work)?

  • User Avatar
    0
    aaron created
    Support Team
  • User Avatar
    0
    DennisAhlin created

    Yes, this is true. I will use injection instead. But isn't it still weird that it doesn't work if I would have had a static factory that required the static Instance?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Where do you run ICustomerConnector's Create method ?

  • User Avatar
    0
    DennisAhlin created

    In a test class in the TestModule.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @dennisahlin then there might be missing module dependency but I don't suggest using IocManager in unit tests because of https://github.com/aspnetboilerplate/aspnetboilerplate/issues/585#issuecomment-135305251

  • User Avatar
    0
    DennisAhlin created

    Ok, good to know. Thank you!