views:

70

answers:

2

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)
A: 

Your code seems good enough and I cannot see how the delegate registration would ever return null unless some error occurs.

I would suggest writing some unit tests that 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.

Peter Lillevold
A: 

The root cause of the issue does not seem to be in Autofac. In case the Rep table has no entry for the login id you create a new Rep object. If is used as a dependency in other components, you need to ensure that they don't refer to non-instantiated reference properties of the Rep object.

Zuber
Yup the problem was that I was accessing the Rep's properties which were null therefore it was giving error.
Bipul