【问题标题】:Azure.Identity Throws Not implemented Exception on Platform Xamarin.iOSAzure.Identity 在平台 Xamarin.iOS 上引发未实现的异常
【发布时间】:2021-11-23 21:31:33
【问题描述】:

我正在尝试使用 Azure.Identity UsernamePasswordCredential 通过图形 api 向 AD 进行身份验证,但每当我初始化凭据 ctor 时,它都会引发以下异常:

The method or operation is not implemented. at System.Diagnostics.Tracing.EventSource.GetSources ()

堆栈跟踪:

System.TypeInitializationException: The type initializer for 'Azure.Core.Pipeline.LoggingPolicy' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Azure.Core.Diagnostics.AzureCoreEventSource' threw an exception. ---> System.NotImplementedException: The method or operation is not implemented.
  at System.Diagnostics.Tracing.EventSource.GetSources () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Diagnostics.Tracing/EventSource.cs:318 
  at Azure.Core.Diagnostics.AzureEventSource.DeduplicateName (System.String eventSourceName) [0x00010] in <0aa90fe0646d47bc95bff520717dbf16>:0 
  at Azure.Core.Diagnostics.AzureEventSource..ctor (System.String eventSourceName) [0x00000] in <0aa90fe0646d47bc95bff520717dbf16>:0 
  at Azure.Core.Diagnostics.AzureCoreEventSource..ctor () [0x00000] in <0aa90fe0646d47bc95bff520717dbf16>:0 
  at Azure.Core.Diagnostics.AzureCoreEventSource..cctor () [0x00000] in <0aa90fe0646d47bc95bff520717dbf16>:0 
   --- End of inner exception stack trace ---
  at Azure.Core.Pipeline.LoggingPolicy..cctor () [0x00000] in <0aa90fe0646d47bc95bff520717dbf16>:0 
   --- End of inner exception stack trace ---
  at Azure.Core.Pipeline.HttpPipelineBuilder.Build (Azure.Core.ClientOptions options, Azure.Core.Pipeline.HttpPipelinePolicy[] perCallPolicies, Azure.Core.Pipeline.HttpPipelinePolicy[] perRetryPolicies, Azure.Core.ResponseClassifier responseClassifier) [0x00164] in <0aa90fe0646d47bc95bff520717dbf16>:0 
  at Azure.Identity.CredentialPipeline..ctor (Azure.Identity.TokenCredentialOptions options) [0x00023] in <47110d8b120248e4b903ba2cc44741ea>:0 
  at Azure.Identity.CredentialPipeline.GetInstance (Azure.Identity.TokenCredentialOptions options) [0x00003] in <47110d8b120248e4b903ba2cc44741ea>:0 
  at Azure.Identity.UsernamePasswordCredential..ctor (System.String username, System.String password, System.String tenantId, System.String clientId, Azure.Identity.TokenCredentialOptions options, Azure.Identity.CredentialPipeline pipeline, Azure.Identity.MsalPublicClient client) [0x00069] in <47110d8b120248e4b903ba2cc44741ea>:0 
  at Azure.Identity.UsernamePasswordCredential..ctor (System.String username, System.String password, System.String tenantId, System.String clientId, Azure.Identity.TokenCredentialOptions options) [0x00000] in <47110d8b120248e4b903ba2cc44741ea>:0 
  at MyProject.GraphService.Init (System.String username, System.String password, System.String tenantId, System.String clientId, System.String eventKeyword) [0x00014] in .../MyProject/GraphService.cs:31 
  at MyProject.Features.Auth.AuthViewModel.InnerGetUser () [0x00077] in .../MyProject/Features/Auth/AuthViewModel.cs:57 

我的代码是 graphService,它是一个标准库 v2.1,由 Xamarin.forms 项目中的 AuthViewModel 使用。

    public class AuthViewModel: BaseViewModel
    {
    ...
        private async Task InnerGetUser()
        {
            try
            {
                this.IsBusy = true;
                if (string.IsNullOrEmpty(this.Email) ||
                    string.IsNullOrEmpty(this.Password))
                    throw new ValidationException("Email e password devono essere compilati");
                
                this._graphService.Init(this.Email, this.Password,
                    this._settingsService.GetStringSetting(Services.Settings.TenantId),
                    this._settingsService.GetStringSetting(Services.Settings.ClientId),
                    this._settingsService.GetStringSetting(Services.Settings.EventKeyword));

                var user = await this._graphService.GetUser();
                this._userService.CurrentUser = new UserDto()
                {
                    Name = user.DisplayName
                };

                this._userService.InvokeOnLoginDone();
            }
            catch (Exception e)
            {
                await base.ShowError(e);
            }
            finally
            {
                this.IsBusy = false;
            }
        }
     ...
}

