static void Main(string[] args) { //consultaSituacao(); //NewMethod(); //conn = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings["TaxAuditCommunity"].ConnectionString; //hostPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Path"].Value; //TaxAuditCommunity.Factory.FileWatcher.Watcher watcher = // new TaxAuditCommunity.Factory.FileWatcher.Watcher(hostPath, conn); //ThreadStart startWatcher = new ThreadStart(watcher.FileWatcher); //Thread threadWatcher = new Thread(startWatcher); //threadWatcher.Start(); //ThreadStart Begining = new ThreadStart(watcher.Beging); //Thread threadBeginig = new Thread(Begining); //while (true) //{ // //threadBeginig.Start(); // //watcher.Beging(); //} var conn = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) .ConnectionStrings.ConnectionStrings["TaxAuditCommunity"].ConnectionString; var connProsoft = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) .ConnectionStrings.ConnectionStrings["Pervasive"].ConnectionString; using (var db = new NFeDbContext(conn)) { IStoreEmpresas store = new StoreEmpresas(db); var teste = store.Syncronization(connProsoft, default(CancellationToken)).Result; } }
private void OnChangedProsoft(object sender, FileSystemEventArgs e) { try { using (var db = new NFeDbContext(conn)) { IStoreEmpresas store = new StoreEmpresas(db); var teste = store.Syncronization(connProsoft, default(CancellationToken)).Result; } } catch (Exception err) { if (eventLog1 != null) { eventLog1.WriteEntry(err.ToString(), EventLogEntryType.Error, 15); } else { Console.WriteLine(err.ToString()); } } }
private void OnChanged(object sender, FileSystemEventArgs e) { switch (e.ChangeType) { case WatcherChangeTypes.Created: { var arquivoXml = e.FullPath; try { bool isFileOpened = true; FileInfo xml = new FileInfo(e.FullPath); while (isFileOpened) { try { xml.OpenRead(); isFileOpened = false; } catch { isFileOpened = true; } } XmlDocument document = new XmlDocument(); document.Load(xml.OpenRead()); if (document.DocumentElement.Name == "nfeProc") { NFe NFe = new NFe(); NFe.Hash = xml.CreateHash(); var node = document.GetElementsByTagName("NFe")[0]; NFe.XmlNFe = new XmlNFe { DhChange = xml.LastWriteTimeUtc, XmlDocument = XElement.Parse(document.InnerXml) }; NFe.SetProperties(node); using (var db = new NFeDbContext(conn)) { INFeStore <NFe, NFeResult> store = new NFeStore <NFe, NFeDbContext>(db); NFeManager manager = new NFeManager(store); var result = manager.GravarNFe(NFe).Result; if (result.Succeeded) { if (eventLog1 != null) { eventLog1.WriteEntry($"O arquivo {xml.Name} foi gravado com sucesso.", EventLogEntryType.SuccessAudit, 1); } else { Console.WriteLine($"O arquivo {xml.Name} foi gravado com sucesso."); } } else { switch (result.NFeResultException) { case NFeResultException.DbUpdateConcurrencyException: { if (eventLog1 != null) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.DbUpdateConcurrencyException.ToString()}", EventLogEntryType.Error, 2); } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.DbUpdateConcurrencyException.ToString()}"); } } break; case NFeResultException.DbUpdateException: { if (eventLog1 != null) { if (result.ErrorNumber != 2627) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.DbUpdateException.InnerException.ToString()}", EventLogEntryType.Error, result.ErrorNumber); } } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.DbUpdateException.InnerException.ToString()}"); } } break; case NFeResultException.Exception: { if (eventLog1 != null) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.Exception.ToString()}", EventLogEntryType.Error, 3); } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.Exception.ToString()}"); } } break; } } } } else { if (eventLog1 != null) { eventLog1.WriteEntry($"O arquivo xml {xml.Name} na pasta {xml.FullName.Replace(xml.Name, "")} " + $"não é um arquivo de NFe valido ou o arquivo não foi transmitido para a SEFAZ." + $"A Tag nfeProc não foi localizada.\n O arquivo não será gravado", EventLogEntryType.Error, 5); } else { Console.WriteLine($"O arquivo xml {xml.Name} na pasta {xml.FullName.Replace(xml.Name, "")} " + $"não é um arquivo de NFe valido ou o arquivo não foi transmitido para a SEFAZ." + $"A Tag nfeProc não foi localizada.\n O arquivo não será gravado"); } } } catch (XmlException err) { if (eventLog1 != null) { eventLog1.WriteEntry(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}", EventLogEntryType.Error, 6); } else { Console.WriteLine(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}"); } } catch (Exception err) { if (eventLog1 != null) { eventLog1.WriteEntry(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}", EventLogEntryType.Error, 7); } else { Console.WriteLine(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}"); } } } break; } }
public void Beging() { try { DirectoryInfo directory = new DirectoryInfo(hostPath); List <FileInfo> files = directory.GetFiles("*.xml", SearchOption.AllDirectories).ToList(); if (files.Count() > 0) { int i = 0; foreach (var xml in files.OrderByDescending(x => x.CreationTimeUtc)) { var arquivoXml = xml.FullName; XmlDocument document = new XmlDocument(); try { i++; document.Load(xml.FullName); if (document.DocumentElement.Name == "nfeProc") { NFe NFe = new NFe(); NFe.Hash = xml.CreateHash(); var node = document.GetElementsByTagName("NFe")[0]; NFe.XmlNFe = new XmlNFe { DhChange = xml.CreationTimeUtc, XmlDocument = XElement.Parse(document.InnerXml) }; NFe.SetProperties(node); using (var db = new NFeDbContext(conn)) { INFeStore <NFe, NFeResult> store = new NFeStore <NFe, NFeDbContext>(db); NFeManager manager = new NFeManager(store); var result = manager.GravarNFe(NFe).Result; if (result.Succeeded) { eventLog1.WriteEntry($"O arquivo {xml.Name} foi gravado com sucesso.", EventLogEntryType.SuccessAudit); } else { switch (result.NFeResultException) { case NFeResultException.DbUpdateConcurrencyException: { if (eventLog1 != null) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.DbUpdateConcurrencyException.ToString()}", EventLogEntryType.Error, 1); } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.DbUpdateConcurrencyException.ToString()}"); } } break; case NFeResultException.DbUpdateException: { if (eventLog1 != null) { if (result.ErrorNumber != 2627) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.DbUpdateException.InnerException.ToString()}", EventLogEntryType.Error, result.ErrorNumber); } } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.DbUpdateException.InnerException.ToString()}"); } } break; case NFeResultException.Exception: { if (eventLog1 != null) { eventLog1.WriteEntry($"Erro: {xml.FullName} - " + $"{result.Exception.ToString()}", EventLogEntryType.Error, 0); } else { Console.WriteLine($"Erro: {xml.FullName} - " + $"{result.Exception.ToString()}"); } } break; } } } } else { if (eventLog1 != null) { eventLog1.WriteEntry($"O arquivo xml {xml.Name} na pasta {xml.FullName.Replace(xml.Name, "")} " + $"não é um arquivo de NFe valido ou o arquivo não foi transmitido para a SEFAZ." + $"A Tag nfeProc não foi localizada.\n O arquivo não será gravado", EventLogEntryType.Error, 5); } else { Console.WriteLine($"O arquivo xml {xml.Name} na pasta {xml.FullName.Replace(xml.Name, "")} " + $"não é um arquivo de NFe valido ou o arquivo não foi transmitido para a SEFAZ." + $"A Tag nfeProc não foi localizada.\n O arquivo não será gravado"); } } } catch (XmlException err) { if (eventLog1 != null) { eventLog1.WriteEntry(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}", EventLogEntryType.Error, 6); } else { Console.WriteLine(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}"); } } catch (Exception err) { if (eventLog1 != null) { eventLog1.WriteEntry(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}", EventLogEntryType.Error, 7); } else { Console.WriteLine(err.InnerException == null ? $"Arquivo: {arquivoXml}\n {err.ToString()}" : $"Arquivo: {arquivoXml}\n{err.InnerException.ToString()}"); } } } } } catch (Exception e) { if (eventLog1 != null) { eventLog1.WriteEntry(e.InnerException == null ? e.ToString() : e.InnerException.ToString(), EventLogEntryType.Error, 8); } else { Console.WriteLine(e.InnerException == null ? e.ToString() : e.InnerException.ToString()); } } finally { if (eventLog1 != null) { eventLog1.WriteEntry("Concluido a verificação completa", EventLogEntryType.Warning, 9); } else { Console.WriteLine("Concluido a verificação completa"); } } }
private static void NewMethod() { conn = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings["TaxAuditCommunity"].ConnectionString; hostPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Path"].Value; try { DateTime lastsaveed = new DateTime(); Task.Run(async() => { using (var db = new NFeDbContext(conn)) { INFeStore <NFe, NFeResult> store = new NFeStore <NFe, NFeDbContext>(db); NFeManager manager = new NFeManager(store); lastsaveed = await manager.LastSaveed(); } }).Wait(); DirectoryInfo directory = new DirectoryInfo(hostPath); var files = directory.GetFiles("*.xml", SearchOption.AllDirectories) .Where(d => d.CreationTimeUtc >= lastsaveed) .OrderBy(d => d.CreationTimeUtc); if (files.Count() > 0) { int i = 0; foreach (var xml in files) { arquivoXml = xml.FullName; XmlDocument document = new XmlDocument(); try { i++; document.Load(xml.FullName); if (document.DocumentElement.Name == "nfeProc") { NFe NFe = new NFe(); var node = document.GetElementsByTagName("NFe")[0]; NFe.XmlNFe = new XmlNFe { DhChange = xml.LastWriteTimeUtc, XmlDocument = XElement.Parse(document.InnerXml) }; NFe.SetProperties(node); using (var db = new NFeDbContext(conn)) { INFeStore <NFe, NFeResult> store = new NFeStore <NFe, NFeDbContext>(db); NFeManager manager = new NFeManager(store); var result = manager.GravarNFe(NFe).Result; if (result.Succeeded) { Console.WriteLine($"Index: {i} - O arquivo {xml.Name} foi gravado com sucesso."); } else { switch (result.NFeResultException) { case NFeResultException.DbUpdateConcurrencyException: { Console.WriteLine($"Index: {i} - Erro: {xml.Name} - {result.DbUpdateConcurrencyException.ToString()}"); } break; case NFeResultException.DbUpdateException: { if (result.DbUpdateException.InnerException.ToString().Contains("truncated.")) { Console.WriteLine(result.DbUpdateException.Entries[0].Entity.ToString()); } Console.WriteLine($"Index: {i} - Erro: {xml.Name} - {result.DbUpdateException.InnerException.ToString()}"); } break; case NFeResultException.Exception: { Console.WriteLine($"Index: {i} - Erro: {xml.Name} - {result.Exception.ToString()}"); } break; } } } } else { Console.WriteLine($"Index: {i} - O arquivo xml {xml.Name} na pasta {xml.FullName.Replace(xml.Name, "")} " + $"não é um arquivo de NFe valido ou o arquivo não foi transmitido para a SEFAZ." + $"A Tag nfeProc não foi localizada.\n O arquivo não será gravado"); } } catch (XmlException e) { Console.WriteLine($"Erro: {xml.Name} - {e.ToString()}"); } catch (Exception e) { Console.WriteLine(e.InnerException == null ? $"Index: {i} - Arquivo: {arquivoXml}\n {e.ToString()}" : $"Arquivo: {arquivoXml}\n{e.InnerException.ToString()}"); } } } } catch (Exception e) { Console.WriteLine(e.InnerException == null ? $"Arquivo: {arquivoXml}\n {e.ToString()}" : $"Arquivo: {arquivoXml}\n{e.InnerException.ToString()}"); } }