如何创建将与 OWIN 启动一起使用的 UnityNancyBootstrapper 实现 class

How to create a UnityNancyBootstrapper-implementation that will work with a OWIN Startup class

我正在尝试创建一个 Nancy/Unity-bootstrapper 可以与 OWIN-startup-class 一起使用,就像这里描述的那样:

Minimal sample: Self hosted Nancy using Owin, Unity bootstrapper including xUnit test

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        IUnityContainer container = new UnityContainer();

        container.RegisterType<IMessageHelper, MessageHelper>();

        app.UseNancy(new NancyOptions
        {
            EnableClientCertificates = true,
            Bootstrapper = new NancyOwinBoxBootstrapper(container)
        });
    }
}

public class NancyOwinBoxBootstrapper : UnityNancyBootstrapper
{
    private IUnityContainer _container;

    public NancyOwinBoxBootstrapper(IUnityContainer container)
    {
        _container = container;
    }

    protected override IUnityContainer GetApplicationContainer()
    {
        return _container;
    }
}

NancyOwinBox.zip 中包含的示例工作正常,但它基于 Nancy.Bootstrappers.Unity 版本 1.1。当我将 NuGet 包升级到最新版本 (1.2) 时,示例失败并显示:

解析依赖失败,类型="Nancy.Bootstrapper.IApplicationStartup",名称="Nancy.ViewEngines.ViewEngineApplicationStartup"。 异常发生在:解析时。

异常是:InvalidOperationException - 当前类型 System.Collections.Generic.IEnumerable`1[Nancy.ViewEngines.IViewEngine] 是一个接口,无法构造。您是否缺少类型映射?

发生异常时,容器为:

正在解析 Nancy.ViewEngines.ViewEngineApplicationStartup、Nancy.ViewEngines.ViewEngineApplicationStartup(映射自 Nancy.Bootstrapper.IApplicationStartup、Nancy.ViewEngines.ViewEngineApplicationStartup) 解析构造函数Nancy.ViewEngines.ViewEngineApplicationStartup(System.Collections.Generic.IEnumerable1[[Nancy.ViewEngines.IViewEngine, Nancy, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null]] viewEngines, Nancy.ViewEngines.IViewCache viewCache, Nancy.ViewEngines.IViewLocator viewLocator) Resolving System.Collections.Generic.IEnumerable1[Nancy.ViewEngines.IViewEngine],(none)

的参数"viewEngines"

当前类型 System.Collections.Generic.IEnumerable`1[Nancy.ViewEngines.IViewEngine] 是接口,无法构造。您是否缺少类型映射?

有谁知道为什么会出现此错误?

您需要在容器中注册 EnumerableExtension,如下所示:

container.AddNewExtension<EnumerableExtension>();

参考 UnityNancyBootstrapper.cs