Kralizek
Kralizek

Reputation: 2063

ASP.NET WebApi - HttpClient - Method not found

I'm trying to use Asp.NET WebAPI module but I get a weird error. When I try to run this simple program:

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();
    }
}

I have this error.

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()

The error occurs in Visual Studio and in LinqPad but it doesn't occur to my colleague.

I thought there could have been some kind of conflict with .NET 4.5 dev preview so I uninstalled it but without any benefit.

Thanks

Upvotes: 2

Views: 3972

Answers (2)

AndyPook
AndyPook

Reputation: 2889

It is possible to use pre-RTM WebAPI after you have installed VS2012. Add the following to your 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>

The problem is that the RTM version of System.Net.Http overrides the pre-RTM version because the newer version is in the GAC and the assy discovery prefers the newer version. Even if you explicity file reference the older version (grrr).

The NewtonSoft entry isn't strictly necessary...

Anyway this has worked for us.

Upvotes: 0

David Peden
David Peden

Reputation: 18414

The ASP.NET Web API Beta is explicitly not compatible with the .NET Framework 4.5 Developer Preview. See http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253802.

I would recommend uninstalling both and reinstalling the Web API after cleaning out both. I don't think uninstalling .NET 4.5 after installing Web API will do the trick.

Upvotes: 2

Related Questions