日期:2014-05-17  浏览次数:20460 次

使用ninject,错误。请大家帮忙看看
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            IKernel ninjectKernal = new StandardKernel();
            ninjectKernal.Bind<ICalculateHelper>().To<CalculateHelper>();
            ICalculateHelper ic = ninjectKernal.Get<ICalculateHelper>();
            CalculateHelper ch = new CalculateHelper(ic);
            Console.WriteLine(ch.Add(1, 2).ToString());
            Console.ReadKey();
        }
    }
    public interface ICalculateHelper
    {
         decimal Add(decimal a, decimal b);
         decimal Sub(decimal a, decimal b);
    }
    public class CalculateHelper:ICalculateHelper
    {
        private ICalculateHelper ic;
        public CalculateHelper(ICalculateHelper icParam)
        {
            ic = icParam;
        }
        public decimal Add(decimal a, decimal b)
        {
            return b + a;
        }
        public decimal Sub(decimal a, decimal b)
        {
            return a - b;
        }
    }
}



错误提示为
Error activating ICalculateHelper using binding from ICalculateHelper to CalculateHelper
A cyclical dependency was detected between the constructors of two services.

------解决方案--------------------
循环依赖:
public class CalculateHelper:ICalculateHelper
{
private ICalculateHelper ic;
------解决方案--------------------
C# code

错误激活ICalculateHelper使用绑定从ICalculateHelper至CalculateHelperA循环依赖检测两个服务之间的构造函数。