Thibault D.
Thibault D.

Reputation: 10005

Dependency loading error when using a .Net Standard Xamarin.Forms project in an iOS app

This question is about the use of .Net Standard in a Xamarin.Forms project targeting iOS (but also Android).

I've read through
https://forums.xamarin.com/discussion/86139/targeting-net-standard/p1
https://blog.xamarin.com/building-xamarin-forms-apps-net-standard/
https://channel9.msdn.com/Shows/XamarinShow/Snack-Pack-15-Upgrading-to-XamarinForms-to-NET-Standard
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/net-standard/
https://stackoverflow.com/questions/tagged/.net-standard+xamarin.forms
http://lastexitcode.com/blog/2017/06/04/NuGetSupportInVisualStudioMac7-0/

What I've done

  1. Created a new blank Xamarin.Forms app, targeting iOS and Android and using PCL, using Visual Studio for Mac
  2. Following the steps of @JamesMontemagno, created a new .Net Standard library project and added Xamarin.Forms 2.4.0 via Nuget

If I understand well, at this point, my new core application project is a .Net Core project.

  1. Added NuGet dependency toward Microsoft.Rest.ClientRuntime because my application uses an autogenerated REST Api definition created using autorest
  2. Added my core application project as a reference to my iOS project. Question here: What type or project is the iOS project here? .Net Framework project? Or something else? How do/can I create a .Net Core/Standard iOS project?

Then I build and run my application. When I hit a button that trigger the use of Microsoft.Rest.ClientRuntime, I get the following error:

Could not load type of field 'MyApp.MyPage+d__3:5__2' (4) due to: Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:<none>

If I add Microsoft.Rest.ClientRuntime NuGet package as a dependency to the iOS project then it does work but:

  1. It's pulling a hell lot of NuGet dependencies
  2. That's not how it used to work with PCL

So I wondered if there's anything documented about referencing .Net Standard from old-not-so-standard projects and found this:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx

But it does not seem to work, at least not for my iOS project using Visual Studio for Mac.

Any suggestion on how to solve this? Or how am I supposed to set up my iOS app project to use the .Net Standard project?

This is how the .Net Standard project is referenced in the iOS application project:

  <ItemGroup>
    <ProjectReference Include="..\MyApp\MyApp.csproj">
      <Project>{2AA92B90-07DC-420B-8A5B-C84F2C437FF2}</Project>
      <Name>MyApp</Name>
    </ProjectReference>
  </ItemGroup>

And this is how MyApp.csproj looks like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="MyBackend\" />
  </ItemGroup>
  <ItemGroup>
    <None Remove="LiuStackLayout.xaml" />
    <None Remove="MyPage.xaml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Update="MyPage.xaml.cs">
      <DependentUpon>MyPage.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="2.4.0.91020" />
    <PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.10" />
  </ItemGroup>
</Project>

Upvotes: 0

Views: 1644

Answers (2)

Thibault D.
Thibault D.

Reputation: 10005

Solution brought by Matt Ward:

If you want to avoid a lot of NuGet dependencies you could switch to using PackageReferences in the iOS project instead of using a packages.config file. If you are using VS for Mac then currently you will need to add one PackageReference into the .csproj manually by using a text editor. Once the project has a single PackageReference then VS Mac will use PackageReferences in the project file from then on.

<ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="2.4.0.280" />
</ItemGroup>

Then perform a fair amount of NuGet restore, clean and rebuild.

Also make sure to use a rather new Mono version (Mono 5.4.1.6 works, Mono 4.8.0 does not). The Matt's solution was not working for me because I was using an old Mono build (4.8.0)

Source: forums.xamarin.com/discussion/...

Upvotes: 0

Zithro
Zithro

Reputation: 31

You could either add the NuGet package to your iOS project, or add the package as a solution-level package. (Right click on the solution in VS and manage NuGet packages for solution)

A solution-level package (NuGet 3.x and later) is installed only once in a solution and is then available for all projects in the solution. A project-level package is installed in each project that uses it. A solution-level package might also install new commands that can be called from within the Package Manager Console.

Via https://learn.microsoft.com/en-us/nuget/policies/nuget-faq#nuget-in-visual-studio

Right now, your package is installed as a project-level package, which means only your Standard library receives access to it (thus your error in iOS)

Upvotes: 0

Related Questions