有没有办法处理WCF服务的构造函数抛出的异常,当该构造函数接受依赖时,它是IoC容器(在本例中为AutoFac)导致异常的依赖实例化?
考虑具有以下构造函数的WCF服务:
public InformationService(IInformationRetriever informationRetriever)
{
_informationRetriever = informationRetriever;
}
//... the service later makes use of the injected InformationRetriever
Run Code Online (Sandbox Code Playgroud)
该服务使用AutoFac WcfIntegration和AutofacWebServiceHostFactory(这恰好是一个RESTful服务).
依赖关系在服务的global.asax.cs中注册,即:
builder.RegisterType<InformationRetriever>()
.As<IInformationRetriever>()
Run Code Online (Sandbox Code Playgroud)
现在,InformationRetriever实现在其构造函数中执行一些检查,以确保一切就绪,以便能够完成其工作.当它在此阶段发现问题时,它会抛出异常.
但是,我不希望服务的调用者接收AutoFac异常:
An exception was thrown while invoking the constructor ... on type InformationRetriever
我有效地试图测试:
鉴于 InformationService正在运行
当我调用GetSomeInformation()方法时
并且无法实例化InformationRetriever
然后我想返回一个友好的错误消息
并记录实际的异常
这是我的设计问题,还是有一个已知的模式来克服或防止这个问题?
我一直在寻找,无法找到有关此类问题的任何信息.