在Nancy项目中获取TinyIoc当前容器

8

我正在构建一个小的Nancy Web项目。

在我的一个类的方法(不是nancy模块)中,我想要实现以下功能:

var myThing = TinyIoC.TinyIoCContainer.Current.Resolve<IMyThing>();

然而,在 .Current (非公共成员,_RegisteredTypes) 中只有一个注册类型:

TinyIoC.TinyIoCContainer.TypeRegistration

自然地,在我的上面的代码中,我得到了以下结果:

无法解析类型: My.Namespace.IMyThing

所以,我猜我的引导程序没有注册相同的容器?

有没有办法获取它?

编辑

为了更好地阐述我的尝试:

基本上,我的 URL 结构类似于:

/{myType}/{myMethod}

所以,想要加载 Customer 服务并执行 showAllWithTheNameAlex 方法的想法是:

前往: /customer/ShowAllWithTheNameAlex

public interface IService
{
    void DoSomething();
    IEnumerable<string> GetSomeThings();
}

我接着创建了一个抽象基类,其中包含一个返回服务的方法 GetService。
在这里,我尝试使用 TinyIoC.TinyIoCContainer.Current.Resolve();
在这种情况下,应该使用 TinyIoC.TinyIoCContainer.Current.Resolve("typeName");

public abstract class Service : IService
{
    abstract void DoSomething();
    abstract IEnumerable<string> GetSomeThings();

    public static IService GetService(string type)
    {
        //currently, i'm doing this with reflection....
    }
}

这是我的服务实现。

public class CustomerService : Service
{
    public void DoSomething()
    {
        //do stuff
    }

    public IEnumerable<string> GetSomeThings()
    {
        //return stuff
    }

    public IEnumerable<Customer> ShowAllWithTheNameAlex()
    {
        //return
    }
}

最后,我有了我的Nancy模块,看起来像这样:
public class MyModule : NancyModule
{
    public MyModule()
    {
        Get["/{typeName}/{methodName}"] = p => ExecuteMethod(p.typeName, p.methodName);
    }

    private dynamic ExecuteMethod(string typeName, string methodName)
    {
        var service = Service.GetService(typeName);

        var result = service.GetType().GetMethod(methodName).Invoke(service, null);

        //do stuff

        return result; //or whatever
    }
}
3个回答

4

@alexjamesbrown - 简单来说,你不需要直接处理容器。Nancy是专门设计的,使你不必直接处理容器。你提到你想依赖IMyThing类,但它不是一个NancyModule。这并不是问题,只要你的模块之一有对它的引用,那么这些依赖关系也可以有自己的依赖关系,在运行时得到满足。

public interface IGreetingMessageService
{
   string GetMessage();
}

public class GreetingMessageService: IGreetingMessageService
{
   public string GetMessage()
   {
      return "Hi!";
   }
}

public interface IGreeter
{
   string Greet();
}

public class Greeter
{
   private readonly IGreetingMessageService service;

   public Greeter(IGreetingMessageService service)
   {
      this.service = service;
   }

   public string Greet()
   {
      return this.service.GetMessage();
   }
}

public class GreetingsModule : NancyModule
{
   public GreetingModule(IGreeter greeter)
   {
      Get["/"] = x => greeter.Greet();
   }
}

以上代码将正常工作,Greeter 在运行时将满足对 IGreetingMessageService 的依赖。

谢谢您的回答。我已经详细说明了我的问题,以便更清楚地说明我要做什么。希望这样能更容易理解! - Alex
由于你在我们的用户组发布了重复帖子,我提供了更详细的解释,链接在这里 https://groups.google.com/d/topic/nancy-web-framework/FVuM9kIiI7Q/discussion - TheCodeJunkie

0

我遇到了非常类似的问题,需要“共享”容器。这是一个问题的原因是我的程序作为服务运行,使用Nancy自托管提供REST API。我的模块有依赖项,这些依赖项由Nancy本身注入,但其他未从模块引用的应用程序部分也需要注入依赖项。 在这里(或任何地方)不是多个容器的明智选择,我需要在Nancy和应用程序的其余部分之间共享容器。

我只需执行以下操作 (我使用Autofac,但我认为TinyIoC类似)

public class Bootstrapper : AutofacNancyBootstrapper
{
    private static readonly Lazy<ILifetimeScope> container = new Lazy<ILifetimeScope>(RegisterTypes);
    public static ILifetimeScope Container => container.Value;

    protected override ILifetimeScope GetApplicationContainer()
    {
        return container.Value;
    }

    // Create container and register my types
    private static ILifetimeScope RegisterTypes()
    {
        var builder = new ContainerBuilder();

        // Register all my own types.....

        return builder.Build();
    }
}

然后,在我的主要代码中,我可以自己使用容器

public class Program
{
    public static void Main(string[] args)
    {
        // Resolve main service with all its dependencies
        var service = Bootstrapper.Container.Resolve<Service>();
        service.Run();
    }
}

由于我的NancyHost在服务内部,因此容器在第一次在main中使用时构建(仅一次),然后当Nancy开始创建Bootstrapper本身时,就会使用这个静态容器。

在理想的情况下,我不希望有一个全局可访问的容器,通常它应该是main函数的本地容器。


-1
在这种特定情况下,“不直接处理容器”非常棘手: public interface IFoo {} public class Foo:IFoo {public Foo(string bar){}}
假设IFoo已经是Nancy模块的构造函数依赖项。
请注意Foo构造函数的字符串依赖关系。当作为Nancy模块依赖项时,我需要向容器发送通信以使用该构造函数用于IFoo单例。我需要在NancyFx使用的TinyIoC实例上进行注册,并传入bar的实际值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接