Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application crash while requesting the events from Microsoft outlook #3903

Closed
Sasikumar3595 opened this issue Jan 3, 2022 · 5 comments
Closed
Labels
area-infrastructure CI, Maestro / Coherency, upstream dependencies/versions platform/iOS 🍎 t/bug Something isn't working
Milestone

Comments

@Sasikumar3595
Copy link

Sasikumar3595 commented Jan 3, 2022

Description

We have created an application to access the outlook event and show it on list view. We are authenticating the account by using the client id and we faced the System.TypeLoadException but it is working fine in windows.

We configure the sample by the steps below:

  • Register the new project on https://portal.azure.com/
  • Enable calendar read write permission for the project
  • Create the IPublicClientApplication on App.cs by client id.
  • Configure the Android project and authenticate the user by AcquireTokenInteractive and GraphServiceClient

The exception was raised while get events after the authentication.

Client.Me.Events.Request().GetAsync();

Note: The issue did not raised on Windows

Steps to Reproduce

  1. Prepare the sample with below code
  2. Run the sample
  3. Exception raised on Client.Me.Events.Request().GetAsync();

In App.cs

public static IPublicClientApplication IdentityClientApp;
		//You need to replace your Application ID 
		public static string ClientID = "CliendID";
		public static string[] Scopes = { "User.Read", "Calendars.Read", "Calendars.ReadWrite" };

		public static object ParentWindow { get; set; }

		public App()
		{
			InitializeComponent();
			IdentityClientApp = PublicClientApplicationBuilder.Create(ClientID)
			.WithRedirectUri($"msal{ClientID}://auth")
			.Build();
			MainPage = new MainPage();
		}

In MainActivity

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            App.ParentWindow = this;
        }

In MainPage,

private Microsoft.Graph.GraphServiceClient Client = null;
		private AuthenticationResult tokenRequest;
		public MainPage()
		{
			InitializeComponent();
		}

		private void Authenticate()
		{
			Client = new Microsoft.Graph.GraphServiceClient("https://graph.microsoft.com/v1.0",
				new Microsoft.Graph.DelegateAuthenticationProvider(async (requestMessage) =>
				{
					List<IAccount> users = (List<IAccount>)await App.IdentityClientApp.GetAccountsAsync();

					if (users == null || users.Count == 0)
						tokenRequest = await App.IdentityClientApp.AcquireTokenInteractive(App.Scopes)
							.WithParentActivityOrWindow(App.ParentWindow)
							.WithUseEmbeddedWebView(true).ExecuteAsync();
					else
						tokenRequest = await App.IdentityClientApp.AcquireTokenSilent(App.Scopes, users.FirstOrDefault()).ExecuteAsync();

					requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", tokenRequest.AccessToken);
				}));
		}

		private async void OnClicked(object sender, EventArgs e)
		{
			this.Authenticate();
			var events = Client.Me.Events.Request().GetAsync();
			var list = events.ToList();
		}

Version with bug

Preview 10 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

Android API 31

Did you find any workaround?

No

Relevant log output

System.TypeLoadException
  Message=Could not resolve type with token 01000023 from typeref (expected class 'System.Threading.Tasks.ValueTask`1' in assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e')
@Sasikumar3595 Sasikumar3595 added the t/bug Something isn't working label Jan 3, 2022
@PureWeen PureWeen added the fatal label Jan 5, 2022
@Eilon
Copy link
Member

Eilon commented Feb 12, 2022

@Sasikumar3595 can you share more info about this method call that looks like it's async, but isn't awaited?

			var events = Client.Me.Events.Request().GetAsync();
			var list = events.ToList();

Is GetAsync() an async method? If so, it must be called using the await keyword.

@Sasikumar3595
Copy link
Author

@Eilon,

I have tried that code also.

The exception was raised when I comment the var list = events.ToList(); code and the exception raised from var events = Client.Me.Events.Request().GetAsync();

@SkyeHoefling
Copy link
Contributor

SkyeHoefling commented Mar 17, 2022

Hey friends! Jumping into this thread as I have the exact same problem and a workaround 🎉

I am using Microsoft.Graph v4.20.0 with an android application running .NET 6 via Uno Platform. I am wondering if this problem should be documented in the core xamarin-android repository instead of .NET MAUI. Since I am running into an identical issue on Uno Platform I am suspect that this also impacts native android running .NET 6.

Workaround

The Microsoft Graph API suggests using the API GetAsync() which handles the response object and returns a model that is easy to use in your downstream code. I was able to resolve this issue by using the API GetResponseAsync() which returns the response object that allows us to read the raw json data and then I was about to deserialize that into the Microsoft.Graph object. See my code snippet below

var response = await graphClient.Me
  .Request()
  .Select(user => new
  {
    Id = user.Id,
    DisplayName = user.DisplayName,
    UserPrincipalName = user.UserPrincipalName
  })
  .GetResponseAsync();

var data = await response.Content.ReadAsStringAsync();
var me = JsonConvert.DeserializeObject<User>(data);

Original Code and Error

Below is the original code that caused the same problem as reported in this thread.

var graphClient = new GraphServiceClient(httpClient);
graphClient.AuthenticationProvider = this;

var me = await graphClient.Me
  .Request()
  .Select(user => new
  {
    Id = user.Id,
    DisplayName = user.DisplayName,
    UserPrincipalName = user.UserPrincipalName
  })
  .GetAsync();

Error Message

System.TypeLoadException: Could not resolve type with token 01000023 from typeref (expected class 'System.Threading.Tasks.ValueTask`1' in assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e')
   at Microsoft.Graph.ResponseHandler.<HandleResponse>d__2`1[[Microsoft.Graph.DriveItem, Microsoft.Graph, Version=4.20.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].MoveNext()
   at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1[[Microsoft.Graph.DriveItem, Microsoft.Graph, Version=4.20.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].MoveNext()
   at Microsoft.Graph.DriveItemRequest.GetAsync(CancellationToken cancellationToken)
  at __my code__

@pekspro
Copy link

pekspro commented Mar 20, 2022

I also have this issue, running in preview 14.

I could confirm that the workaround @ahoefling has done is working.

@Redth
Copy link
Member

Redth commented Mar 22, 2022

This looks to use the https://www.nuget.org/packages/Microsoft.Graph.Core/ package which does not support net6.0-ios.

It will need to be updated to be built against and support net6.0-ios to resolve this.

@Redth Redth closed this as completed Mar 22, 2022
@Redth Redth added this to the 6.0.300-rc.1 milestone Mar 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 21, 2022
@Eilon Eilon added area-infrastructure CI, Maestro / Coherency, upstream dependencies/versions and removed area/upstream labels May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-infrastructure CI, Maestro / Coherency, upstream dependencies/versions platform/iOS 🍎 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants