Skip to content

Commit

Permalink
Updated the PCL project to target WP8 and dealt with the fallout
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbatum committed Mar 22, 2014
1 parent 1cdce19 commit edb96ee
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,38 @@ Now the project can be added to the solution without errors. Next I want to do a

![Add Windows Phone 8 project](/images/add-wp8-proj.png)

The next step is to update the project to use our shared code. Now to be able to reference my shared project, I'll need to update its targets to include WP8. So lets do that now and build:

![Windows Phone 8 http errors](/images/wp8-http-errors.png)

Uhoh! Windows Phone 8 doesn't have HttpClient, and so our PCL is not valid. We can fix this by using the [HttpClient NuGet package](http://www.nuget.org/packages/Microsoft.Net.Http/) which the Windows Phone 8 project is already using. I right click on the solution, select Manage NuGet Packages and make sure that for HttpClient, my shared project is ticked:

![Add http client](/images/add-http-client.png)

This fixes my compilation errors, but there is a catch. The NuGet for HttpClient uses Bcl.Build for some of its magic, but this magic doesn't work quite right with Xamarin yet. To see this in action, run the iOS project on a real device and you'll get the error "This header must be modified with the appropiate property":

![assembly binding redirect hell](/images/assembly-binding-redirect-hell.png)

The BCL team at Microsoft is aware of this issue but at the current time of writing this issue hasn't been fixed, so here's the workaround: in your iOS and Android projects, replace the generated app.config with the following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

This forces the Mono version of HttpClient into use. Be aware that as you work with your projects, you may find that certain actions cause your app.config to be regenerated and it may become invalid again. They key to remember is that for Http related assemblies, your iOS and Android projects need the binding above.

You might also see warnings in Visual Studio about incorrect assembly bindings. My advice is to ignore any of such warnings that appear for your iOS and Android projects.





### Contact
Expand Down
Binary file added images/add-http-client.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/assembly-binding-redirect-hell.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/wp8-http-errors.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions xamarinpcl/xamarinpcl-android/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
1 change: 1 addition & 0 deletions xamarinpcl/xamarinpcl-android/xamarinpcl-android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="ToDoItemWrapper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<None Include="Properties\AndroidManifest.xml" />
Expand Down
11 changes: 11 additions & 0 deletions xamarinpcl/xamarinpcl-ios/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
1 change: 1 addition & 0 deletions xamarinpcl/xamarinpcl-ios/xamarinpcl-ios.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Info.plist" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions xamarinpcl/xamarinpcl-shared/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl" version="1.1.6" targetFramework="portable-net45+wp80+win+MonoAndroid10+MonoTouch10" />
<package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="portable-net45+wp80+win+MonoAndroid10+MonoTouch10" />
<package id="Microsoft.Net.Http" version="2.2.18" targetFramework="portable-net45+wp80+win+MonoAndroid10+MonoTouch10" />
</packages>
19 changes: 18 additions & 1 deletion xamarinpcl/xamarinpcl-shared/xamarinpcl-shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<OutputType>Library</OutputType>
<RootNamespace>xamarinpclshared</RootNamespace>
<AssemblyName>xamarinpcl-shared</AssemblyName>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<TargetFrameworkProfile>Profile78</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down Expand Up @@ -42,6 +42,23 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\Components\azure-mobile-services-1.1.5\lib\mobile\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\portable-net40+sl4+win8+wp71\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Primitives">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Primitives.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
</Project>

0 comments on commit edb96ee

Please sign in to comment.