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 3399494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:46:41+00:00 2026-05-18T04:46:41+00:00

Is it possible to resolve a service in autofac without registering it with the

  • 0

Is it possible to resolve a service in autofac without registering it with the container?

So far I have come up with something like this:

    public object ResolveUnknownService(IContainer container, Type serviceType)
    {
        ILifetimeScope lifetimeScope = container.BeginLifetimeScope(b => b.RegisterType(serviceType).ExternallyOwned());
        try
        {
            return lifetimeScope.Resolve(serviceType);
        }
        finally
        {
            lifetimeScope.Dispose();
        }
    }

It works, but I am a bit worried about performance. I also don’t want to use AnyConcreteTypeNotAlreadyRegisteredSource beacuse I want to be specific about which services I want (or don’t want) to resolve this way.

  • 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-18T04:46:41+00:00Added an answer on May 18, 2026 at 4:46 am

    In your example, disposing the temporary lifetime scope will dispose the object being returned as well as any of its dependencies; components only live as long as the lifetime scope that they were resolved from.

    Are there any known criteria that identifies these types? There is an overload of the AnyConcrete... class’s constructor that can specify a filter on what it will resolve. If there are such criteria though then RegisterAssemblyTypes() is almost always a better option.

    (Unless you’re integrating with a legacy/third-party framework then this pattern is probably not the best way to use Autofac, as it leads down the Service Locator path. YMMV of course.)

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

Sidebar

Related Questions

I have run into an issue while creating a data service and using Autofac
I have declared two service contracts as follows: [ServiceContract] public interface IContract1 { [OperationContract]
When I use this registration: container.Register( Component .For<IFooFactory>() .ImplementedBy<FooFactory>(), Component .For<IFoo>() .UsingFactoryMethod(kernel => kernel.Resolve<IFooFactory>().CreateFoo())
Is it possible to easily configure autofac so it will only resolve using non-obsolete
Possible Duplicate: Injecting data to a WCF service I have a WCF service that
I have an executable that is started by a windows service, this program will
is it possible to resolve objects which use generics in unity only using interfaces.
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
Possible Duplicate: options for restoring appengine datastore data? I'd like to parse the files
Possible Duplicate: Properly handling spaces and quotes in bash completion I would like to

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.