如何在apphost中使用Db Connection

时间:2013-04-09 21:33:26

标签: asp.net servicestack

我想知道如何从我的全局过滤器中访问IDbConnection,到目前为止我是这样做的。

现在我想的越多,我的apphost中就没有IDb,所以我怎样才能检索包装ormlite'stuff'和Cache的我的存储库?

谢谢你, 斯蒂芬

Global.asax中

public override void Configure(Container container)
{


    var dbConnectionFactory =
        new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("test"), true, SqlServerOrmLiteDialectProvider.Instance);

    container.Register<IDbConnectionFactory>(dbConnectionFactory); 

    RequestFilters.Add((httpReq, httpResp, requestDto) =>
        {

            // Do something here with either my repo or ormLite

        });
}

1 个答案:

答案 0 :(得分:2)

如果您可以访问容器,则可以执行以下操作:

RequestFilters.Add((httpReq, httpResp, requestDto) => {
    using (var db = container.Resolve<IDbConnectionFactory>().Open()) 
    {
        // Do something here with either my repo or ormLite
    }
});

否则您可以通过AppHostBase.Resolve<T>单身人士访问它:

RequestFilters.Add((httpReq, httpResp, requestDto) => {
    using (var db = AppHostBase.Resolve<IDbConnectionFactory>().Open()) 
    {
        // Do something here with either my repo or ormLite
    }
});