Timeout expired. - Using Db in ServiceStack Service

谁都会走 提交于 2019-12-03 14:22:47

The IDbConnectionFactory like all connection managers is a thread-safe factory to create DB Connections, i.e. it's not a DB connection itself. It is supposed to be registered as a singleton.

By default ServiceStack's IOC (Funq) registers as a Singleton by default, so the correct registration is just:

container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));

Resolving Services

As ServiceStack Services have the potential to utilize managed resources, they should be used within a using statement whenever they're resolved from another service, so that they're appropriately disposed of, e.g:

using (var orders = ResolveService<WarrantyOrderService>())
using (var status = ResolveService<WarrantyStatusService>())
{
    var warranty = new Warranty { 
        WarrantyOrder = orders.Get(new WarrantyOrder { WarrantyId = warranty.Id }),
        WarrantyStatus = status.Get(new WarrantyStatus { 
            WarrantyId = warranty.StatusId }),
       //etc
    }       

    return warranty; 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!