BreezeControllerAttribute异常:无法加载文件或程序集'System.Net.Http

时间:2013-07-02 12:59:01

标签: asp.net-web-api odata breeze

当我调用API时,会显示错误:

  

“无法加载文件或程序集'System.Net.Http,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其中一个依赖项。系统找不到指定的文件。”,

代码

我的API有一个基本控制器

[BreezeNHController]
public class baseApiController<T> : ApiController
    where T : class, IEntity
{
    public IRepository<T> Repositorio { get; private set; }

    [HttpGet, BreezeNHQueryable(AllowedQueryOptions = AllowedQueryOptions.All, PageSize = 20)]
    public IQueryable<T> Get()
    {
        return Repositorio.All();
    }

    // ..
}

并实施:

[BreezeController, Authorize]
public class usuariosController : baseApiController<User>
{
    public usuariosController(IUserRepository repositorio)
        : base(repositorio)
    { }
}

网址:GET /api/usuarios抛出一个例外!

但我的DDL参考:

enter image description here

完全响应错误:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The type initializer for 'Breeze.WebApi.BreezeControllerAttribute' threw an exception.",
    "ExceptionType": "System.TypeInitializationException",
    "StackTrace": "   at Breeze.WebApi.BreezeControllerAttribute.Initialize(HttpControllerSettings settings, HttpControllerDescriptor descriptor)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()\r\n   at System.Lazy`1.CreateValue()\r\n   at System.Lazy`1.LazyInitValue()\r\n   at System.Lazy`1.get_Value()\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Could not load file or assembly 'System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.",
        "ExceptionType": "System.IO.FileNotFoundException",
        "StackTrace": "   at Breeze.WebApi.JsonFormatter.Create()\r\n   at Breeze.WebApi.BreezeControllerAttribute..cctor()"
    }
}

尝试

在包管理器控制台中,运行命令Add-BindingRedirect

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
发现奇怪的是没有指向4.0。这可能是问题吗?如果是,为什么Add-BindingRedirect不起作用?

1 个答案:

答案 0 :(得分:0)

这绝对是一个绑定重定向问题。我建议您将整个<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">内容从一个全新的ASP.NET MVC 4项目复制到您的web.config文件中。这种情况偶尔发生在我身上,刷新搞乱的绑定可以解决这个问题。