简体   繁体   中英

WCF Ninject Ninject.ActivationException

I get the error:

    Error activating IRule using binding from IRule to Crazy

A cyclical dependency was detected between the constructors of two services.

I have multpiple rules is my "WCF service" the rules implements the interface IRule:

public interface IRule
{
    warior ExecuteRule(warrior _warrior);
}

for example the rule Crazy looks like this:

public class Crazy : IRule
    {
        private readonly IRule _rule;

        public Crazy(IRule rule)
        {
            _rule = rule;
        }

        public wariors ExecuteRule(warriors _warriors)
        {
            using (var context = new warriors_officialEntities())
            {
                _warriors = context.warriors.SingleOrDefault(x => x.Name == _warriors.Name);
                _warriors.cursed = (_warriors.Sleep > 70 && _warriors.Hunger > 70);
                context.SaveChanges();
            }
            return _warriors;
        }
    }

I'm also using a ninject module I call it the RuleFactory. I bind an IRule to every rule class and give them a name.

public class RuleFactory : NinjectModule
{

    public override void Load()
    {
        Bind<IRule>().To<Crazy>().Named("crazy");
        Bind<IRule>().To<Hungry>().Named("hunger");
        Bind<IRule>().To<Munchies>().Named("munchies");
        Bind<IRule>().To<Sleepy>().Named("sleepys");
    }
}

When I retrieve all the wariors from my service is fire a method called DoRules(), this method will loop trough all the warriors and tries to execute every rule for them.

 IKernel _rules = new StandardKernel(new RuleFactory());
 public void DoRule(List<warriors> tmgs)
        {
            IEnumerable<IRule> rules = _rules.GetAll<IRule>();
            using (var context = new warriors_officialEntities())
            {
                foreach (warriors tmg in tmgs)
                {
                    foreach (var rule in rules)
                    {
                          rule.ExecuteRule(tmg);
                    }
                    tmg.LastAccess = DateTime.Now;

                    context.Entry(tmg).State = EntityState.Modified;
                }
                context.SaveChanges();
            }
        }

The code will give an exception in foreach (var rule in rules) Any idea how to fix this?

The Crazy class' receives an IRule as constructor parameter - but doesn't use it. This causes a cyclical dependency - but I believe the relevant binding is missing from your question.

The exception occurs on foreach (var rule in rules) because GetAll returns an IEnumerable which only leads to actually retrieving the instances when it is enumerated. Hence the exception doesn't occur on GetAll but rather when looping the IEnumerable .

As the Crazy class doesn't actually make use of the IRule it get's injected just remove it from the constructor and the exception should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM