RegisterWithContext和生活方式不匹配 [英] RegisterWithContext and Lifestyle Mismatch

查看:220
本文介绍了RegisterWithContext和生活方式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想注入一个记录器,以我的控制器和我需要通过扩大信息的采集器的构造。为此目的我已经使用 RegisterWithContext

I wanted to inject a logger to my controllers and I needed to pass extended info to the logger's constructor. For the purpose I've use RegisterWithContext:

container.RegisterWithContext<Common.Logging.ILogger>(context =>
{
    if (context.ServiceType == null && !container.IsVerifying())
        {
        throw new InvalidOperationException(
            "Can't request ILogger directly from container, " + 
            "it must be injected as a dependency.");
    }
    return new Common.Logging.NLogLogger(context.ImplementationType.FullName);
});



RegisterWithContext 扩展方法明确地注册提供的委托为瞬态

RegisterWithContext extension method explicitly registers the supplied delegate as Transient.

我需要注入同样的记录器( Common.Logging.NLogLogger ),其中恰好是单身的服务。

I need to inject the same Logger (Common.Logging.NLogLogger) in a service which happens to be singleton.

升级到3.0.6 SimpleInjector之前的事情似乎按预期方式工作和 container.Verify()真是太开心了与整个配置。

Before upgrading to SimpleInjector 3.0.6 things seemed to work as expected and container.Verify() was quite happy with the whole configuration.

升级后验证返回的几个误区:

After the upgrade the verifier returns a few errors:

[生活方式不匹配] SearchEngineIndexerService(辛格尔顿)取决于
ILogger(瞬态)。 [生活方式不匹配] MembershipService(网络
请求)取决于ILogger(瞬态)。

[Lifestyle Mismatch] SearchEngineIndexerService (Singleton) depends on ILogger (Transient). [Lifestyle Mismatch] MembershipService (Web Request) depends on ILogger (Transient).

和它的意义。我能理解为什么出现这种情况,为什么它应该是可以避免的。

and it makes sense. I can understand why that happens and why it should be avoided.

我试图避免在难道我登录太多综合征,但,其实,我真的需要做一些记录在一对夫妇的服务。

I am trying to avoid the "Do I log too much" syndrome but, actually, I really need to do some logging in a couple of services.

我试图使用的 RegisterConditional 根据一定的条件,但是,当然,现在所有的记录应当予以登记条件或我得到这个例外注册一个不同的记录:

I've tried to use RegisterConditional to register a different logger based on certain conditions but, of course, all the logger now should be registered conditional or I get this exception:

键入ILogger已经被注册为无条件的注册。对于非泛型类型,有条件和无条件注册不能混用。

Type ILogger has already been registered as unconditional registration. For non-generic types, conditional and unconditional registrations can't be mixed.

什么是注册一个记录器暂态的控制器,另一个用于一个单独的服务?

What's the best approach to register a logger as transient for a controller and another one for a singleton service?

推荐答案

您现在看到这个异常的原因,是因为v3.0.6固定的一些bug ,阻止生活方式不匹配无法显示在某些场合的警告。

The reason you are seeing this exception now, is because v3.0.6 fixed some bugs that prevented the lifestyle mismatch warning from showing up in certain occasions.

这是最好的忽略 RegisterWithContext 扩展方法,因为它已经被取代了 RegisterConditional 在v3中的方法。 RegisterConditional 然而,只允许注册类型;不是代表,因为代表们允许您根据运行时的决策作出决定,但它是不好的做法,使对象图解析过程中运行时的决策。

It is best to ignore the RegisterWithContext extension method, because it has been superseded by the RegisterConditional method in v3. RegisterConditional however, only allows registering types; not delegates, because delegates allow you to make decisions based on runtime decisions, but it is bad practice to make runtime decisions during object graph resolution.

所以,相反,它是最好的以限定允许转发该呼叫到真实记录器代理记录器类。例如:

So instead, it is best to define a proxy logger class that allows forwarding the call to the real logger. For instance:

public sealed class Logger<T> : ILogger
{
    private static readonly ILogger logger = 
        new Common.Logging.NLogLogger(typeof(T).FullName);

    // Implement ILogger methods here
    void ILogger.Log(string message) {
        // Delegate to real logger
        logger.Log(message);
    }
}

这实现可以注册如下:

container.RegisterConditional(typeof(ILogger),
    c => typeof(Logger<>).MakeGenericType(c.Consumer.ImplementationType),
    Lifestyle.Singleton,
    c => true);



文档描述得更详细。

这篇关于RegisterWithContext和生活方式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