枚举Autofac使用寿命跟踪的一次性用品 [英] Enumerating disposables tracked by an Autofac lifetime

查看:122
本文介绍了枚举Autofac使用寿命跟踪的一次性用品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Autofac使用生存期作用域作为处理在一个工作单元中创建的所有组件的一种方式.尽管这是一项强大的功能,但编写不正确设置生命周期范围的代码很容易,这导致跟踪的一次性物品的数量随着时间的推移而增长:有效地导致内存泄漏.

Autofac uses lifetime scopes as a way of disposing of all of the components created during a unit of work. While this is a powerful feature, it is easy to write code that doesn't dispose the lifetime scopes properly, which results in the number of tracked disposables growing over time: effectively a memory leak.

有没有一种方法可以监视Lifetime Scope在任何时间点跟踪的一次性对象的数量.我对编写工具感兴趣,可以帮助我发现与未正确分配一次性用品到工作单位有关的问题.目前,我使用内存分析器工具查找泄漏,但这是一项繁重的工作.

Is there a way of monitoring the number of Disposable objects being tracked by a Lifetime Scope at any point in time. I'm interested in writing tool to help me find issues related to not properly assigning disposables to units of work. At the moment I use a memory profiler tool to find the leaks, but this is pretty onerous work.

我已经看过ILifetimeScope的公共接口,但是看不到任何有用的东西.

I've looked at the public interface of ILifetimeScope but do not see anything that is of use.

推荐答案

不幸的是,当前分析是Autofac的薄弱环节之一. 有一个存储库,在该存储库上已开始对分析软件包进行一些工作,但是肯定存在一些空白-例如,您可以跟踪对象何时被激活,并且可以查看何时设置生存期,但是您不能跟踪何时单个对象 >作为作用域的一部分处理.没有任何活动.

Unfortunately, analytics is one of Autofac's weaker spots at the moment. There is a repository where some work was started on an analytics package but there are definitely some gaps - for example, you can track when an object is activated and you can see when lifetime scopes are disposed, but you can't track when individual objects are disposed as part of a scope. There's just no event for it.

一个非常简单的激活跟踪模块如下所示:

A very simple tracking module for activations would look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;

namespace DiagnosticDemo
{
  public class TrackingModule : Module
  {
    private readonly IDictionary<Type, int> _activations = new Dictionary<Type, int>();

    private readonly object _syncRoot = new object();

    public void WriteActivations()
    {
      foreach (var pair in this._activations.Where(p => p.Value > 0))
      {
        Console.WriteLine("* {0} = {1}", pair.Key, pair.Value);
      }
    }

    protected override void AttachToComponentRegistration(
      IComponentRegistry componentRegistry,
      IComponentRegistration registration)
    {
      if (registration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
      {
        registration.Activated += this.OnRegistrationActivated;
      }
    }

    private void OnRegistrationActivated(
      object sender,
      ActivatedEventArgs<object> e)
    {
      if (e.Instance is IDisposable)
      {
        var type = e.Instance.GetType();
        Console.WriteLine("Activating {0}", type);
        lock (this._syncRoot)
        {
          if (this._activations.ContainsKey(type))
          {
            this._activations[type]++;
          }
          else
          {
            this._activations[type] = 1;
          }
        }
      }
    }
  }
}

您将使用类似以下的方式:

You would use it something like this:

static void Main(string[] args)
{
  var trackingModule = new TrackingModule();

  var builder = new ContainerBuilder();
  // Register types, then register the module
  builder.RegisterType<Consumer>().As<IConsumer>();
  builder.RegisterType<DisposableDependency>().As<IDependency>();
  builder.RegisterType<NonDisposableDependency>().As<IDependency>();
  builder.RegisterModule(trackingModule);
  var container = builder.Build();

  // Do whatever it is you want to do...
  using (var scope = container.BeginLifetimeScope())
  {
    scope.Resolve<IConsumer>();
  }

  // Dump data
  Console.WriteLine("Activation totals:");
  trackingModule.WriteActivations();
}

但是,这不会告诉您不会处理哪些项目,这是您想知道的.不过,它可能会为您带来一些想法,或者至少有所帮助.

However, this isn't going to tell you which items weren't disposed, which is I think what you want to know. It may get you some ideas, though, or at least help a bit.

如果您有兴趣帮助改进Autofac中的分析,我们很乐意进行PR或有关如何改进的具体设计思路.

If you're interested in helping to improve the analytics in Autofac, we'd love to take PRs or specific design ideas for how to improve.

这篇关于枚举Autofac使用寿命跟踪的一次性用品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