dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 528 forks source link

MSAL for Xamarin.Android causes System.TypeInitializationException #3812

Closed bgavrilMS closed 5 years ago

bgavrilMS commented 5 years ago

Steps to Reproduce

  1. Clone MSAL (Microsoft.Identity.Client) https://github.com/AzureAD/microsoft-authentication-library-for-dotnet
  2. Build it (or at least build the Android target)

OR download version 4.5.0 from nuget

  1. Use a disassembler to look into the lib assembly and into the ref assembly

Expected Behavior

No Resource class in the lib assembly. Older versions of MSAL, such as 4.4.0, do not have this class.

Actual Behavior

A resource file is now present in the lib assembly, but not in the ref assembly.

image

This is causing type initalization exceptions for consumers of the library:

System.TypeInitializationException: The type initializer for 'Attribute' threw an exception. ---> System.TypeLoadException: Could not resolve type with token 01000022 from typeref (expected class 'Microsoft.Identity.Client.Ref.Resource' in assembly 'Microsoft.Identity.Client, Version=4.5.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae')

More details about the exception: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/1449

Version Information

Microsoft Visual Studio Enterprise 2019 Version 16.3.5 VisualStudio.16.Release/16.3.5+29411.108 Microsoft .NET Framework Version 4.8.03752

Installed Version: Enterprise

Visual C++ 2019 00435-60000-00000-AA277 Microsoft Visual C++ 2019

Application Insights Tools for Visual Studio Package 9.1.00913.1 Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2019 16.3.285.24939 ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 16.3.285.24939 For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.3.285.24939 Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.3.285.24939 Azure Functions and Web Jobs Tools

C# Tools 3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.0 (d16-2@8b56e20) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub.VisualStudio 2.10.7.8114 A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 0x10 - v2.9.20816.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.0.83+gbc8a4b23ec Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.3.7 (9d260c5) Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

Snapshot Debugging Extension 1.0 Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.61908.27190 Microsoft SQL Server Data Tools

TypeScript Tools 16.0.10821.2002 TypeScript Tools for Microsoft Visual Studio

Typewriter 1.22.0 Typewriter generates TypeScript files from c# code files using TypeScript Templates. This allows you to create fully typed TypeScript representations of server side API that automatically updates when you make changes to your c# code.

Visual Basic Tools 3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.4 for F# 4.6 16.3.0-beta.19455.1+0422ff293bb2cc722fe5021b85ef50378a9af823 Microsoft Visual F# Tools 10.4 for F# 4.6

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Xamarin 16.3.0.277 (d16-3@c0fcab7) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.3.0.246 (remotes/origin/d16-3@bd2f86892) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.3.565 (27e9746) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 10.0.3.0 (d16-3/4d45b41) Xamarin.Android Reference Assemblies and MSBuild support. Mono: mono/mono/2019-06@5608fe0abb3 Java.Interop: xamarin/java.interop/d16-3@5836f58 LibZipSharp: grendello/LibZipSharp/d16-3@71f4a94 LibZip: nih-at/libzip/rel-1-5-1@b95cf3fd ProGuard: xamarin/proguard/master@905836d SQLite: xamarin/sqlite/3.27.1@8212a2d Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-3@cb41333

Xamarin.iOS and Xamarin.Mac SDK 13.4.0.2 (e37549b) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Log File

-- this is too long --

jonathanpeppers commented 5 years ago

So this csproj is using MSBuild.Sdk.Extras, a community-led project.

I had to make this change before it would build for me:

diff --git a/src/client/Microsoft.Identity.Client.Ref/Microsoft.Identity.Client.Ref.csproj b/src/client/Microsoft.Identity.Client.Ref/Microsoft.Identity.Client.Ref.csproj
index bdb49af9..126483bf 100644
--- a/src/client/Microsoft.Identity.Client.Ref/Microsoft.Identity.Client.Ref.csproj
+++ b/src/client/Microsoft.Identity.Client.Ref/Microsoft.Identity.Client.Ref.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="MSBuild.Sdk.Extras">
+<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
diff --git a/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj b/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj
index b49e6370..7bdbeeee 100644
--- a/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj
+++ b/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="MSBuild.Sdk.Extras">
+<Project Sdk="MSBuild.Sdk.Extras/2.0.54">

I'm not sure what version it gets if you don't specify it?

But then I could solve the problem by adding /p:AndroidUseIntermediateDesignerFile=False at the command-line. I wasn't able to put it in the project, as MSBuild.Sdk.Extras was overwriting it.

I will look into this, as I'm not really sure what changed.

bgavrilMS commented 5 years ago

@jonathanpeppers - I just tried this and it seems to work. It looks like the sdk.extras set this flag to true if it is not set: https://github.com/onovotny/MSBuildSdkExtras/blob/master/Source/MSBuild.Sdk.Extras/Build/Platforms/Xamarin/Xamarin.Android.targets#L14

This is great, we will most likely produce a hot fix for the library, as it is blocking quite a few customers. Thanks for the quick reply!