即使我使用的是ConfigureAwait(false),我也会在某些C#代码上遇到死锁。不幸的是,我无法全面使用异步,因此我依赖于ConfigureAwait。
情况是我需要发出HTTP请求,以便在启动Web应用程序和控制台应用程序期间从Azure AD检索数据库访问令牌。在这两种情况下,当Autofac尝试解析触发Web请求的数据库令牌时,程序会死锁。奇怪的是它可以工作两次但第三次失败。
Autofac模块:
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);
}
}
发出HTTP请求的服务(我已通过调用google取代了http请求):
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;
}
}
这里是死锁线程的(编辑过的)堆栈跟踪(在控制台应用中) -
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
编辑:也许它不是一个僵局 - 似乎它冻结了所有线程。这是其他线程堆栈的顶部 -
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#
答案 0 :(得分:5)
如果你不能让它一直异步,那么让它一直同步:
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;
}
}
答案 1 :(得分:1)
解决方法是
return AcquireDatabaseToken(certCred,authContext).ConfigureAwait(false).GetAwaiter().GetResult();
而不是
return AcquireDatabaseToken(certCred,authContext).Result;