After ClickOnce update, "...cannot access the file because it is being used by another process" for an assembly

8

Problem description

I support a WPF ClickOnce deployed application. The application has a feature that reports exceptions to me by email. After each deployment, I usually get one or two exception reports like the one shown below. There are fewer than 100 users.

The call stacks are not identical. It always says, "Could not load file or assembly.." and then lists a DLL like EntityFramework or Unity. It also says, "(Exception from HRESULT: 0x80070020)"

I have automatic ClickOnce updates disabled. I am handling the updates with the ApplicationDeployment API. After an update has been downloaded, I display a status bar message, "Restart to run the latest version." The user then restarts the application. After restarting the application, the "cannot access the file because it is being used by another process" exception occurs. Generally, if the user restarts the application again, the exception no longer occurs.

Another developer described a similar experience here.

What is causing this?

I initially thought perhaps the previous version of the app was still shutting down when the user started the new version. However, the application is a single-instance application. The single-instance feature is implemented in Main() well before this exception is thrown. That makes me fairly confident this is not caused by two instances of my application. And besides, my application is not directly reading the EntityFramework or Unity DLLs.

I have two remaining hypotheses.

  1. A virus scanner is locking the DLLs that ClickOnce has freshly copied.
  2. There is a Microsoft bug in the ClickOnce update mechanism.

My question is, "Does anyone know what is causing this?"

Sample call stack #1

Here is one example:

 System.IO.FileLoadException:
 Could not load file or assembly
 'Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
 or one of its dependencies.
 The process cannot access the file because
 it is being used by another process.
 (Exception from HRESULT: 0x80070020)

    at MyNamespace.AppState.RestartTemplateCacheThread(UserOptions userOptions)
    at MyNamespace.AppState.RestartTemplateCacheThread()
    at MyNamespace.MainWindow.hyperlinkReconnect(Object sender, RoutedEventArgs e)

    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    at System.Windows.ContentElement.RaiseEvent(RoutedEventArgs e)
    at System.Windows.Documents.Hyperlink.OnClick()
    at System.Windows.Documents.Hyperlink.DispatchNavigation(Object sender)
    at System.Windows.Documents.Hyperlink.DoUserInitiatedNavigation(Object sender)
    at System.Windows.Documents.Hyperlink.OnMouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
    at System.Windows.Documents.Hyperlink.OnMouseLeftButtonUp(MouseButtonEventArgs e)
    at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
    at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    at System.Windows.ContentElement.RaiseTrustedEvent(RoutedEventArgs args)
    at System.Windows.Input.InputManager.ProcessStagingArea()
    at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
    at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
    at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
    at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

The line in RestartTemplateCacheThread(...) is...

var ioc = new UnityContainer().LoadConfiguration();

...which presumably is the first time the Unity DLL is accessed.

Sample call stack #2

In this example, the Unity DLL is successfully loaded, but the EntityFramework DLL is locked.

 System.IO.FileLoadException: 
 Could not load file or assembly
 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
 or one of its dependencies.
 The process cannot access the file because it is being used by another process.
 (Exception from HRESULT: 0x80070020)

    at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
    at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
    at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
    at System.Type.GetType(String typeName)

    at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveAlias(String typeNameOrAlias)
    at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveTypeInternal(String typeNameOrAlias)
    at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails)
    at Microsoft.Practices.Unity.Configuration.RegisterElement.GetMappedType()
    at Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container)
    at Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element)
    at Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action)
    at Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container)
    at Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName)
    at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName)
    at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container)

    at MyNamespace.AppState.RestartTemplateCacheThread(UserOptions userOptions)
    at MyNamespace.AppState.SetUserOptions(UserOptions options, Boolean restartBackgroundThreads)
    at MyNamespace.Models.DatasetManager.LoadDataset(String path)
    at MyNamespace.MainWindow.MainWindow_Loaded(Object sender, RoutedEventArgs args)

    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
    at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
    at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
    at MS.Internal.LoadedOrUnloadedOperation.DoWork()
    at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
    at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
    at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
    at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
c#
clickonce
asked on Stack Overflow Feb 7, 2014 by Wallace Kelly

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0