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

  • SEARCH
  • Home
  • 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 6136091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:33:38+00:00 2026-05-23T17:33:38+00:00

I user Autofac and wonder if it is possible to come around the DependencyResolutionException

  • 0

I user Autofac and wonder if it is possible to come around the DependencyResolutionException that is thrown if the instance that is returned by a registered type is null?
Consider the following example:

builder.Register( c => c.Resolve<HttpContextBase>().CurrentHandler as ITemplatePage
                ).InstancePerHttpRequest();

The example will return null if the current handler (for example the aspx-page that I am visiting) isn’t of type ITemplatePage, and Autofac will throw the DependencyResolutionException even if I use the ResolveOptional alternative like in this way:

container.ResolveOptional<ITemplatePage>(); 

Is there any way to come arond this and get an behaviour where Autofac returns null instead of thowing an exeption?

I have worked around this by adding a IsNull-proerty to ITemplatePage and always instanciate it, like this:

builder.Register( c => 
                  c.Resolve<HttpContextBase>().CurrentHandler as ITemplatePage
                  ?? new TemplatePage(true)
                 ).As<ITemplatePage>().InstancePerHttpRequest();

and then I can work with it in this way:

var templatePage = container.Resolve<ITemplatePage>();  
if(!templatePage.IsNull){
   // Do stuff
}

But I do not think that is an optimal solution to my problem.

Edit:
To catch the error is not an option since I use ITemplatePage in constructors of other types I resolve, and since I am using constructor injection to resolve these types I want them to return the instance created with the constructor without the ITemplatePage parameter rather than to crash as well.

  • 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-23T17:33:39+00:00Added an answer on May 23, 2026 at 5:33 pm

    The best way to do this is to register the service conditionally at the start of a web request, e.g. (pseudocode):

    void Application_BeginRequest()
    {
        var tp = HttpContext.CurrentHandler as ITemplatePage;
        if (tp != null)
        {
            var updater = new ContainerBuilder();
            updater.RegisterInstance(tp);
            updater.Update(ContainerProvider.RequestLifetime.ComponentRegistry);
        }
    }
    

    This way, you can simply make the ITemplatePage an optional constructor parameter of components that can use it (ITemplatePage tp = null) or use property injection/ResolveOptional().

    Note there have been some reliability improvements around these kinds of delayed registrations in Autofac 2.5 so I recommend updating before using this approach.

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

Sidebar

Related Questions

User x = null; object o = x; // determine type with only reference
User may input character like Ā Ē ú, how to remove that mark and
User Class - Name - Picture Friend Class - Profile Of Type User -
User.findOne({}, function(err, doc){ //do stuff with doc here. }); That's the way I currently
User equals untrustworthy. Never trust untrustworthy user's input. I get that. However, I am
User: columns: id: type: integer(4) autoincrement: true primary: true username: type: string(255) Group: tableName:
User.should_receive(:update_attributes).with({'these' => 'params'}) What does that statement mean? these isn't instantiated anywhere as meaning
USER = user PASS = pass QUERY = SELECT c1, c2, c3, c4, c5,
User is a model. I have 100 users. I need to present the list
User Controls -- Do they serve a special purpose? As far as I can

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.