简体   繁体   中英

How to call Stored Procedure asynchronously?

In my ASP.NET MVC (C#) application, i am using Entity Framework and calling Stored Procudures like this :

    public virtual ObjectResult<ART_USP_GetAssetReportGridList_Result> ART_USP_GetAssetReportGridList(string searchExpression, string sortExpression, string sortDirection, Nullable<int> startIndex, Nullable<int> pageSize, ObjectParameter count)
    {
        var searchExpressionParameter = searchExpression != null ?
            new ObjectParameter("SearchExpression", searchExpression) :
            new ObjectParameter("SearchExpression", typeof(string));

        var sortExpressionParameter = sortExpression != null ?
            new ObjectParameter("SortExpression", sortExpression) :
            new ObjectParameter("SortExpression", typeof(string));

        var sortDirectionParameter = sortDirection != null ?
            new ObjectParameter("SortDirection", sortDirection) :
            new ObjectParameter("SortDirection", typeof(string));

        var startIndexParameter = startIndex.HasValue ?
            new ObjectParameter("StartIndex", startIndex) :
            new ObjectParameter("StartIndex", typeof(int));

        var pageSizeParameter = pageSize.HasValue ?
            new ObjectParameter("PageSize", pageSize) :
            new ObjectParameter("PageSize", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<ART_USP_GetAssetReportGridList_Result>("ART_USP_GetAssetReportGridList", searchExpressionParameter, sortExpressionParameter, sortDirectionParameter, startIndexParameter, pageSizeParameter, count);
    }

Here ART_USP_GetAssetReportGridList is the stored procedure name. This is a synchronous call. If the number of records are more than 3000 this call is giving me Time-out error.

How can i make this call asynchronously without getting the Time-out error?

You may increase the timeout on specific operations as follows.

((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180; // in sec

Or set this code on your DbContext constructor.

Calling this asyncronously wont make any difference. You are getting a command timeout error. I order to avoid this you should increase the CommandTimeout setting of the SQL connection. But I would very much recommend not doing this, and looking at your stored procedure efficiency - the 30 second default timeout should be way, way more than you need.

请尝试这个

 var data = ObjectContext.Database.SqlQuery<ART_USP_GetAssetReportGridList_Result>("ART_USP_GetAssetReportGridList", searchExpressionParameter, sortExpressionParameter, sortDirectionParameter, startIndexParameter, pageSizeParameter, count).FirstOrDefaultAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM