简体   繁体   中英

Deadlocking on Async even with ConfigurationAwait(false) when resolving object with Autofac

I'm getting a deadlock on some C# code even though I'm using ConfigureAwait(false). Unfortunately I'm not able to use async all-the-way up so I'm relying on ConfigureAwait.

The situation is that I need to make an HTTP request to retrieve a database access token from Azure AD during the startup of both a web application and a console application. In both cases the program is deadlocking when Autofac attempts to resolve the database token which triggers the web request.The odd thing is that it works twice but fails the third time.

Autofac Module:

 public class AzureActiveDirectoryModule : BaseModule
 {
    protected override bool SupportsMultipleModuleRegistrations => true;

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<AzureActiveDirectoryService>().As<IAzureActiveDirectoryService>();

        builder.Register(c => c.Resolve<IAzureActiveDirectoryService>().GetAuthenticationContext())
               .As<AuthenticationContext>()
               .SingleInstance();

        builder.Register(c => c.Resolve<IAzureActiveDirectoryService>().GetCertificateCredential())
               .As<ClientAssertionCertificate>()
               .SingleInstance();

        builder.Register(c =>
                         c.Resolve<IAzureActiveDirectoryService>()
                          .GetDatabaseAccessToken(c.Resolve<AuthenticationContext>(), c.Resolve<ClientAssertionCertificate>()))
               .Named<string>(PersistenceModule.DatabaseAccessToken);
    }
}

Service that makes the HTTP Request (I've replaced the http request with a call to google):

public class AzureActiveDirectoryService : IAzureActiveDirectoryService
{

    public string GetDatabaseAccessToken(AuthenticationContext authContext, ClientAssertionCertificate certCred)
    {
        try
        {
            return AcquireDatabaseToken(certCred, authContext).Result;
        }
        catch (Exception ex)
        {
            Log.Error(ex, "Exception occurred while attempting to retrieve database access token.");
        }

        return string.Empty;
    }

    private async Task<string> AcquireDatabaseToken(ClientAssertionCertificate certCred, AuthenticationContext authContext)
    {
            try
            {
                await DoHttpRequestAsync().ConfigureAwait(false);
                return string.Empty;
            }
            catch (Exception ex)
            {
                Log.Error(ex,
                          "An error occurred",
                          ex.ToString());
            }
    }

    private async Task<string> DoHttpRequestAsync()
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            HttpRequestMessage requestMessage = new HttpRequestMessage();
            requestMessage.RequestUri = new Uri("http://www.google.com");
            requestMessage.Headers.Accept.Clear();

            client.Timeout = TimeSpan.FromMilliseconds(30000);

            try
            {
                await client.SendAsync(requestMessage).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                var a = ex.Message;
            }

        }

        return string.Empty;
    }
}

Here's the deadlocked thread's (edited) stacktrace (in the console app)-

mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout, bool exitContext)   Unknown
mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout) Unknown
mscorlib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)  Unknown
mscorlib.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)    Unknown
mscorlib.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)    Unknown
mscorlib.dll!System.Threading.Tasks.Task<string>.GetResultCore(bool waitCompletionNotification) Unknown
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.Result.get()   Unknown Contoso.Infrastructure.dll!Infrastructure.Azure.AzureActiveDirectoryService.GetDatabaseAccessToken(Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext, Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate certCred) Line 101 C#
Contoso.Infrastructure.dll!Infrastructure.Modules.Azure.AzureActiveDirectoryModule.Load.AnonymousMethod__2_2(Autofac.IComponentContext c) Line 30   C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveNamed<string>(Autofac.IComponentContext context, string serviceName, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveNamed<string>(Autofac.IComponentContext context, string serviceName)    Unknown
Contoso.Infrastructure.dll!Infrastructure.Modules.PersistenceModule.Load.AnonymousMethod__9_0(Autofac.IComponentContext c) Line 54  C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<System.Data.Common.DbConnection>(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<System.Data.Common.DbConnection>(Autofac.IComponentContext context)    Unknown
Contoso.Infrastructure.dll!Infrastructure.Modules.PersistenceModule.Load.AnonymousMethod__9_1(Autofac.IComponentContext c) Line 62  C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid id, System.Func<object> creator)    Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Infrastructure.Persistence.IDbContext>(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Infrastructure.Persistence.IDbContext>(Autofac.IComponentContext context)  Unknown
Contoso.Infrastructure.dll!Infrastructure.Modules.PersistenceModule.Load.AnonymousMethod__9_2(Autofac.IComponentContext c) Line 68  C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid id, System.Func<object> creator)    Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.AutowiringParameter.CanSupplyValue.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()    Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid id, System.Func<object> creator)    Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.Features.Collections.CollectionRegistrationSource.RegistrationsFor.AnonymousMethod__1(Autofac.Core.IComponentRegistration cr)   Unknown
System.Core.dll!System.Linq.Enumerable.WhereSelectArrayIterator<System.__Canon, System.__Canon>.MoveNext()  Unknown
System.Core.dll!System.Linq.Buffer<object>.Buffer(System.Collections.Generic.IEnumerable<object> source)    Unknown
System.Core.dll!System.Linq.Enumerable.ToArray<object>(System.Collections.Generic.IEnumerable<object> source)   Unknown
Autofac.dll!Autofac.Features.Collections.CollectionRegistrationSource.RegistrationsFor.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)    Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<System.Collections.Generic.IEnumerable<Dispatching.Core.Tasks.Core.ITasksConfigurationSet>>(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<System.Collections.Generic.IEnumerable<Dispatching.Core.Tasks.Core.ITasksConfigurationSet>>(Autofac.IComponentContext context) Unknown
Contoso.Dispatching.Core.dll!Dispatching.Core.Modules.TaskRunnersModule.Load.AnonymousMethod__2_0(Autofac.IComponentContext c) Line 39  C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid id, System.Func<object> creator)    Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.AutowiringParameter.CanSupplyValue.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()    Unknown
Autofac.dll!Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute.AnonymousMethod__0()  Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid id, System.Func<object> creator)    Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Dispatching.Core.Tasks.Core.ITaskManager>(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)    Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Dispatching.Core.Tasks.Core.ITaskManager>(Autofac.IComponentContext context)   Unknown
Contoso.Dispatching.Core.dll!Dispatching.Core.Modules.DispatchingModule.Load.AnonymousMethod__2_2(Autofac.IComponentContext c) Line 73  C#
Autofac.dll!Autofac.RegistrationExtensions.Register.AnonymousMethod__f(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Builder.RegistrationBuilder.ForDelegate.AnonymousMethod__0(Autofac.IComponentContext c, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> p)   Unknown
Autofac.dll!Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Resolving.InstanceLookup.Execute() Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope, Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)  Unknown
Autofac.dll!Autofac.Core.Resolving.ResolveOperation.Execute(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)    Unknown
Autofac.dll!Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)   Unknown
Autofac.dll!Autofac.Core.Container.ResolveComponent(Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)    Unknown
Autofac.dll!Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters, out object instance) Unknown
Autofac.dll!Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters) Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Dispatching.Core.Worker.DispatcherWorker>(Autofac.IComponentContext context, System.Collections.Generic.IEnumerable<Autofac.Core.Parameter> parameters)    Unknown
Autofac.dll!Autofac.ResolutionExtensions.Resolve<Dispatching.Core.Worker.DispatcherWorker>(Autofac.IComponentContext context)   Unknown
Topshelf.Autofac.dll!Topshelf.Autofac.ServiceConfiguratorExtensions.ConstructUsingAutofacContainer.AnonymousMethod__0_0(Topshelf.Runtime.HostSettings serviceFactory)   Unknown
Topshelf.dll!Topshelf.Builders.DelegateServiceBuilder<Dispatching.Core.Worker.DispatcherWorker>.Build(Topshelf.Runtime.HostSettings settings)   Unknown
Topshelf.dll!Topshelf.Builders.RunBuilder.Build(Topshelf.Builders.ServiceBuilder serviceBuilder)    Unknown
Topshelf.dll!Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost()   Unknown
Topshelf.dll!Topshelf.HostFactory.New(System.Action<Topshelf.HostConfigurators.HostConfigurator> configureCallback) Unknown
Contoso.Dispatcher.Shell.exe!Dispatcher.Shell.Program.Main() Line 48    C#
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Unknown
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()   Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)    Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()    Unknown

