带有funq的servicestack

带有funq的servicestack

本文介绍了带有funq的servicestack-按约定自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在其构造函数中采用IMyDependency的服务. IMyDependency,MyDependency和服务都位于同一程序集中. MyDependency具有一个公共的,无参数的构造函数.

I have a service which takes an IMyDependency in its constructor. IMyDependency, MyDependency and the service all live in the same assembly. MyDependency has a single, public, parameterless constructor.

令我惊讶的是,这没有用:

To my surprise, this did not work:

container.RegisterAutoWired<IMyDependency>();

它将引发"System.NullReferenceException".

It throws a "System.NullReferenceException".

如果我这样做,它将起作用:

It works if I do this:

container.RegisterAutoWiredAs<MyDependency, IMyDependency>();

但是,这也是如此:

container.RegisterAs<MyDependency, IMyDependency>();

那有什么区别?如果自动接线"找不到具体的实现,并且对可以解决需要依赖项的服务也没有影响,那么什么是自动接线?

So what is the difference? If 'auto wiring' cannot find a concrete implementation, and it makes no difference to whether services requiring the dependency can be resolved, then what is auto wiring?

Funq是否应该能够按照约定找到您的具体实现?如果是的话,那是什么约定,如果不是同名的话?

Is Funq supposed to be able to find your concrete implementations by convention? If so, what is that convention, if not same-named-ness?

谢谢.

推荐答案

对于像这样的简单查询,最好只联系源代码,例如这是 RegisterAutoWired :

For simple queries like this it's best to just contact the source, e.g. here is the source code for RegisterAutoWired:

public IRegistration<T> RegisterAutoWired<T>()
{
    var serviceFactory = GenerateAutoWireFn<T>();
    return this.Register(serviceFactory);
}

它通过具体实施生成自动连线的工厂.接口没有实现,它需要是一个具体的类.

It generates an auto-wired factory over a Concrete implementation. An interface has no implementation, it needs to be a concrete class.

以及 RegisterAs的源代码:

public IRegistration<TAs> RegisterAs<T, TAs>() where T : TAs
{
    return this.RegisterAutoWiredAs<T, TAs>();
}

这只是您可以使用的较短别名,而不是RegisterAutoWiredAs.

Which is just a shorter alias you can use instead of RegisterAutoWiredAs.

这篇关于带有funq的servicestack-按约定自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:31