简体   繁体   English

WCF Ninject Ninject.ActivationException

[英]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: 我有多个规则是我的“ WCF服务”,该规则实现了接口IRule:

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

for example the rule Crazy looks like this: 例如Crazy规则看起来像这样:

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. 我也在使用ninject模块,我将其称为RuleFactory。 I bind an IRule to every rule class and give them a name. 我将IRule绑定到每个规则类,并为其命名。

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. 当我从服务中检索所有战士时,会调用一个名为DoRules()的方法,该方法将遍历所有战士并尝试为其执行所有规则。

 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? 该代码将在foreach (var rule in rules)提供一个异常foreach (var rule in rules)知道如何解决此问题吗?

The Crazy class' receives an IRule as constructor parameter - but doesn't use it. Crazy类会收到IRule作为构造函数参数,但不会使用它。 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. 异常发生在foreach (var rule in rules)因为GetAll返回一个IEnumerable ,它仅在枚举枚举时才导致实际检索实例。 Hence the exception doesn't occur on GetAll but rather when looping the IEnumerable . 因此,该异常不会在GetAll发生,而是在循环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. 由于Crazy类实际上并未利用IRule因此注入它只是将其从构造函数中删除,因此异常应消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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