Bug 43826 - Apple Watch 3.0 extension reads from wrong file directory
Summary: Apple Watch 3.0 extension reads from wrong file directory
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.WatchOS.dll ()
Version: XI 9.99 (iOS 10 previews)
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-08-29 11:47 UTC by Christer Nordvik
Modified: 2016-08-29 13:51 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Christer Nordvik 2016-08-29 11:47:47 UTC
When downloading data in the background on the Apple Watch simulator I get a callback with the location of the temp file. The file exists on disk and the location is correct but when I try a "File.ReadAllText" it tries to read the file from a wrong location. To me this looks like a bug? 

public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
   Console.WriteLine("File downloaded in : {0}", location);
   var xml = File.ReadAllText(location.ToString());   

2016-08-29 13:43:34.309 FotMobFotMobWatchAppV2Extension[46076:10183221] File downloaded in : file:///Users/cnordvik/Library/Developer/CoreSimulator/Devices/30661A28-1ADC-4632-8101-C09B19F59043/data/Containers/Data/PluginKitPlugin/82BBBADC-A18C-43D7-BE19-1C59DE188EB7/Library/Caches/com.apple.nsurlsessiond/Downloads/com.mobilefootie.fotmobpro.watchkitapp.watchkitextension/CFNetworkDownload_Xvo2wA.tmp
2016-08-29 13:43:34.332 FotMobFotMobWatchAppV2Extension[46076:10183221] *** Terminating app due to uncaught exception 'System.IO.DirectoryNotFoundException', reason: 'Could not find a part of the path "/Users/cnordvik/Library/Developer/CoreSimulator/Devices/30661A28-1ADC-4632-8101-C09B19F59043/data/Containers/Bundle/Application/B296C1BB-0BB1-4AC3-9D30-50D31C23C3D8/FotMobFotMobWatchAppV2.app/PlugIns/FotMobFotMobWatchAppV2Extension.appex/file:///Users/cnordvik/Library/Developer/CoreSimulator/Devices/30661A28-1ADC-4632-8101-C09B19F59043/data/Containers/Data/PluginKitPlugin/82BBBADC-A18C-43D7-BE19-1C59DE188EB7/Library/Caches/com.apple.nsurlsessiond/Downloads/com.mobilefootie.fotmobpro.watchkitapp.watchkitextension/CFNetworkDownload_Xvo2wA.tmp".'
*** First throw call stack:
	0   CoreFoundation                      0x00666a22 __exceptionPreprocess + 194
	1   libobjc.A.dylib                     0x0568fe76 objc_exception_throw + 52
	2   FotMobFotMobWatchAppV2Extension     0x0030a7a5 xamarin_process_managed_exception + 965
	3   FotMobFotMobWatchAppV2Extension     0x00310d17 xamarin_invoke_trampoline + 8999
	4   FotMobFotMobWatchAppV2Extension     0x0031700c xamarin_arch_trampoline + 156
	5   FotMobFotMobWatchAppV2Extension     0x003176e6 xamarin_i386_common_trampoline + 36
	6   CFNetwork                           0x0504c673 __82-[NSURLSession delegate_downloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 40
	7   Foundation                          0x015b9f4b __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 12
	8   Foundation                          0x015b9c1c -[NSBlockOperation main] + 105
	9   Foundation                          0x015b83f2 -[__NSOperationInternal _start:] + 672
	10  Foundation                          0x015b814a -[NSOperation start] + 70
	11  Foundation                          0x015b41fa __NSOQSchedule_f + 220
	12  libdispatch.dylib                   0x05f6450f _dispatch_client_callout + 14
	13  libdispatch.dylib                   0x05f48d82 _dispatch_queue_serial_drain + 908
	14  libdispatch.dylib                   0x05f49570 _dispatch_queue_invoke + 1103
	15  libdispatch.dylib                   0x05f4b304 _dispatch_root_queue_drain + 384
	16  libdispatch.dylib                   0x05f4b11c _dispatch_worker_thread3 + 134
	17  libsystem_pthread.dylib             0x062e725c _pthread_wqthread + 1050
	18  libsystem_pthread.dylib             0x062e4f56 start_wqthread + 34
libc++abi.dylib: terminating with uncaught exception of type NSException
Comment 1 Christer Nordvik 2016-08-29 13:22:17 UTC
Just FYI the iOS libraries can find the file and move it around just fine. 

The code below works and the code enters the if(success) block, but then crashes when trying to read the file. Is there no native API for reading files that I can use instead of File.ReadAllText?  

NSError error;
NSFileManager fileManager = NSFileManager.DefaultManager;
var docDirectory = fileManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0];

var destinationFilename = downloadTask.OriginalRequest.Url.LastPathComponent;

var destinationURL = docDirectory.AppendPathExtension(destinationFilename);

if (fileManager.FileExists(destinationURL.Path))
	fileManager.Remove(destinationURL, out error);

var success = fileManager.Copy(location, destinationURL, out error);

if (success)
	Logging.Debug("Copied file!");
	var xml = File.ReadAllText(destinationURL.ToString());
Comment 2 Rolf Bjarne Kvinge [MSFT] 2016-08-29 13:37:17 UTC
I think you're trying to pass a file:/// url to File.ReadAllText, but that API wants a local path, not a url.

Can you try this instead?

    File.ReadAllText (destinationURL.Path)
Comment 3 Christer Nordvik 2016-08-29 13:51:24 UTC
Sorry, you are correct! Thanks :-)