public class GraphService: IGraphService
{
     ...
        public void Init(string username, string password, string tenantId, string clientId, string eventKeyword)
        {
            this._eventKeyword = eventKeyword;
            Console.WriteLine("Creo credentials provider");

            var userNamePasswordCredential = new UsernamePasswordCredential(
                username, password, tenantId, clientId, new TokenCredentialOptions
                {
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
                });

            Console.WriteLine("Creo graph service client");

            this._graphClient = new GraphServiceClient(userNamePasswordCredential);
        }
     ...
}

奇怪的是,它在 net5 中的控制台应用程序上运行良好。

掘金:

  • Xamarin.forms 5.0.0.2125,
  • Azure.Identity 1.4.1,
  • Microsoft.Graph 4.6.0

环境:

  • mac.os 11.6
  • visual studio for mac 8.10.10(内部版本 8)

SDK:

  • .NET 5.0.401,
  • 单声道 6.12.0.140,
  • Xamarin.iOS 15.0.0.6。

有什么想法吗?

谢谢。

【问题讨论】:

  • 你的.../MyProject/Features/Auth/AuthViewModel.cs:57 代码是什么?
  • 它调用了我上面粘贴的那段代码,女巫尝试实例化 UsernamePasswordCredential 并失败。在 Xamarin.forms 项目中,调用者是 authviewmodel。
  • 我已经更新了示例代码
  • 从问题 24055 中可以看出,这似乎是 Azure.Core 的一个错误。新版本将于 10 月发布。

标签: xamarin xamarin.forms xamarin.ios microsoft-graph-api azure-sdk-.net


【解决方案1】:

Azure.Storage.Blobs 遇到了类似的问题

System.TypeInitializationException: The type initializer for 'Azure.Core.Pipeline.LoggingPolicy' threw an exception. ---&gt; System.TypeInitializationException: The type initializer for 'Azure.Core.Diagnostics.AzureCoreEventSource' threw an exception. ---&gt; System.NotImplementedException: The method or operation is not implemented.

我通过安装Azure.Core 解决了这个问题。希望对你有用。

【讨论】:

  • 嗨 Johan,添加 Azure.Core 实际上会阻止 UsernamePasswordCredential 抛出,但现在下一步(通过调用 var user = await this._graphClient.Me.Request() .GetAsync();)抛出另一个组件上的相同异常:“Azure.Identity.Azure Identity EventSource”的类型初始化程序引发了异常。 ---> System.NotImplementedException:方法或操作未实现。在 System.Diagnostics.Tracing.EventSource.GetSources ()
  • @Gaburiere 您是否在所有平台上都安装了这两个软件包?共享、iOS 和 Android?
  • 是的,我试过但没有结果。只有将 Microsoft.Graph 更新到 4.7.0 版和将 Azure.Identity 更新到 1.5.0 版才能成功!现在,通过将自定义权利添加到包含对钥匙串本身的访问权限的权利,钥匙串访问和本机链接存在问题,但我认为这是另一回事:)。它适用于安卓。
【解决方案2】:

我将其发布为不是答案,因为我遇到了同样的问题,我尝试了这个但没有成功。 我尝试将所有诊断选项设置为 false。它确实有效果,因为它在我创建 BlobServiceClient 时停止抛出异常,但现在它在我迭代列出 blob 的结果时抛出 System.NotImplementedException。

ClientSecretCredentialOptions options = new ClientSecretCredentialOptions();

            options.Diagnostics.IsDistributedTracingEnabled = false;
            options.Diagnostics.IsLoggingContentEnabled = false;
            options.Diagnostics.IsLoggingEnabled = false;
            options.Diagnostics.IsTelemetryEnabled = false;

【讨论】:

【解决方案3】:

将 Microsoft.Graph 更新到 4.7.0 版和将 Azure.Identity 更新到 1.5.0 版已解决了该问题。

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 2017-12-06
    • 2019-04-01
    • 2011-11-05
    • 2020-09-24
    • 2012-10-10
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多