0

嗨所以我在日志中出现错误,并且某些功能有时会损坏,但它是间歇性的。

完整的堆栈跟踪看起来是这样的:

System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参阅内部异常。---> System.InvalidOperationException: ExecuteReader 需要一个打开且可用的连接。连接的当前状态是打开的。在 System.Data.SqlClient.SqlConnection.GetOpenConnection(String method) 在 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) 在 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) 在 System .Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String 方法, TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget 目标, Func 3 operation, TInterceptionContext interceptionContext, Action3 执行, Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) --- End of inner exception stack trace --- at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 在 System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 操作) 在 System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable1 forMergeOption) 在 System.Data.Entity.Core .Objects.DataClasses.EntityCollection 1.Load(List1 集合,MergeOption mergeOption) 在 System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad() 在 System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem]( TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass72.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) at System.Data.Entity.DynamicProxies.Product_49EFA7CB993633FA6F92A211A42F2C206E79CC0974B0D20D2E9E66248C8DC082.get_ProductSpecificationAttributes() at Nop.Plugin.Widgets.Enhancements.Controllers.ProductController.ProductDetail_SizesDropDownStockLevelsOverall(Int32 productId, String productColor) at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2个参数)在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker。 AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker。AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.b__1c() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<> c__DisplayClass21.b__1e(IAsyncResult asyncResult) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) 在 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 在 System.Web.Mvc.Async.AsyncResultWrapper .WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) 在 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) 在 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid `1.CallEndDelegate(IAsyncResult asyncResult) 在 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStepImpl (IExecutionStep 步骤)在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean& completedSynchronously)

我在连接字符串中看到了一些关于多个活动结果集的解决方案,但显然这是一种解决方法,而不是解决方案。

堆栈跟踪将我带到这部分代码,尽管我不能完全弄清楚它是什么。

[HttpPost]
    public ActionResult ProductDetail_SizesDropDown(int productId, string productColor)
    {
        string dropdownOptions = "";
        string dropdownOptionsSizes = "";
        var allSizes = new List<Tuple<string, int>>();
        var product = _productService.GetProductById(productId);
        if (product == null)
        {
            return Json(new
            {
                success = false
            });
        }
        var parentProductId = (product.ParentGroupedProductId > 0) ? product.ParentGroupedProductId : productId;

        var childProducts = _cacheManager.Get("CHILD_PRODUCTS_" + parentProductId, () => _productService.GetAssociatedProducts(parentProductId));

        if (childProducts.Count() == 0)
        {
            return Json(new
            {
                success = false
            });
        }

        foreach (var childProduct in childProducts)
        {
            var sizeSpec =
                childProduct.ProductSpecificationAttributes.FirstOrDefault(
                    x => x.SpecificationAttributeOption != null && x.SpecificationAttributeOption.SpecificationAttribute != null && x.SpecificationAttributeOption.SpecificationAttribute.Name == "Size");
            if (sizeSpec != null)
            {
                var size = this.GetSpecValue(sizeSpec);
                allSizes.Add(new Tuple<string, int>(size, sizeSpec.SpecificationAttributeOption.DisplayOrder));
            }
        }

        allSizes = allSizes.Distinct().OrderBy(x => x.Item2).ToList();

        var countSizes = 0;
        foreach (var sizeTuple in allSizes)
        {
            var size = sizeTuple.Item1;
            var disabled = "";
            var selected = "";

            var sizeProduct = _cacheManager.Get("SIZE_PRODUCT_" + parentProductId + "_" + sizeTuple.Item1 + "_" + productColor, 60, () =>
            {
                if (!String.IsNullOrEmpty(productColor))
                {
                    return _productService.GetAssociatedProducts(parentProductId).FirstOrDefault(x =>
                        x.ProductSpecificationAttributes.Any(psa =>
                            psa.SpecificationAttributeOption != null && psa.SpecificationAttributeOption.SpecificationAttribute != null && psa.SpecificationAttributeOption.Name == sizeTuple.Item1 &&
                            psa.SpecificationAttributeOption.SpecificationAttribute.Name == "Size") &&
                        x.ProductSpecificationAttributes.Any(psa =>
                            psa.SpecificationAttributeOption != null && psa.SpecificationAttributeOption.SpecificationAttribute != null && psa.SpecificationAttributeOption.Name == productColor &&
                            psa.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour"));
                }
                else
                {
                    return _productService.GetAssociatedProducts(parentProductId).FirstOrDefault(x =>
                        x.ProductSpecificationAttributes.Any(psa =>
                            psa.SpecificationAttributeOption != null && psa.SpecificationAttributeOption.SpecificationAttribute != null && psa.SpecificationAttributeOption.Name == sizeTuple.Item1 &&
                            psa.SpecificationAttributeOption.SpecificationAttribute.Name == "Size"));
                }
            });

            // No size child exists for this product so we don't add to the dropdown
            if (sizeProduct == null)
                continue;

            var sizeLabel = size;
            if (sizeProduct != null)
            {
                var stockLabel = RefreshProductStockLabel(sizeProduct.Id, false);

                if (stockLabel.ToLower().Contains("out of stock"))
                {
                    stockLabel = "Out of stock";
                }

                if (stockLabel.ToLower() == "out of stock" || stockLabel.ToLower() == "low stock")
                {
                    sizeLabel += " <strong class=\"" + stockLabel.ToLower().Replace(" ", "-") + "\">" + stockLabel + "</strong>";
                }

                if (stockLabel.ToLower() == "out of stock" && !sizeProduct.PreorderAvailableNew)
                {
                    disabled = " disabled='disabled'";
                    selected = "";
                }
            }

            dropdownOptionsSizes += String.Format("<option value='{0}'{2}{3} data-text='{1}'>{0}</option>",
                HttpUtility.HtmlEncode(size.Replace("\"", "")), HttpUtility.HtmlEncode(sizeLabel), disabled, selected);

            countSizes++;
        }
        dropdownOptions = "<option value=''>Choose from " + countSizes + " sizes</option>" + dropdownOptionsSizes;


        return Json(new
        {
            success = true,
            dropdownOptions
        });
    }

我想我可能需要用来列出,当我拿孩子产品时,虽然我不确定。

var childProducts = _cacheManager.Get("CHILD_PRODUCTS_" + parentProductId, () => _productService.GetAssociatedProducts(parentProductId));

任何指导将不胜感激。谢谢

4

1 回答 1

1

这是关键点:

    System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad() 
at  System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem]

您的服务正在返回启用了延迟加载的实体。延迟加载实体具有对加载它们的 DbContext 的引用,因此不能被多个线程访问,也不能在 DbContext 实例被释放后访问。因此,如果您要从控制器方法返回对象以进行序列化,或者缓存它们以供其他请求使用,则必须禁用延迟加载。

您需要为将要序列化或缓存的实体关闭延迟加载

于 2020-04-28T23:37:40.827 回答