kala-tamin Error: The creation delegate must not return null in Autofac?


Error: The creation delegate must not return null in Autofac?



In my project I am using Autofac. Previously it was working fine. Now I have added some new code in Autofac and it's giving me "The creation delegate must not return null " error.

The changes that I have made are as follows

 builder.Register<Rep>(c => {                                       /*Get Session and LoginId*/                                 return session.CreateQuery(@"from Rep where LoginId='" +                                                    LoginId+ "'").List<Rep>().                                                    FirstOrDefault() ?? new Rep();                             });  builder.Register<TestPermissionHelper>(); 

Now in the Global.asax, in Applicaiton_PostAuthenticate() method I am resolving TestPermissionHelper.

The interesting thing is that, when I am hitting the login page at the time it is giving me error, else it is working fine.

I know it is hard to understand the entire picture from this small code. It will be great if someone can at least tell me, in general what are the possible reason for this type of error.

Thanks

Note: If it can help, I am also giving the Stack Trace of the error

    Stack trace:    at Autofac.Component.Activation.DelegateActivator.ActivateInstance(IContext context, IEnumerable`1 parameters)    at Autofac.Component.Registration.ResolveInstance(IContext context, IEnumerable`1 parameters, IDisposer disposer, Boolean& newInstance)    at Autofac.Context.TryResolve(Service service, Object& instance, IEnumerable`1 parameters)    at Autofac.Context.Resolve(Service service, IEnumerable`1 parameters)    at Autofac.Context.Resolve(Type serviceType, IEnumerable`1 parameters)    at Autofac.Component.Activation.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()    at Autofac.Component.Activation.DirectConstructorInvoker.<InvokeConstructor>b__0(Func`1 pa)    at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)    at Autofac.Component.Activation.DirectConstructorInvoker.InvokeConstructor(IContext context, IEnumerable`1 parameters, ConstructorInfo ci, Func`1[] args)    at Autofac.Component.Activation.ReflectionActivator.ConstructInstance(ConstructorInfo ci, IContext context, IEnumerable`1 parameters, Func`1[] parameterAccessors)    at Autofac.Component.Activation.ReflectionActivator.ActivateInstance(IContext context, IEnumerable`1 parameters)    at Autofac.Component.Registration.ResolveInstance(IContext context, IEnumerable`1 parameters, IDisposer disposer, Boolean& newInstance)    at Autofac.Context.TryResolve(Service service, Object& instance, IEnumerable`1 parameters)    at Autofac.Context.Resolve(Service service, IEnumerable`1 parameters)    at Autofac.Context.Resolve(Type serviceType, IEnumerable`1 parameters)    at Autofac.Context.Resolve[TService](IEnumerable`1 parameters)    at Autofac.Context.Resolve[TService](Parameter[] parameters)    at Autofac.Container.Resolve[TService](Parameter[] parameters) 

Detect if an element is selected in listbox or not through jquery

1:

asp.net mvc jquery how to enable the button after disable
The root cause of the issue does not seem to be in Autofac. Creating Database Mocks in ASP.NET MVC using Data from Existing Database In case the Rep table has no entry for the login id you create a new Rep object. Is it possible to configure ELMAH entirely in code?If is used as a dependency in another components, you need to ensure this they don't refer to non-instantiated reference properties of the Rep object.. How to design configurable MVC panels?
How to get Controller name, and data available in a base Controller?Anonymous Type syntax in ASP.NET MVC

2:

can hidden variables value be accessed in a controller in MVC
Your code seems good enough and I cannot see how the delegate registration would ever return null unless any error occurs.. I would suggest writing any unit tests this tries out resolving TestPermissionHelper with various return values (including null) from the Rep registration. Hopefully you're able to reproduce the error and isolate the condition in which the error occurs..


83 out of 100 based on 83 user ratings 233 reviews