为什么未经我的许可就处置我的自动常见问题范围?

时间:2018-06-27 15:37:43

标签: .net autofac

我正在尝试在我的dot net项目中使用auto-fac来处理依赖关系。我创建了scopeContainer来管理在程序中单击“新建”时重置范围。不知何故,我的ChildScope在不经接触的情况下被处置。是否有任何autofac功能会在我不知情的情况下处理我的ChildScope?

ScopeContainer:

    /// <summary>
    /// Scope container. Manage IOC container
    /// </summary>
    public class ScopeContainer
    {
        #region Fields

        /// <summary>
        /// The bootstrapper
        /// </summary>
        [NotNull] private static readonly Bootstrapper Bootstrapper;

        /// <summary>
        /// Backing field to base scope.
        /// </summary>
        private static ILifetimeScope _baseScope;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes static members of the <see cref="ScopeContainer" /> class.
        /// </summary>
        static ScopeContainer()
        {
            Bootstrapper = new Bootstrapper();
            Bootstrapper.Configure();
            _baseScope = Bootstrapper.Container.BeginLifetimeScope(ContainerConstants.SCOPE_BASESCOPE);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the view topmost view model scope.
        /// </summary>
        public static ILifetimeScope BaseScope => _baseScope;

        #endregion

        #region Methods

        /// <summary>
        /// Get a new child lifetime scope
        /// </summary>
        /// <param name="scopeName">Name of scope.</param>
        /// <returns>A new scope</returns>
        public static ILifetimeScope GetNewChildScope(string scopeName = null)
        {
            return string.IsNullOrWhiteSpace(scopeName) ? BaseScope?.BeginLifetimeScope() : BaseScope?.BeginLifetimeScope(scopeName);
        }

        /// <summary>
        /// Reset the view model scopes
        /// </summary>
        public static void ResetScope()
        {
            _baseScope?.Dispose();
            _baseScope = Bootstrapper.Container.BeginLifetimeScope(ContainerConstants.SCOPE_BASESCOPE);
        }

        #endregion

引导程序

/// <summary>
/// In charge of setting up IOC behavior
/// </summary>
public class Bootstrapper
{
    #region Properties

    /// <summary>
    /// Gets or sets the IOC Container
    /// </summary>
    public IContainer Container { get; set; }

    #endregion

    #region Methods

    /// <summary>
    /// Configures the container
    /// </summary>
    public void Configure()
    {
        if (null != Container) { return; }

        var builder = new ContainerBuilder();

        // View Models:

        // Per Lifetime
        builder.RegisterType<TonesViewModel>().As<ITonesViewModel>();
        builder.RegisterType<ProgramViewModel>().As<IProgramViewModel>();
        ...

        Container = builder.Build();
    }

    #endregion
}

0 个答案:

没有答案