- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在运行的网站有大约 15.000 个实时用户(谷歌分析)(大约 1000 个请求/秒(性能计数器))。
我们在负载均衡器后面有两个 Web 服务器。
有时每天有时每周 1 次我们的 Web 服务器之一停止执行请求并开始响应错误,并且每个请求都记录以下异常:
“System.IndexOutOfRangeException - 索引超出了数组的范围。”
我们的环境:IIS 8.5、.Net 4.5.0、Mvc 5.1.0、Unity 3.5(与 3.0 状态相同)、WebActivatorEx 2.0
在 IIS 中,工作进程 1 和其他设置为默认值。
我们无法捕捉到任何导致此错误的特定场景。应用程序池回收后一切都没有问题。在每个请求都以错误响应之前,没有任何与之相关的错误。
过去与相关的旧 Unity 版本有一个问题:
https://unity.codeplex.com/discussions/328841
http://unity.codeplex.com/workitem/11791
看不出我能做些什么。
这里异常详细信息:
System.IndexOutOfRangeException
Index was outside the bounds of the array.
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)
at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegisterInstance(Object sender, RegisterInstanceEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Microsoft.Practices.Unity.UnityContainer.RegisterInstance(Type t, String name, Object instance, LifetimeManager lifetime)
at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterInstance[TInterface](IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager)
at DemoSite.News.Portal.UI.App_Start.UnityConfig.<>c__DisplayClass1.<RegisterTypes>b__0()
at DemoSite.News.Portal.Core.Controller.BaseController.Initialize(RequestContext requestContext)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TState](AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
public static void RegisterTypes(IUnityContainer container)
{
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
container.LoadConfiguration(section);
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
}
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
if (requestContext.RouteData.Values["ViewActionId"] != null)
{
int viewActionId;
if (!int.TryParse(requestContext.RouteData.Values["ViewActionId"].ToString(), out viewActionId))
return;
var cacheProvider = ServiceLocator.Current.GetInstance<ICacheProvider>();
List<ViewActionClass> viewActionClasses = null;
string cacheKey = CacheKeyCompute.ComputeCacheKey("ViewActionClass", CacheKeyTypes.DataCache,
new KeyValuePair<string, string>("viewActionId", viewActionId.ToString()));
_configuration = ServiceLocator.Current.GetInstance<IConfiguration>();
viewActionClasses =
cacheProvider.AddOrGetExistingWithLock<List<ViewActionClass>>(cacheKey, () =>
{
var viewActionClassBusiness =
ServiceLocator.Current.GetInstance<IViewActionClassBusiness>();
return viewActionClassBusiness.ViewActionClassGetByViewActionId(viewActionId);
});
ViewBag.ActionClass = viewActionClasses;
ViewBag.Configuration = _configuration;
}
base.Initialize(requestContext);
}
<type type="DemoSite.Runtime.Caching.ICacheProvider, DemoSite.Core"
mapTo="DemoSite.Runtime.Caching.ObjectCacheProvider, DemoSite.Core">
<lifetime type="containerControlledLifetimeManager" />
</type>
<type type="DemoSite.Core.Configuration.IConfiguration, DemoSite.Core"
mapTo="DemoSite.Core.Configuration.ConfigFileConfiguration, DemoSite.Core">
<lifetime type="containerControlledLifetimeManager" />
</type>
<type type="DemoSite.News.Business.IViewActionClassBusiness, DemoSite.News.Business"
mapTo="DemoSite.News.Business.Default.ViewActionClassBusiness, DemoSite.News.Business.Default">
<lifetime type="perRequestLifetimeManager" />
</type>
最佳答案
据我从堆栈跟踪中可以看出,您正在 Web 请求期间在容器中注册实例。 RegisterType
和 RegisterInstance
方法是不是 Unity 中的线程安全(这可能适用于 .NET 中的大多数 DI 库)。这就解释了为什么这种情况会发生在随机点和高负载下。
最好只在启动时注册您的容器,以后不要更改它。特别是使用依赖倒置原则和依赖注入(inject)模式,您尝试集中了解对象图如何连接的知识,但稍后通过进行新的注册再次分散它。即使注册在 Unity 中是线程安全的,您仍然很可能通过在运行时更改注册来引入竞争条件。
更新
您的代码具有导致问题的以下代码:
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
new
语句在 lambda 内,它将导致新的
UnityServiceLocator
每次调用
ServiceLocator.Current
时都会创建.这本身并不坏,但是
UnityServiceLocator
的构造函数调用
container.RegisterInstance
在容器中注册自己。但正如我已经说过的:调用 RegisterInstance` 不是线程安全的。
RegisterInstance
不会替换现有注册,但会将其附加到注册列表中。这意味着
UnityServiceLocator
的列表容器中的实例将不断增长,并最终导致系统因 OutOfMemoryException 而崩溃。您实际上很幸运,您首先遇到了这个并发错误,因为 OOM 错误更难追溯。
UnityServiceLocator
的构造退出 lambda 并每次返回该单个实例:
var locator = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => locator);
UnityServiceLocator
的行为在我看来是一个设计缺陷,因为
RegisterInstance
不是线程安全的,
UnityServiceLocator
不知道它被创建了多少次,它不应该调用
RegisterInstance
从它的构造函数中 - 或者至少 - 不检查注册该实例是否安全。
RegisterInstance
的调用是一个突破性的变化,但仍然可能是 Unity 团队的最佳解决方案。大多数用户可能不会注意到丢失的
IServiceLocator
无论如何注册,如果他们这样做,Unity 将在这种情况下传达明确的异常消息。另一种选择是让
UnityServiceLocator
检查是否已经从容器中解析了任何实例,在这种情况下,从
UnityServiceLocator
中抛出 InvalidOperationException。的构造函数。
关于asp.net-mvc - unity and Random "Index was outside the bounds of the array"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24822232/
这个问题已经有答案了: 已关闭14 年前。 ** 重复:What's the difference between X = X++; vs X++;? ** 所以,即使我知道你永远不会在代码中真正做到
我在一本C语言的书上找到了这个例子。此代码转换输入数字基数并将其存储在数组中。 #include int main(void) { const char base_digits[16] =
尝试使用“pdf_dart”库保存 pdf 时遇到问题。 我认为问题与我从互联网下载以尝试附加到 pdf 的图像有关,但我不确定它是什么。 代码 import 'dart:io'; import 'p
我的 Apache 服务器曾经可以正常工作,但它随机开始对几乎每个目录发出 403 错误。两个目录仍然有效,我怎样才能使/srv/www/htdocs 中的所有目录正常工作? 我查看了两个可用目录的权
这些索引到 PHP 数组的方法之间有什么区别(如果有的话): $array[$index] $array["$index"] $array["{$index}"] 我对性能和功能上的差异都感兴趣。 更
我有一个简单的结构,我想为其实现 Index,但作为 Rust 的新手,我在借用检查器方面遇到了很多麻烦。我的结构非常简单,我想让它存储一个开始值和步长值,然后当被 usize 索引时它应该返回 st
我对 MarkLogic 中的 element-range-index 和 field-range-index 感到困惑。 请借助示例来解释差异。 最佳答案 这两个都是标量索引:特定类型的基于值的排序
我对 MarkLogic 中的 element-range-index 和 field-range-index 感到困惑。 请借助示例来解释差异。 最佳答案 这两个都是标量索引:特定类型的基于值的排序
所以我有一个 df,我在其中提取一个值以将其存储在另一个 df 中: import pandas as pd # Create data set d = {'foo':[100, 111, 222],
我有一个由 codeigniter 编写的网站,我已经通过 htaccess 从地址中删除了 index.php RewriteCond $1 !^(index\.php|resources|robo
谁能告诉我这两者有什么区别: ALTER TABLE x1 ADD INDEX(a); ALTER TABLE x1 ADD INDEX(b); 和 ALTER TABLE x1 ADD INDEX(
我在 Firefox 和其他浏览器上遇到嵌套 z-index 的问题,我有一个 div,z-index 为 30000,位于 label 下方> zindex 为 9000。我认为这是由 z-inde
Link to the function image编写了一个函数来查找中枢元素(起始/最低)的索引 排序和旋转数组。我解决了这个问题并正在检查 边缘情况,它甚至适用于索引为零的情况。任何人都可以 解
我正在尝试运行有关成人人口普查数据的示例代码。当我运行这段代码时: X_train, X_test, y_train, y_test = cross_validation.train_test_spl
我最近将我的 index.html 更改为 index.php - 我希望能够进行重定向以反射(reflect)这一点,然后还进行重写以强制 foo.com/index.php 成为 foo.com/
我最近将我的 index.html 更改为 index.php - 我希望能够进行重定向以反射(reflect)这一点,然后还进行重写以强制 foo.com/index.php 成为 foo.com/
我有一个用户定义的函数,如下所示:- def genre(option,option_type,*limit): option_based = rank_data.loc[rank_data[
我有两个巨大的数据框我正在合并它们,但我不想有重复的列,因此我通过减去它们来选择列: cols_to_use=df_fin.columns-df_peers.columns.difference(['
感谢您从现在开始的回答, 我是React Native的新手,我想做一个跨平台的应用所以我创建了index.js: import React from 'react'; import { Co
我知道 not_analyzed 是什么意思。简而言之,该字段不会被指定的分析器标记化。 然而,什么是 NO_NORMS 方法?我看到了文档,但请用简单的英语解释我。什么是索引时间字段和文档提升和字段
我是一名优秀的程序员,十分优秀!