Errors Using a Custom Dependent Transformer with a Unity Controller and Web API

I use the following class as a dependency converter. Got a link from http://www.asp.net/web-api/overview/advanced/dependency-injection

public class UnityWebAPIResolver : IDependencyResolver { protected IUnityContainer container; public UnityWebAPIResolver(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; } public object GetService(Type serviceType) { try { return container.Resolve(serviceType); **// This line throws error** } catch (ResolutionFailedException) { return null; } } public IEnumerable<object> GetServices(Type serviceType) { try { return container.ResolveAll(serviceType); **// This line throws error** } catch (ResolutionFailedException) { return new List<object>(); } } public IDependencyScope BeginScope() { var child = container.CreateChildContainer(); return new UnityWebAPIResolver(child); } public void Dispose() { container.Dispose(); } } 

In the WebApiConfig class, after configuring the route, I configure the dependency converter as follows

 config.DependencyResolver = new UnityWebAPIResolver(UnityConfig.GetContainer()); 

The problem is that I get several such errors.

 InvalidOperationException - The type IHostBufferPolicySelector does not have an accessible constructor. InvalidOperationException - The type ModelMetadataProvider does not have an accessible constructor. InvalidOperationException - The type ITraceManager does not have an accessible constructor. InvalidOperationException - The type ITraceWriter does not have an accessible constructor. InvalidOperationException - The type IHttpControllerSelector does not have an accessible constructor. InvalidOperationException - The type IAssembliesResolver does not have an accessible constructor. InvalidOperationException - The type IHttpControllerTypeResolver does not have an accessible constructor. InvalidOperationException - The type IHttpActionSelector does not have an accessible constructor. InvalidOperationException - The type IActionValueBinder does not have an accessible constructor. InvalidOperationException - The type IContentNegotiator does not have an accessible constructor. InvalidOperationException - The type IHttpControllerActivator does not have an accessible constructor. InvalidOperationException - The type IBodyModelValidator does not have an accessible constructor. 

Even if I try to do something similar in my global.asax, I get the same errors.

 GlobalConfiguration.Configuration.DependencyResolver = new UnityWebAPIResolver(UnityConfig.GetContainer()); 

Question : All the dependencies in my API controller seem to be correctly entered , my only problem is that it cannot eliminate a few of the above (platform-specific) , is there any chance that this could lead to the failure of the whole system and cause random mistakes?

+7
c # asp.net-web-api unity-container
source share
1 answer

No problem, and your program works as expected. What you see are the first chance exceptions thrown by the Unity Resolve method. An exception is thrown because Unity will never return null when the service cannot be resolved. However, implementations of IDependencyResolver.GetService should always return null if the requested service is not registered with the dependency IDependencyResolver.GetService implementation.

If GetService returns null , MVC will revert to the default implementation for the core infrastructure for the requested service. In most cases, there is no need to override these services in the Unity container, and even if another service is required, you can easily replace the default MVC implementation without adding it to the Unity configuration at all.

But since Unity is expected to throw this exception, why do these methods contain a catch clause. That way, the exception you are throwing falls into this method and null is returned.

Obviously, it’s very annoying that the debugger stops on these methods many times after starting the application, so the solution should flag these methods with the [DebuggerStepThrough] attribute to prevent the debugger in these methods.

+14
source share

All Articles