使用Autofac WcfIntegration时如何处理构造函数异常 [英] How to handle constructor exception when using Autofac WcfIntegration

查看:56
本文介绍了使用Autofac WcfIntegration时如何处理构造函数异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当该构造函数接受依赖项时,是否有一种方法可以处理WCF服务的构造函数引发的异常,并且这是IoC容器(在这种情况下为AutoFac)对依赖项的实例化,导致异常?

Is there a way to handle an exception thrown by the constructor of a WCF service, when that constructor takes in a dependency, and it is the instantiation of the dependency by the IoC container (AutoFac in this case) that causes the exception?

使用以下构造函数考虑WCF服务:

Consider a WCF service with the following constructor:

public InformationService(IInformationRetriever informationRetriever)
{
    _informationRetriever = informationRetriever;
}
//... the service later makes use of the injected InformationRetriever

该服务使用AutoFac WcfIntegration和AutofacWebServiceHostFactory(这恰好是RESTful服务).

The service uses AutoFac WcfIntegration and the AutofacWebServiceHostFactory (this happens to be a RESTful service).

相关性已在服务的global.asax.cs中注册,即:

Dependencies are registered in the global.asax.cs of the service, i.e.:

builder.RegisterType<InformationRetriever>()
                .As<IInformationRetriever>()

现在,InformationRetriever实现在其构造函数中执行一些检查,以确保一切就绪以使其能够完成其工作.当它在此阶段发现问题时,将引发异常.

Now the InformationRetriever implementation performs some checks in its constructor to ensure everything is in place for it to be able to do its job. When it discovers a problem in this phase, it throws an exception.

但是,我不希望服务的调用者收到AutoFac异常:

However, I do not want the caller of the service to receive the AutoFac exception:

An exception was thrown while invoking the constructor ... on type InformationRetriever

有效地我正在测试:

给出 InformationService正在运行

Given the InformationService is running

何时,我调用GetSomeInformation()方法

When I call the GetSomeInformation() method

并且信息实例无法实例化

然后,我想返回一条友好的错误消息

Then I want to return a friendly error message

并且记录实际的异常

这是我的设计中的问题,还是有已知的模式可以克服或防止此问题?

Is this a problem with my design, or is there a known pattern to overcome or prevent this problem?

我已经四处搜寻,找不到有关此类问题的任何信息.

I have hunted around and could not find any information on this type of problem.

推荐答案

我不是构造函数的狂热爱好者,他们出于错误参数以外的原因抛出异常.我可能会以不同的方式为我的类型建模.但是这里有一些想法.起初我想做这样的事情:

I'm not a big fan of constructors throwing exceptions for reasons other than bad arguments. I'd probably model my types a different way. But here's some ideas. At first I thought about doing something like this:

builder
    .Register(c => {
        try
        {
            return new InformationRetriever();
        }
        catch (Exception)
        {
            return new FailoverInformationRetreiver();
        }})
    .As<IInformationRetriever>();

...,其中FailoverInformationRetreiver在成员访问时引发异常.另一个想法可能是:

... where FailoverInformationRetreiver throws exceptions on member access. Another idea might be to do:

public InformationService(Lazy<IInformationRetriever> informationRetriever)
{
    _informationRetriever = informationRetriever;
}

try/catch围绕InformationService内部的用法.如果在应用启动时知道InformationRetriever的可用性,则可以使用另一个选项:

and try/catch around usages inside InformationService. Another option you could go with if the availability of InformationRetriever is known at app startup:

// inside your container builder:
if (InformationRetreiver.IsAvailable())
    builder.RegisterType<InformationRetriever>()
           .As<IInformationRetriever>()

// inside InformationService, make the dependency optional
public InformationService(IInformationRetriever informationRetriever = null)
{
    _informationRetriever = informationRetriever;
}

这些想法有帮助吗?

这篇关于使用Autofac WcfIntegration时如何处理构造函数异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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