【问题标题】:ASP.NET WebApi - HttpClient - Method not foundASP.NET WebApi - HttpClient - 找不到方法
【发布时间】:2012-03-20 13:14:11
【问题描述】:

我正在尝试使用 Asp.NET WebAPI 模块,但出现了一个奇怪的错误。 当我尝试运行这个简单的程序时:

class Program
{
    static void Main(string[] args)
    {
        System.Net.Http.HttpClient client = new HttpClient();
        string data = client.GetStringAsync("http://www.kralizek.se/").Result;

        Console.WriteLine(data);

        Console.ReadLine();
    }
}

我有这个错误。

System.MissingMethodException was unhandled
  Message=Method not found: 'System.Threading.Tasks.Task`1<System.String> System.Net.Http.HttpClient.GetStringAsync(System.String)'.
  Source=Connector.App
  StackTrace:
       at ConnectorApp.Program.Main(String[] args)
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

错误发生在 Visual Studio 和 LinqPad 中,但我的同事没有发生。

我认为可能与 .NET 4.5 开发预览存在某种冲突,因此我将其卸载但没有任何好处。

谢谢

【问题讨论】:

  • 你引用的是 .net 4.5 吗?
  • 不,我不是。但同一个 bin 文件夹在我的同事工作站上“按原样”运行

标签: asp.net httpclient asp.net-web-api


【解决方案1】:

ASP.NET Web API Beta 明显不兼容 .NET Framework 4.5 开发者预览版。见http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253802

我建议卸载两者并在清除两者后重新安装 Web API。我不认为在安装 Web API 之后卸载 .NET 4.5 可以解决问题。

【讨论】:

  • 这部分正确:我卸载了 Visual Studio 11 DP 和 .NET 4.5。之后我修复了 VS2010,一切正常。谢谢
【解决方案2】:

安装 VS2012 后可以使用 pre-RTM WebAPI。将以下内容添加到您的 app/web.config

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0 - 2.0.0.0" newVersion="2.0.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

问题在于 System.Net.Http 的 RTM 版本会覆盖 RTM 之前的版本,因为较新的版本在 GAC 中,并且组件发现更喜欢较新的版本。即使您 explicity 文件引用了旧版本 (grrr)。

NewtonSoft 条目并非绝对必要...

无论如何,这对我们有用。

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多