依赖注入-名称';绑定';当前上下文中不存在-未处理MethodAccessException

本文关键字:不存在 MethodAccessException 未处理 上下文 绑定 -名 注入 依赖 | 更新日期: 2023-09-27 17:57:48

代码示例:

namespace DependencyInjection
{
class Program  
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        var samurai = kernel.Get<Samurai>();
        Bind<IWeapon>().To<Sword>();
   }       
}

class Samurai
{
    readonly IWeapon _weapon;
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }
    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}
interface IWeapon
{
    void Hit(string target);
}
class Sword : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Sword - ", target);
    }
}
class Arrow : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Arrow - ", target);
    }
}
}

这是我第一次尝试使用ninject实现DI。

我不知道如何解决错误"名称"Bind"在当前上下文中不存在"。我经历了这个问题编译错误与Ninject,但仍然不确定如何解决这个问题。如何解决这一问题。如果我能得到代码样本,这样我就能更好地理解,那就太好了

编辑:

namespace DependencyInjection
{
class Program  
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        kernel.Bind<IWeapon>().To<Sword>();
        var samurai = kernel.Get<Samurai>(); -----> Exception Line
        samurai.Attack("Hello");
   }       
}
class Samurai
{
    readonly IWeapon _weapon;
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }
    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}
interface IWeapon
{
    void Hit(string target);
}
class Sword : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Sword - ", target);
    }
}
class Arrow : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Arrow - ", target);
    }
}
}

上面的代码导致var samurai=kernel行出现"MethodAccessException was unhandled"。获取();我在谷歌上搜索,但找不到任何具体的解决方案

异常

System.MethodAccessException was unhandled
  Message="DependencyInjection.Sword..ctor()"
  Source="Anonymously Hosted DynamicMethods Assembly"
  StackTrace:
       at DynamicInjector84db385a6cfb4301b146100b5027c44a(Object[] )
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
       at Ninject.Activation.Context.Resolve()
       at Ninject.KernelBase.<Resolve>b__4(IContext context)
       at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
       at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
       at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass1.<Create>b__0(ITarget target)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
       at Ninject.Activation.Context.Resolve()
       at Ninject.KernelBase.<Resolve>b__4(IContext context)
       at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
       at DependencyInjection.Program.Main(String[] args) in D:'Sandboxes'C_Sharp'DependencyInjection'DependencyInjection'Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

依赖注入-名称';绑定';当前上下文中不存在-未处理MethodAccessException

出现此错误的原因是Bind通常是内核上的一个方法,因此应该在模块外执行此操作。此外,您应该在解析Samurai之前创建绑定,因此请交换最后两行:

static void Main(string[] args)
{
    IKernel kernel = new StandardKernel();
    kernel.Bind<IWeapon>().To<Sword>();
    kernel.Bind<Samurai>().ToSelf();
    var samurai = kernel.Get<Samurai>();

}

Bind有时在没有内核的情况下工作的原因是,您可能在模块中看到过这样的教程。NinjectModule有一个受保护的Bind方法,它执行相同的功能:

public class NinjaModule : NinjectModule {
    public void Load() {
       Bind<Samurai>().ToSelf()
       Bind<IWeapon>().To<Sword>();
    }
} 

无论哪种方法都是合适的,当绑定变得有点复杂时,建议将它们放入模块中。

EDIT确保所有的类和接口都是公共的,否则Ninject无法执行它们。如果未指定可访问性修饰符,则默认为internal。这就解释了MethodAccessException

kernel.Bind<IWeapon>().To<Sword>();

或者在继承自CCD_ 7的类中进行绑定。

Bind()方法是在IBindingRoot上定义的。StandardKernel和NinjectModule都派生自实现IBindingRoot的BindingRoot。

您可以在内核上绑定服务

public static void Main()
{
    var kernel = new StandardKernel();
    kernel.Bind<IWeapon>().To<Sword>();
    // Optional, Ninject will try to resolve any non-registered concrete type.
    kernel.Bind<Samuari>().ToSelf();
    var samurai = kernel.Get<Samuari>();
}

或使用Ninject模块

public class WeaponModule : NinjectModule
{
    public override void Load()
    {
        Bind<IWeapon>().To<Sword>();
        // Optional, Ninject will try to resolve any non-registered concrete type.
        Bind<Samuari>().ToSelf();
    }
}

然后加载模块

public static void Main()
{
    var module = new WeaponModule();
    var kernel = new StandardKernel(module);
    var weapon = kernel.Get<IWeapon>();
}