在我的 WCF 项目中,我在 global.asax 中使用 Castle Windsor 注册了我的界面:
Component.For<IStrategy>()
.ImplementedBy<MyStrategy>()
.LifestylePerWcfOperation(),
然后稍后在同一个文件中,我使用 FluentNhibernate 使用提供程序配置 NHibernate:
FluentConfiguration configuration = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(myConnString)
.Provider<ConnectionProvider>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TenantMap>()) etc etc
但是,此 ConnectionProvider 托管在一个公共共享 dll 库中,因为还有其他 WCF 服务需要使用它。我想按如下方式注册此依赖项,但它不起作用,这意味着我必须手动新建一个实例。
public ConnectionProvider()
{
// doesnt work
using (IDependencyScope scope = GlobalConfiguration.Configuration.DependencyResolver.BeginScope())
{
_myStrategy = scope.GetService<IStrategy>();
}
}
有没有办法使这项工作?它就像它现在在另一个组件中一样失去了范围。在同一个程序集中,DependencyScope 工作正常并创建了一个实例,但我想将它保存在一个共享的 dll 中。
编辑:我得到的错误是“System.Web.Http.Dependencies.EmptyResolver”在这段代码的手表上:GlobalConfiguration.Configuration.DependencyResolver