我遇到过一个我无法理解的性能问题.我知道如何解决它,但我不明白为什么会这样.这只是为了好玩!
我们来谈谈代码.我尽可能地简化了代码以重现问题.
假设我们有一个泛型类.它内部有一个空列表,并T
在构造函数中执行某些操作.它具有Run
调用IEnumerable<T>
列表上的方法的方法,例如Any()
.
public class BaseClass<T>
{
private List<T> _list = new List<T>();
public BaseClass()
{
Enumerable.Empty<T>();
// or Enumerable.Repeat(new T(), 10);
// or even new T();
// or foreach (var item in _list) {}
}
public void Run()
{
for (var i = 0; i < 8000000; i++)
{
if (_list.Any())
// or if (_list.Count() > 0)
// or if (_list.FirstOrDefault() != null)
// or if (_list.SingleOrDefault() != null)
// or …
Run Code Online (Sandbox Code Playgroud) 我有一个MVC WebApi owin(软托管)项目,它使用Unity来解析控制器依赖项
看起来像这样
public class PacientaiController : ODataController
{
private readonly IEntityRepo<Ent.Pacientas> repo;
public PacientaiController(IEntityRepo<Ent.Pacientas> repo)
{
this.repo = repo;
}
Run Code Online (Sandbox Code Playgroud)
我试图解决的问题是 - 如何将'OwinContex'传递给回购.
public class PacientasEntityRepo:IEntityRepo<Pacientas>,IDisposable
{
public PacientasEntityRepo(IOwinContext ctx)
{
.........
Run Code Online (Sandbox Code Playgroud)
如果我尝试在这里注册它 Startup.cs
Container.RegisterType<IOwinContext>(new InjectionFactory(o => HttpContext.Current.GetOwinContext()));
Run Code Online (Sandbox Code Playgroud)
我得到一个空引用,说这HttpContext.Current
是NULL
这里的主要思想是将当前经过身份验证的用户传递给repo,因为Repo托管逻辑以查询数据库,具体取决于用户.(如果用户是Admin,则返回此数据,如果用户是guest - 返回此数据)
关键是 - 这是一个自我主持人!
我想替换对象来返回不同对象的序列.例如:
var http = Substitute.For<IHttp>();
http.GetResponse(Arg.Any<string>()).Returns(resourceString, resourceString2);
http.GetResponse(Arg.Any<string>()).Returns(x => { throw new Exception(); });
Run Code Online (Sandbox Code Playgroud)
将返回resourceString然后是resourceString2然后返回异常.
或类似的东西:
var http = Substitute.For<IHttp>();
http.GetResponse(Arg.Any<string>()).Returns(resourceString, x => { throw new Exception(); }, resourceString2);
Run Code Online (Sandbox Code Playgroud)
将返回resourceString然后异常,然后是resourceString2.
我怎样才能做到这一点?
我在西欧和标准模式下设置了azure网站.突然今天在2014年1月30日03:00 UTC开始不断重启应用程序池.ShutdownReason是HostingEnvironment.我在eventlog.xml中有很多这样的事件:
<Event>
<System>
<Provider Name="W3SVC-WP"/>
<EventID>2299</EventID>
<Level>3</Level>
<Task>0</Task>
<Keywords>Keywords</Keywords>
<TimeCreated SystemTime="9:14:50 AM"/>
<EventRecordID>15807234</EventRecordID>
<Channel>Application</Channel>
<Computer>RD00155D3A08C6</Computer>
<Security/>
</System>
<EventData>
<Data>Worker Process requested recycle due to 'Slow Requests' limit.
</Data>
</EventData>
</Event>
Run Code Online (Sandbox Code Playgroud)
我找不到有关"慢速请求"限制的任何信息.
这些重新启动与代码无关,一周内没有任何更改.只有将网站模式切换为共享才能帮助我.
这是它第二次发生,我不明白这种行为的原因.
我正在尝试为多模块项目设置 Maven 插件管理。我想使用pluginManagement
没有继承的部分(指定不在父 pom 中的插件版本)。我的项目结构看起来像这样
base_project
-- pom.xml
-- project-bom
-- pom.xml
-- project-a
-- pom.xml
Run Code Online (Sandbox Code Playgroud)
在根 pom.xml 中我只有两个模块:
<modules>
<module>project-bom</module>
<module>project-a</module>
</modules>
Run Code Online (Sandbox Code Playgroud)
在 project-bom pom.xml 中,我指定了包含dependencyManagement
我pluginManagement
想要使用的依赖项和插件版本的部分:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.some</groupId>
<artifactId>dependency</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.some</groupId>
<artifactId>plugin</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Run Code Online (Sandbox Code Playgroud)
在project-a pom.xml中,我导入project-bom pom并使用依赖项和插件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.project</groupId>
<artifactId>project-bom</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.some</groupId>
<artifactId>dependency</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.some</groupId>
<artifactId>plugin</artifactId>
</plugin>
</plugins> …
Run Code Online (Sandbox Code Playgroud)