Unity Dependency Resolver不与Katana和WebAPI合作?

时间:2013-09-15 22:18:46

标签: c# asp.net-web-api katana

我尝试使用Unity 3来托管Katana中的Web API。

我得到一个异常,即system.web.http无法在Startup上加载。

有人做过这项工作吗?

HttpConfiguration apiConfig = new HttpConfiguration();

apiConfig.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
apiConfig.Formatters.Remove(apiConfig.Formatters.XmlFormatter);
apiConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
apiConfig.DependencyResolver = new UnityDependencyResolver(_container);

app.UseWebApi(apiConfig);

1 个答案:

答案 0 :(得分:4)

如果您使用的是Unity.WebAPI程序包,则它取决于System.Web.Http v4.0。要在Web API v2中使用它,您需要将程序集绑定重定向添加到web.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>