EDIT: Perhaps it's not a deadlock - it seems like it's freezing all threads. Here are the top of the stacks of the other threads -

mscorlib.dll!System.Threading.LazyInitializer.EnsureInitializedCore<System.__Canon>(ref System.__Canon target, ref bool initialized, ref object syncLock, System.Func<System.__Canon> valueFactory) Unknown
mscorlib.dll!System.Threading.LazyInitializer.EnsureInitialized<Infrastructure.Persistence.Migrator.FluentMigratorSchemaVersion.MigrationsInfo>(ref Infrastructure.Persistence.Migrator.FluentMigratorSchemaVersion.MigrationsInfo target, ref bool initialized, ref object syncLock, System.Func<Infrastructure.Persistence.Migrator.FluentMigratorSchemaVersion.MigrationsInfo> valueFactory) Unknown
Contoso.Infrastructure.dll!Infrastructure.Persistence.Migrator.FluentMigratorSchemaVersion.EnsureInitialized() Line 43  C#


mscorlib.dll!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle waitableSafeHandle, long millisecondsTimeout, bool hasThreadAffinity, bool exitContext)  Unknown
mscorlib.dll!System.Threading.WaitHandle.WaitOne(System.TimeSpan timeout, bool exitContext) Unknown
mscorlib.dll!System.Threading.WaitHandle.WaitOne(System.TimeSpan timeout)   Unknown
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Channel.InMemoryTransmitter.Runner()    Unknown


mscorlib.dll!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle waitableSafeHandle, long millisecondsTimeout, bool hasThreadAffinity, bool exitContext)  Unknown
mscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) Unknown
mscorlib.dll!System.Threading.WaitHandle.WaitOne()  Unknown
Microsoft.ServiceBus.dll!Microsoft.ServiceBus.Common.AsyncResult.End<Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations.GetAsyncResult<Microsoft.ServiceBus.Messaging.TopicDescription>>(System.IAsyncResult result)   Unknown
Microsoft.ServiceBus.dll!Microsoft.ServiceBus.Common.AsyncResult<Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations.GetAsyncResult<Microsoft.ServiceBus.Messaging.TopicDescription>>.End(System.IAsyncResult asyncResult)  Unknown
Microsoft.ServiceBus.dll!Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations.EndGet<Microsoft.ServiceBus.Messaging.TopicDescription>(System.IAsyncResult asyncResult)   Unknown
Microsoft.ServiceBus.dll!Microsoft.ServiceBus.NamespaceManager.EndTopicExists(System.IAsyncResult result)   Unknown
Microsoft.ServiceBus.dll!Microsoft.ServiceBus.NamespaceManager.TopicExists(string path) Unknown
Contoso.Infrastructure.dll!Infrastructure.Azure.ServiceBus.ServiceBusTopic.Setup() Line 136 C#

If you can't make it asynchronous all the way, then make it synchronous all the way:

public class AzureActiveDirectoryService : IAzureActiveDirectoryService
{
    public string GetDatabaseAccessToken(AuthenticationContext authContext, ClientAssertionCertificate certCred)
    {
        try
        {
            return AcquireDatabaseToken(certCred, authContext);
        }
        catch (Exception ex)
        {
            Log.Error(ex, "Exception occurred while attempting to retrieve database access token.");
        }

        return string.Empty;
    }

    private string AcquireDatabaseToken(ClientAssertionCertificate certCred, AuthenticationContext authContext)
    {
        try
        {
            DoHttpRequest();
            return string.Empty;
        }
        catch (Exception ex)
        {
            Log.Error(ex,
                      "An error occurred",
                      ex.ToString());
        }
    }

    private string DoHttpRequest()
    {
        using (WebClient client = new WebClient())
        {
            return client.DownloadString("http://www.google.com");
        }
        return string.Empty;
    }
}

The fix was to do

return AcquireDatabaseToken(certCred,authContext).ConfigureAwait(false).GetAwaiter().GetResult();

instead of

return AcquireDatabaseToken(certCred,authContext).Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM