Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Nancy Ninject模块构造_C#_Ninject_Inversion Of Control_Nancy - Fatal编程技术网

C# Nancy Ninject模块构造

C# Nancy Ninject模块构造,c#,ninject,inversion-of-control,nancy,C#,Ninject,Inversion Of Control,Nancy,我正在尝试使用Ninject作为IoC容器来测试我的Nancy模块。我的问题是,我似乎无法让Nancy使用我的IoC绑定来解析NancyModule类型 我使用的是Nuget上最新的Nancy,最新的Nancy.Bootstrap.Ninject,它是使用最新的Ninject从源代码构建的 我的测试设置如下: [TestFixtureSetUp] public virtual void ClassSetup() { Assembly.Load(typeof (My

我正在尝试使用Ninject作为IoC容器来测试我的Nancy模块。我的问题是,我似乎无法让Nancy使用我的IoC绑定来解析NancyModule类型

我使用的是Nuget上最新的Nancy,最新的Nancy.Bootstrap.Ninject,它是使用最新的Ninject从源代码构建的

我的测试设置如下:

[TestFixtureSetUp]
    public virtual void ClassSetup()
    {
        Assembly.Load(typeof (MyModule).Assembly.FullName);
        var bootstrapper = new AspHostConfigurationSource();
        this.host = new Browser(bootstrapper);
    }

[Test]
public void test()
{
/*snip */
    var id = entity.Id;

    var response = host.Put("/path/to/{0}/".With(id.Encode()),
            (with) =>
            {
                with.HttpRequest();
                with.Header("Accept", "application/xml");
            });

        response.StatusCode.Should().Be(Nancy.HttpStatusCode.OK);

            /*snip */
    }
Bind<Nancy.NancyModule>()
        .ToMethod(context =>
        {
            return new MyModule1(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                Properties.Settings.Default.NamedCollection1),
                                                  context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                                                    Properties.Settings.Default.NamedCollection2));
        })
        .Named(typeof(MyModule1).FullName);

Bind<Nancy.NancyModule>()
        .ToMethod(context =>
        {
            return new MyModule2(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                Properties.Settings.Default.NamedCollection3),
                                                  context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                                                    Properties.Settings.Default.NamedCollection4));
        })
        .Named(typeof(MyModule2).FullName);
这是我的测试设置,剪了。现在是我的主机设置(在我的程序集中定义):

我的IoC绑定如下所示:

[TestFixtureSetUp]
    public virtual void ClassSetup()
    {
        Assembly.Load(typeof (MyModule).Assembly.FullName);
        var bootstrapper = new AspHostConfigurationSource();
        this.host = new Browser(bootstrapper);
    }

[Test]
public void test()
{
/*snip */
    var id = entity.Id;

    var response = host.Put("/path/to/{0}/".With(id.Encode()),
            (with) =>
            {
                with.HttpRequest();
                with.Header("Accept", "application/xml");
            });

        response.StatusCode.Should().Be(Nancy.HttpStatusCode.OK);

            /*snip */
    }
Bind<Nancy.NancyModule>()
        .ToMethod(context =>
        {
            return new MyModule1(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                Properties.Settings.Default.NamedCollection1),
                                                  context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                                                    Properties.Settings.Default.NamedCollection2));
        })
        .Named(typeof(MyModule1).FullName);

Bind<Nancy.NancyModule>()
        .ToMethod(context =>
        {
            return new MyModule2(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                Properties.Settings.Default.NamedCollection3),
                                                  context.Kernel.Get<IMongoRepository<Guid, Entity>>(
                                                    Properties.Settings.Default.NamedCollection4));
        })
        .Named(typeof(MyModule2).FullName);
Bind()
.ToMethod(上下文=>
{
返回新的MyModule1(context.Kernel.Get(
Properties.Settings.Default.NamedCollection1),
context.Kernel.Get(
Properties.Settings.Default.NamedCollection2);
})
.Named(typeof(MyModule1).FullName);
绑定()
.ToMethod(上下文=>
{
返回新的MyModule2(context.Kernel.Get(
Properties.Settings.Default.NamedCollection3),
context.Kernel.Get(
Properties.Settings.Default.NamedCollection4);
})
.Named(typeof(MyModule2).FullName);
我想控制南希模块的建设。我查看了Nancy的源代码,它看起来像是请求Nancy向配置的IoC容器询问NancyModule类型的所有注册绑定,并使用适当的密钥。下面的代码在Nancy.Bootstrappers.Ninject程序集中定义

   protected override sealed NancyModule GetModuleByKey(IKernel container, string moduleKey)
    {
        return container.Get<NancyModule>(moduleKey);
    }
protected override sealed NancyModule GetModuleByKey(IKernel容器,字符串moduleKey)
{
返回容器.Get(moduleKey);
}
密钥看起来是使用密钥生成器对象生成的:

public class DefaultModuleKeyGenerator : IModuleKeyGenerator
{
    /// <summary>
    /// Returns a string key for the given type
    /// </summary>
    /// <param name="moduleType">NancyModule type</param>
    /// <returns>String key</returns>
    public string GetKeyForModuleType(Type moduleType)
    {
        return moduleType.FullName;
    }
}
公共类DefaultModuleKeyGenerator:IModuleKeyGenerator
{
/// 
///返回给定类型的字符串键
/// 
///NancyModule型
///串键
公共字符串GetKeyFormModuleType(类型moduleType)
{
返回moduleType.FullName;
}
}
这就是为什么我的绑定被设置为命名绑定。其思想是,当Nancy请求命名绑定(对于模块)时,它将选择p my命名绑定

它没有按预期工作。Ninject抱怨我为NancyModule类型设置了多个绑定

重申:我的目标是控制模块的构建

任何想法都将不胜感激


另外,当我谈到将依赖项注入模块时,我不/不/将Ninject模块称为

不太确定您在这里做什么。MyBootstrapper类型,是测试程序集中的代码还是主程序集中的代码?这是我的bootstrapper程序集中的代码我想出了解决问题的方法。我不确定这是不是最佳的解决方案,也不确定这是否是“黑客”的解决方案,但事实是:在我的IoC绑定中,我可以执行以下Unbind();这将注销Nancy以前设置的所有绑定,并允许我控制模块的创建。如果没有更好的答案,我将把这个问题转移到答案上来。你可以在测试中使用可配置引导程序,它可以让你对每个测试的引导程序设置进行细粒度的控制。这里有文档,我为Nancy创建了一个问题。这里是测试