title: How to return NULL in an Autofac registration?
body: <p>Is there a way to return null with autofac? I get a exception if I do this.</p>
  
  <p>In some cases I cant resolve a object and should return null.</p>
  
  <pre><code>m_builder.Register &lt; IMyObject &gt; ((c, p) =&gt; {
    //do something
  
    if (...) {
      //cant create/resolve object (error case)
      return null;
    } else {
      return new IMyObject();
    }
  
  }).InstancePerHttpRequest();
  </code></pre>
  
  <p>Is there a build-in way to do this with autofac?</p>
  
  <p>Edit:
  I want to return null. Or do something else to inform the user that the creation was not successful. If I return null this exception will be thrown:</p>
  
  <p>DependencyResolutionException:</p>
  
  <blockquote>
    <p>A delegate registered to create instances of 'IMyObject' returned
    null.</p>
  </blockquote>
  
  <p>Stacktrace:    </p>
  
  <blockquote>
    <p>at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext
    context, IEnumerable<code>1 parameters)<br>
    at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable</code>1
    parameters)<br>
    at Autofac.Core.Resolving.InstanceLookup.b__0()<br>
    at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id,
    Func<code>1 creator)<br>
    at Autofac.Core.Resolving.InstanceLookup.Execute() 
    at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope
    currentOperationScope, IComponentRegistration registration,
    IEnumerable</code>1 parameters)<br>
    at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration
    registration, IEnumerable<code>1 parameters)<br>
    at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration
    registration, IEnumerable</code>1 parameters)<br>
    at Autofac.ResolutionExtensions.TryResolveService(IComponentContext
    context, Service service, IEnumerable<code>1 parameters, Object&amp; instance) 
    at Autofac.ResolutionExtensions.ResolveService(IComponentContext
    context, Service service, IEnumerable</code>1 parameters)<br>
    at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext
    context, IEnumerable`1 parameters)<br>
    at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext
    context, Parameter[] parameters)<br>
    at RestApi.Controllers.MyApiController.CreateEntityContext() in
    C:...</p>
  </blockquote>