Fixing HttpClient and Portable Class Library compile errors
I had a simple Portable Class Library project which used Microsoft HTTP Client Libraries (System.Net.Http.HttpClient). The problem: The Visual Studio wouldn’t compile it:
The type or namespace name ''Http'' does not exist in the namespace ''System.Net'' (are you missing an assembly reference?)
The PCL project targeted the following frameworks:
- Windows Phone 8
- Silverlight 5
- .NET 4.5
- Windows Store apps
I was using the stable 2.1.10 release of the HttpClient library. Resharper didn’t find anything wrong with the code but still it wouldn’t compile. So I created an another PCL project, added the same HttpClient NuGet package and the same code. And it compiled without a problem.
So I had two identical projects with identical references and with identical code. But only one of them compiled.
I decided to diff the project files and this helped me to notice that the non-compiling project had an app.config –file:
And the configuration contained bindingRedirects:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
When I created the non-compiling PCL project, I set some of the target frameworks wrong. Because of this, it seems, the HttpClient Nuget-package added the configuration. And this broke the compilation after I had fixed the target frameworks.
The solution was simple: I just had to remove the runtime-element from the configuration.