public static long?GetRowCount(string streamPath) { var certificate = CertificateGenerator.GetCertificateByThumbprint(); var settings = new Microsoft.Cosmos.ExportClient.ScopeExportSettings(); settings.Path = streamPath; settings.ClientCertificate = certificate; var exportClient = new Microsoft.Cosmos.ExportClient.ExportClient(settings); try { return(exportClient.GetRowCount(null).Result); } // we cannot catch the exception "CosmosFileNotFoundException" by using CosmosUriException, //catch (Microsoft.Cosmos.CosmosUriException e) // and we either cannot use this exception "CosmosFileNotFoundException". // For the asynchronous function GetRowCount(We can see its return is a subclass of Task), its exception will be packaged as AggregateException, we cannot directly catch it. catch (CosmosFileNotFoundException e) { return(null); } // We just can catch AggregateException and judge it in AggregateException.InnerException // AggregateException: Represents one or more errors that occur during application execution. // Doc link: https://docs.microsoft.com/en-us/dotnet/api/system.aggregateexception?view=netframework-4.8 catch (AggregateException e) { // Here we just want to catch the Exception "CosmosFileNotFoundException", if it is not the reason, we will throw the exception. if (e.InnerException.GetType() == typeof(CosmosFileNotFoundException)) { Console.WriteLine("Cannot find the file..."); return(null); } if (e.InnerException.GetType() == typeof(CosmosDirectoryNotFoundException)) { Console.WriteLine("Cannot find the directory..."); return(null); } if (e.InnerException.GetType() == typeof(CosmosArgumentException)) { Console.WriteLine($"CosmosArgumentException...{e.InnerException.Message}"); return(null); } throw; } // This can catch any exception.. catch (Exception e) { Console.WriteLine($"Message: {e}; Type: {e.GetType()}"); return(null); } }
public void Export(string stream, string filename) { var settings = new Microsoft.Cosmos.ExportClient.ScopeExportSettings(); settings.Path = stream; var exportClient = new Microsoft.Cosmos.ExportClient.ExportClient(settings); // Top if (this.Top > 0) { settings.Top = this.Top; } // Columns settings.ColumnFilters = this.Columns; // Partitions // if PartitionIndices is set to null, or not set, default to get all partitions settings.PartitionIndices = exportClient.GetAllPartitionIndices(null).Result; try { var task = exportClient.Export(null, new System.Threading.CancellationToken()); var readTask = task.ContinueWith((prevTask) => { using (Microsoft.Cosmos.ExportClient.IExportDataReader dataReader = prevTask.Result.DataReader) { OutputToFile(dataReader, filename, this.Separator, this.Terminator); Console.WriteLine("New Thread"); } }); readTask.Wait(); } catch (System.AggregateException ae) { //System.Console.WriteLine(ae.ToString()); throw; } }
// This function also just can check the existing of file not directory. public static bool CheckExists(string streamPath, out long rowCount) { var certificate = CertificateGenerator.GetCertificateByThumbprint(); var settings = new Microsoft.Cosmos.ExportClient.ScopeExportSettings(); settings.Path = streamPath; settings.ClientCertificate = certificate; var exportClient = new Microsoft.Cosmos.ExportClient.ExportClient(settings); try { rowCount = exportClient.GetRowCount(null).Result; return(true); } catch (Exception e) { Console.WriteLine($"Message: {e.Message}; Type: {e.GetType()}"); rowCount = -1; return(false); } }