相关疑难解决方法(0)

在Web API 2和.NET 4.5.1迁移之后,GlobalConfiguration.Configure()不存在

我最近开始按照本指南将我的项目迁移到.NET 4.5.1和Web Api 2.

MS开发人员Rick Anderson要求你做的第一件事就是改变:

WebApiConfig.Register(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)

GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)

在global.asax文件中.然而,当我尝试构建时,这给了我一个错误:

错误1'System.Web.Http.GlobalConfiguration'不包含'Configure'的定义

我的项目目前是MVC 5和Web Api 2和.NET 4.5.1,但我认为System.Web.Http仍然认为它是.NEt 4.0版本.

我该怎么办呢?

谢谢.

编辑:

这是我的程序集绑定:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <!--
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly> -->
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

顶部评论后评论的所有内容都因为我收到了错误:

警告2发现无法解析的同一依赖程序集的不同版本之间发生冲突.当日志详细程度设置为详细时,这些引用冲突将在构建日志中列出.

并且摆脱硬绑定正在解决这个问题.

c# asp.net-4.5 asp.net-web-api asp.net-mvc-5

143
推荐指数
6
解决办法
15万
查看次数

依赖注入无法与Owin自托管Web Api 2和Autofac一起使用

我正在寻找Web Api 2,Owin和Autofac,并需要一些指导.

概述
我有一个Owin自托管的Web Api,它使用Autofac进行IoC和依赖注入.该项目是一个控制台应用程序,就像一个服务,这意味着它可以停止和启动.我有一个带有两个构造函数的身份验证控制器:一个参数少,另一个注入存储库.

问题
当我运行服务并调用api时,我的无参数构造函数被调用,我的存储库永远不会被注入(_repository = null).

研究
我做了一些研究,并在Github上发现了一些有用的项目,我将其复制到了发球台,但我错过了很大一部分难题.很有帮助,但没有解决我的问题.我在Stack Overflow上阅读了这个问题,Dane Sparza有一个很好的演示项目,但我找不到一个明确的解决方案.问题不是自托管而是依赖注入.

我的代码(细化了解释)

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        var connectioninfo = ConnectionInfo.FromAppConfig("mongodb");

        var builder = new ContainerBuilder();                                    // Create the container builder.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());         // Register the Web API controllers.
        builder.Register(c => …
Run Code Online (Sandbox Code Playgroud)

c# autofac owin asp.net-web-api2

25
推荐指数
1
解决办法
2万
查看次数