Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:27:33+00:00 2026-05-26T05:27:33+00:00

Should it throw an exception, pass resolution to its parent or something else? My

  • 0

Should it throw an exception, pass resolution to its parent or something else?

My favorite container Autofac raises an exception – which is the only thing I don’t like about it.
I think that it should pass resolution to its parent which should resolve my issue with this code


    class LazyClass
    {
        public void DoSomething() { }
    }

    class SomeClass
    {
        public event EventHandler WatchOut = (s, ea) => { };
        public void Start()
        {
            WatchOut(this, EventArgs.Empty);
        }
    }

    class LazyInterceptor
    {
        Lazy<LazyClass> lazy;
        public LazyInterceptor(Lazy<LazyClass> lazy)
        {
            this.lazy = lazy;
        }
        public void Register(SomeClass some)
        {
            some.WatchOut += (s, ea) => lazy.Value.DoSomething();
        }
    }

    [TestMethod]
    public void LazyAndEvents()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<LazyClass>().SingleInstance();
        var container = builder.Build();

        var someClass = new SomeClass();

        using (var inner = container.BeginLifetimeScope(cb => 
            cb.RegisterType().SingleInstance()))
        {
            var interceptor = inner.Resolve();
            interceptor.Register(someClass);
        }

        someClass.Start();
    }

I know of three workarounds around this, but they all seem just wrong

  • not disposing container – this is what I currently do, but that’s just to make Autofac happy
  • explicitly registering lazy – seems just wrong and I don’t even know what will be resolved
  • taking ownership for dispose – it doesn’t seem as bad, but I don’t use object in that way, so again it looks like a workaround. Also, in Autofac it introduces dependency which is not nice
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:27:34+00:00Added an answer on May 26, 2026 at 5:27 am

    The whole point of lifetime scopes is that to control the lifetime of the contained instances. Building on the Disposable pattern, disposing the container is a clean and well-established way of releasing the resources governed by that container. Thus, when a lifetime scope is disposed you should no longer depend on instances resolved from that lifetime scope.

    So I would say that you are definitively using Autofac the wrong way here. Perhaps if you explained why you want to use the scope like that, we could figure out the proper usage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a when in JBehave which under certain circumstances should throw an exception.
What exception should I throw if I encounter an illegal state - for instance,
According to the UserNamePasswordValidator sample on http://msdn.microsoft.com/en-us/library/system.identitymodel.selectors.usernamepasswordvalidator.aspx one should throw a SecurityTokenException if the
Why does Convert.ToBoolean(1) throw a System.FormatException ? How should I proceed with this conversion?
SVector.H: void pop_back() throw (underflow_error); In my SVector.cpp file, should I also include the
I hope this doesn't come across as a stupid question but its something I
I have a factory class that decides which of four available subclasses it should
Possible Duplicate: C++ STL stack question: Why does pop() not throw an exception if
Speaking generally, is the caller that should guarantee to pass well-formed data to the
Should I start with Django or JavaScript?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.