2017-10-11 28 views
1

WebApiプロジェクトでUnityを使用していて、設定ファイルを使用してタイプを登録しています。私は、コントローラのコンストラクタへの依存性を注入してきたし、それは見事に取り組んできました...私はタイプマッピングを忘れてしまったし、次のエラー受信まで:ResolutionFailedExceptionが成功として返される

An error occurred when trying to create a controller of type 'FooController'. Make sure that the controller has a parameterless public constructor.

問題は、私はエラーを受け取ったことはないですが。私はそれを解決するためにマッピングを簡単に追加することができます。問題は内部がクライアントと共有され、クライアントがHTTPステータスコード200を受け取ったことです。

私は同様の記事(Unity wraps exception to the ResolutionFailedException. How to avoid?)を読んでいますが、これらの回答は役に立ちませんでした。解決策がない、または話題になっていると述べています。解決策がないということを私が受け入れることは難しいので、より良い答えが得られることを期待して質問を言い換えることができたと思いました。

代わりにHttp Error 500が返されるようにResolutionFailedExceptionをキャッチ/インターセプトできますか?私が聞くことができるイベントや、何らかの方法でラップできるクラスはありますか?たとえば、DefaultHttpControllerActivatorをラップし、ラッパーを使用して例外をキャッチし、HTTP 500エラーとして返すことはできますか? CustomErrors設定の設定に基づいてクライアントからHTTP 500をフィルタリングすることが期待されます。また、クライアントに詳細を返す代わりに、エラーをログファイルに記録することもできます。ここで

は、XMLなどのエラーの例です:

<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
An error occurred when trying to create a controller of type 'FooController'. Make sure that the controller has a parameterless public constructor. 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext() 
</StackTrace> 
<InnerException> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
Resolution of the dependency failed, type = "MyWebApi.API.Controllers.FooController", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, MyWebApi.DataProvider.Interfaces.IFooProvider, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- 
At the time of the exception, the container was: Resolving MyWebApi.API.Controllers.FooController,(none) Resolving parameter "FooProvider" of constructor MyWebApi.API.Controllers.FooController(MyWebApi.DataProvider.Interfaces.IFooProvider FooProvider) Resolving MyWebApi.DataProvider.Interfaces.IFooProvider,(none) 
</ExceptionMessage> 
<ExceptionType> 
Microsoft.Practices.Unity.ResolutionFailedException 
</ExceptionType> 
<StackTrace> 
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) 
at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) 
at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides) 
at Microsoft.Practices.Unity.WebApi.UnityDependencyResolver.SharedDependencyScope.GetService(Type serviceType) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
</StackTrace> 
<InnerException> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
The current type, MyWebApi.DataProvider.Interfaces.IFooProvider, is an interface and cannot be constructed. Are you missing a type mapping? 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) 
at lambda_method(Closure , IBuilderContext) 
at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) 
at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) 
at lambda_method(Closure , IBuilderContext) 
at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) 
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) 
</StackTrace> 
</InnerException> 
</InnerException> 
</Error> 
+0

グローバルな 'Application_Error()'でトラップできますか? – Jasen

+0

いいえ、私はそうではありません。エラーが何かに巻き込まれているように見えて、本体のエラーのXML表現を含むHTTP 200応答としてパッケージ化されているようです。私はそれが何であるか分からない。 – KrimblKrum

答えて

0

私はここに答えが見つかりました:

  • フル:私の好みのソリューションを実装する際に、私は次の事を学びましたcatch all unhandled exceptions in ASP.NET Web Api

    をスタックトレースのエラーは、CustomErrors設定のためにのみ表示されます。私はすべてをローカルで実行していて、CustomErrors = RemoteOnlyを実行していたことを忘れていました。

  • 例外をキャッチしてログに記録する場合は、IExceptionLoggerを追加します。これは現在のニーズに対応するためのソリューションです。
  • 例外をキャッチしてレスポンスを操作する場合は、を置き換えます。 IExceptionHandler。これは知っておくと良いことです。
  • いずれの解決策もすべての例外をキャプチャしません。私はまだ私のApiControllerメソッド内でキャッチされていない例外をキャプチャするためにApplication_Errorを使用しています。
関連する問題