当我调用 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 是参考:
完整响应错误:
{
"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
没有工作?