0

私のプロジェクトでは Autofac を使用しています。以前は正常に動作していました。Autofac に新しいコードを追加したところ、「The creation delegate must not return null」というエラーが表示されました。

私が行った変更は次のとおりです

 builder.Register<Rep>(c => {
                                      /*Get Session and LoginId*/

                               return session.CreateQuery(@"from Rep where LoginId='" +
                                                   LoginId+ "'").List<Rep>().
                                                   FirstOrDefault() ?? new Rep();
                            });
 builder.Register<TestPermissionHelper>();

今、Global.asaxメソッドApplicaiton_PostAuthenticate()で私は解決してTestPermissionHelperいます。

興味深いことに、ログインページにアクセスするとエラーが発生しますが、それ以外は正常に機能しています。

この小さなコードから全体像を理解するのは難しいことはわかっています。一般的に、このタイプのエラーの考えられる理由は何か、誰かが少なくとも私に教えてくれれば素晴らしいことです。

ありがとう

注:それが役立つ場合は、エラーのスタックトレースも提供しています

    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)
4

2 に答える 2

0

この問題の根本的な原因はAutofacにあるようには見えません。RepテーブルにログインIDのエントリがない場合は、新しいRepオブジェクトを作成します。が他のコンポーネントの依存関係として使用されている場合は、それらがRepオブジェクトのインスタンス化されていない参照プロパティを参照していないことを確認する必要があります。

于 2010-05-12T12:27:19.933 に答える
0

nullあなたのコードは十分に良さそうで、エラーが発生しない限り、デリゲート登録がどのように返されるかわかりません。

TestPermissionHelper登録からのさまざまな戻り値 (null を含む) で解決を試みる単体テストを作成することをお勧めしますRep。エラーを再現し、エラーが発生した状況を特定できることを願っています。

于 2010-05-11T18:47:53.557 に答える