超时已过期. - 在ServiceStack服务中使用Db

Cal*_*ass 6 servicestack ormlite-servicestack

Db在ServiceStack服务中使用该属性来访问我的数据库,但我偶尔会从IIS中收到以下错误:

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

堆栈跟踪:

[InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.]
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +6371713
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6372046
   System.Data.SqlClient.SqlConnection.Open() +300
   ServiceStack.OrmLite.OrmLiteConnection.Open() +44
   ServiceStack.OrmLite.OrmLiteConnectionFactory.OpenDbConnection() +132
   ServiceStack.ServiceInterface.Service.get_Db() +68
Run Code Online (Sandbox Code Playgroud)

我相信在每个请求的基础上设置应关闭连接ReuseScopeConfigure方法ReuseScope.None?我在这做错了什么?

public override void Configure(Container container)
{
    JsConfig.EmitCamelCaseNames = true;

    //Register all your dependencies
    ConfigureDb(container);

    //Set MVC to use the same Funq IOC as ServiceStack
    ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
}
Run Code Online (Sandbox Code Playgroud)

ConfigureDb:

private static void ConfigureDb(Container container)
{
    var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
    container.Register<IDbConnectionFactory>(c =>
        new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider))
        .ReusedWithin(ReuseScope.None);

    using (var db = container.Resolve<IDbConnectionFactory>().Open())
    {
      // Do database seed/initialisation
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

经过更多诊断后,当我调用此服务方法时,我几次刷新页面时似乎发生了这种情况:

public Warranty Get(Warranty request)
    {
        var warranty = new Warranty();
        if (request.Id != default(int))
        {
            warranty = Db.Id<Warranty>(request.Id);
            warranty.WarrantyOrder = ResolveService<WarrantyOrderService>().Get(new WarrantyOrder { WarrantyId = warranty.Id });
            warranty.WarrantyStatus = ResolveService<WarrantyStatusService>().Get(new WarrantyStatus { Id = warranty.StatusId });
            warranty.WarrantyNotes = ResolveService<WarrantyNoteService>().Get(new WarrantyNotes { WarrantyId = warranty.Id });
            warranty.WarrantyDialogues = ResolveService<WarrantyDialogueService>().Get(new WarrantyDialogues { WarrantyId = warranty.Id });
            warranty.WarrantyCredit = ResolveService<WarrantyCreditService>().Get(new WarrantyCredit { WarrantyId = warranty.Id });
            warranty.WarrantyPhotos = ResolveService<WarrantyPhotoService>().Get(new WarrantyPhotos { WarrantyReference = warranty.WarrantyReference });
            warranty.WarrantyReport = ResolveService<WarrantyReportService>().Get(new WarrantyReport { WarrantyId = warranty.Id });
        }

        return warranty;
    }
Run Code Online (Sandbox Code Playgroud)

我已ConfigureDb根据@mythz的答案改变如下:

    private static void ConfigureDb(Container container)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
        container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
    }
Run Code Online (Sandbox Code Playgroud)

该服务需要调用其他服务来填充我Warranty对象上的其他对象,我不确定如何改进这个?

myt*_*thz 6

IDbConnectionFactory像所有的连接管理器是线程安全的工厂创建数据库连接,也就是说,它不是一个DB连接本身.它应该被注册为单身人士.

默认情况下,ServiceStack的IOC(Funq)默认注册为Singleton,因此正确的注册只是:

container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
Run Code Online (Sandbox Code Playgroud)

解决服务

由于ServiceStack服务有可能利用托管资源,因此只要它们从另一个服务中解析出来,就应该在using语句中使用它们,以便它们被适当处理,例如:

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; 
}
Run Code Online (Sandbox Code Playgroud)

  • @jeffgabhart ServiceStack仅处理它自行解析的服务,过滤器,依赖关系等.因此,如果您自己手动解决来自IOC的自动连接依赖关系(即[ResolveService在引擎盖下做什么](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/HostContext.cs#L299) ))而不是你负责处理它. (2认同)