Wcf: UWP Xamarin.Forms cannot use System.ServiceModel.Http NuGet Package

Created on 4 Aug 2018  ·  11Comments  ·  Source: dotnet/wcf

Description

When UWP app project refers System.ServiceModel.Http NuGet package, it always crashes with following exception message and callstack.

((System.IO.FileLoadException)e.Exception).Message
~~~~~~~~
Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
~
~~~~~~~

e.Exception.RemoteStackTrace
~~~~~~~~
at App1.MainPage..ctor()
at App1.App..ctor()
at App1.UWP.MainPage..ctor()
at App1.UWP.App1_UWP_XamlTypeInfo.XamlTypeInfoProvider.Activate_4_MainPage()
at App1.UWP.App1_UWP_XamlTypeInfo.XamlUserType.ActivateInstance()
~
~~~~~~~

Steps to Reproduce

(Attached App1.zip sample project to reproduce the problem)

  1. On VS2017, create new Xamarin.Forms solution as following option.
    Platform: Windows (UWP)
    Code Sharing Strategy: .NET Standard
  2. When you done creating a solution, you can find two projects in Solution Explorer.
  3. Right click on code sharing project, you can click [Manage NuGet Packages...].
  4. On NuGet Package Manager, search "System.ServiceModel.Http" and install the latest version one (v4.5.3).
  5. Next, please replace with the code below for MainPage.xaml.cs for code sharing project.
    ~~~~~
    using System.ServiceModel;
    using Xamarin.Forms;

namespace App1{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var channel = new ChannelFactory(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/Example"));
}
}

[ServiceContract()]
public interface ITest
{
}

}
~~~~~

  1. Launch and execute app with Debug x86 or x64 build.

Expected Behavior

Launch application without any problem.

Actual Behavior

Crash with 'System.IO.FileLoadException' at LoadApplication().
~~~~~
namespace App1.UWP
{
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();

        LoadApplication(new App1.App());  << Exception 'System.IO.FileLoadException' occurs at here
    }
}

}
~~~~~

Basic Information

  • Version with issue: Xamarin.Forms v3.1.0.697729
  • Last known good version: System.ServiceModel.Http v4.4.4
  • IDE: Any editions of Visual Studio 2017 (Version 15.7.5)
  • Platform Target Frameworks:

    • iOS: Not confirmed.

    • Android: This problem does not occured on Android platform.

    • UWP: UAP,Version=10.0.16299.0

  • Android Support Library Version:
  • Nuget Packages:
    Microsoft.NETCore.UniversalWindowsPlatform 3.1.7
    Xamarin.Forms 3.1.0.697729
    NETStandard.Library 2.0.3
    System.ServiceModel.Http 4.5.3
  • Affected Devices:
    Windows Devices

  • Other version info
    Xamarin Designer 4.12.1 (f3257e429)
    Visual Studio extension to enable Xamarin Designer tools in Visual Studio.
    Xamarin.Android SDK 8.3.3.2 (HEAD/dffc59120)
    Xamarin.Android Reference Assemblies and MSBuild support.
    Xamarin.iOS and Xamarin.Mac SDK 11.12.0.4 (64fece5)
    Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Screenshots

Reproduction Link

bug

All 11 comments

Same here

Workaround: You can avoid the problem by using version 4.4.4 package.

Looks like this is the same problem as #3031. We are investigating.

In .NET Core 2.1 release (released in this year), WCF Core assemblies have updated version numbers to 4.5... (minor version number changed). However Xamarin WCF facade, like System.ServiceModel.Primitives inbox version is Version=4.2.1.0. When Xamarin app reference to a .Netstandard library, which referece to 4.5.* WCF Core Nuget package, it fails to load WCF Core assembly, like System.ServiceModel.Primitives: Version=4.5.0.3. This is due to Xamarin façade System.ServiceModel.Primitives assembly version (4.2.1.0) is lower than the .NET standard library expected version (4.5.0.3). Xamarin WCF Core facades need to be updated to 4.5.. from below current versions released with VS.

            System.ServiceModel.Primitives: Version=4.2.1.0
            System.ServiceModel.Http: Version=4.1.3.0
            System.ServiceModel.Duplex: Version=4.1.1.0
            System.ServiceModel.NetTcp: Version=4.1.3.0
            System.ServiceModel.Security: Version=4.0.4.0
            System.ServiceModel.Web: Version=4.1.3.0

this looks like a potential duplicate of https://github.com/dotnet/wcf/issues/2438 too

We had further look at this issue. It doesn’t repro for Xamarin iOS app, but repro with Xamarin UWP app with Code sharing strategy of “.NET Standard”. Even after specifically adding reference to the same 4.5.3 WCF package in both UWP and Xamarin projects in the solution, the UWP process still loads the WCF assembly in the original UWP meta package (System.ServiceModel.Primitives: Version=4.2.1.0) first, which causes fileloadexception later when process tries to load newer version of System.ServiceModel.Primitives.dll. Looks like it is UWP project issue instead of Xamarin façade issue. per @marek-safar, mono whitelist these assemblies and they should work with any assembly version numbers with Xamarin products. UWP team is investigating the issue

The issue has been fixed in UWP version 6.2.3.

The problem is still (or again) here with latest packages and with .net standard 2.0 as shared library

Same issue with UWP version 6.2.10
.net Standard 2.0 PCL.

System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

Me too, same as pavloss.
What should I do, use 6.2.9, or 8, or ...?

I can try some things, but I am very weary of stumbling into one weird problem after the other, while trying to get an UWP variant working.
So far it has never succeeded. The Android one did work.

@a-einstein bit of a shot in the dark but this might be useful to you considering the error code and the fact that this is uwp:
https://devblogs.microsoft.com/oldnewthing/20200615-00/?p=103868

https://github.com/asklar/WinRTComponent

Was this page helpful?
0 / 5 - 0 ratings