有没有办法处理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
然后我想返回一个友好的错误消息
并记录实际的异常
这是我的设计问题,还是有一个已知的模式来克服或防止这个问题?
我一直在寻找,无法找到有关此类问题的任何信息.
使用实体框架4.3.1代码优先和数据迁移.
我编写了一个实用程序,使用MigratorScriptingDecorator自动为目标数据库生成迁移脚本.
但是,有时从头开始重新生成目标数据库时,生成的脚本无效,因为它声明了两次具有相同名称的变量.
变量名是@ var0.
当应用多个迁移时,以及当至少两个迁移导致默认约束被删除时,似乎会发生这种情况.
生成脚本表单代码和使用程序包管理器控制台命令时都会出现此问题:
Update-Database -Script
Run Code Online (Sandbox Code Playgroud)
以下是生成的脚本中的违规片段:
DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'SomeTableName')
Run Code Online (Sandbox Code Playgroud)
和
DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'SomeOtherTableName')
Run Code Online (Sandbox Code Playgroud)
我希望能够覆盖为每次迁移生成SQL的点,然后添加"GO"语句,以便每次迁移都在一个单独的批处理中,这将解决问题.
任何人都有任何想法如何做到这一点,或者如果我咆哮错误的树然后你可以建议一个更好的方法?