asp.net-web-api – Web Api使用IDependencyResolver补充启动异常

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – Web Api使用IDependencyResolver补充启动异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个Web Api,我决定使用自定义DependencyResolver.我参考 this [Dependency Injection for Web API Controllers]文章.到目前为止,一切都在依赖注入控制器方面运行良好.我的Owin启动类的配置代码片段
private void RegisterIoC(HttpConfiguration config)
{
    _unityContainer = new UnityContainer();
    _unityContainer.RegisterType<IAccountService,AccountService>();
    .........
    .........
    config.DependencyResolver = new UnityResolver(_unityContainer);
}

但是当Api首次在UnityResolver的GetService方法中抛出一些ResolutionFailedException(但捕获到)时.这是异常消息

"Exception occurred while: while resolving. 
Exception is: InvalidOperationException - 
The current type,System.Web.Http.Hosting.IHostBufferPolicySelector,**is an interface and cannot be constructed. Are you missing a type mapping?**"

上述同样的异常抛出以下类型

System.Web.Http.Hosting.IHostBufferPolicySelector
System.Web.Http.Tracing.ITraceWriter
System.Web.Http.Metadata.ModelMetadataProvider
System.Web.Http.Tracing.ITraceManager
System.Web.Http.Dispatcher.IHttpControllerSelector
System.Web.Http.Dispatcher.IAssembliesResolver
System.Web.Http.Dispatcher.IHttpControllerTypeResolver
System.Web.Http.Controllers.IHttpActionSelector
System.Web.Http.Controllers.IActionValueBinder
System.Web.Http.Validation.IBodyModelValidator
System.Net.Http.Formatting.IContentNegotiator

我知道这些ResolutionFailedException是因为上述类型的unity配置没有提供映射.

现在这里是我的问题: – 如果我实现定制单位DependencyResolver,我需要定义上述类型的映射,如果需要定义什么将是他们相应的默认实现类型或有一些替代方法来实现DependencyResolver.我真的很关心即使应用程序运行正常,无法解决上述类型可能会导致严重的问题.请帮忙.

最后一个加法:
对于以下类型,当我向我的web api请求任何动作时抛出同样的ResolutionFailedException异常

System.Web.Http.Dispatcher.IHttpControllerActivator
System.Web.Http.Validation.IModelValidatorCache
System.Web.Http.Controllers.IHttpActionInvoker

解决方法

我正在使用Unity与WebApi和OWIN / K@R_404_284@a同样的问题.

我的解决方案是使用Unity.WebApi Nuget包中定义的UnityDependencyResolver,而不是我自己的自定义实现(像上面的@Omar Alani)

Install-Package Unity.WebAPI

请注意,该软件包将尝试在App_Start中添加名为UnityConfig.cs的文件(我自己使用的文件名).

在UnityConfig.cs文件中,该软件包将添加代码,以便根据GlobalConfiguration.Configuration.DependencyResolver注册该容器,这不是我们想要的OWIN.

所以代替使用:

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

更改使用:

config.DependencyResolver = new UnityDependencyResolver(container);

为了完整性:

我的UnityConfig.cs

public static class UnityConfig
{
    public static void Register(HttpConfiguration config)
    {
        var container = new UnityContainer();

        // Your mappings here

        config.DependencyResolver = new UnityDependencyResolver(container);
    }
}

我的Startup.cs

[assembly: OwinStartup(typeof(UnityTest.BusinessLayer.Api.ApiStartup))]
namespace UnityTest.BusinessLayer.Api
{
    public partial class ApiStartup
    {
        public void Configuration(IAppBuilder app)
        {

            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

            HttpConfiguration httpConfig = new HttpConfiguration();

            UnityConfig.Register(httpConfig);

            ConfigureAuth(app); //In App_Start ->Startup.Auth

            WebApiConfig.Register(httpConfig);

            app.UseWebApi(httpConfig);
    }
  }
}

猜你在找的asp.Net相关文章