【问题标题】:Nancy Unable to resolve type when constructor has parameters当构造函数有参数时,南希无法解析类型
【发布时间】:2019-02-07 21:14:42
【问题描述】:

当我有以下代码时:

public class Webserver: NancyModule
{
    public Webserver()
    {
        Post[""] = _ => 200;
    }
}

一切正常,但当我有这个时

public class Webserver: NancyModule
{
    public Webserver(string id="")
    {
        Post[""] = _ => 200;
    }
}

我得到 8 个内部异常,其中一个是 TinyIoCResolutionException: Unable to resolve type: <namespace>.Webserver


在一个可能相关的问题中,当我有以下代码时,我得到了相同的异常:

public class Webserver<T> : NancyModule
{
    public Webserver()
    {
        Post[""] = _ => 200;
    }
}

我安装了 Nancy 版本 1.4.3 并安装了 Nancy.Hosting.Self 版本 1.4.1

【问题讨论】:

    标签: c# .net nancy


    【解决方案1】:

    您的模块必须可以使用应用程序的TinyIocContainer 进行解析。这意味着您不能传递原始构造函数参数,因为 TinyIoc 不知道如何创建它们。您作为参数传递给模块的任何内容本身都必须可以通过容器解析。

    第二种情况也是如此。由于您已将模块设为通用,TinyIoc 将不知道如何创建它。你可以为你的模块创建一个抽象的泛型基类,但是每个暴露的模块都必须关闭这个泛型类。

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 2017-04-03
      • 2015-05-05
      相关资源
      最近更新 更多