private void Startup() { // Hack - force log4net to initialise before Metrics.net _log.Debug(""); // Global exception handler in case anything blows AppDomain.CurrentDomain.UnhandledException += (o, e) => { _log.Fatal($"Unhandled exception thrown: {e.ExceptionObject}"); }; // Now Metrics.NET Metric.Config.WithHttpEndpoint("http://localhost:14999/"); // Topshelf configuration HostFactory.Run(config => { config.Service<SearchMonkeyService>(service => { service.ConstructUsing(name => { var notifier = new SlackNotifier(ConfigurationManager.AppSettings["SlackServiceUri"]); var finder = new EbayFinder() .Configure(c => { // Initialize service end-point configuration c.EndPointAddress = "http://svcs.ebay.com/services/search/FindingService/v1"; c.GlobalId = "EBAY-GB"; // set eBay developer account AppID here! c.ApplicationId = ConfigurationManager.AppSettings["eBayApplicationId"]; }); var factory = new SearchRepositoryFactory(); var resultAggregator = new ResultAggregator(notifier, Int32.Parse(ConfigurationManager.AppSettings["ReportingPeriod"])); using (var repo = factory.CreateSearchRepository()) { var reviewer = new CompletedItemReviewer(repo.GetBasicSuspiciousPhrases().ToList()); return new SearchMonkeyService(finder, notifier, factory, reviewer, resultAggregator); } }); service.WhenStarted(svc => svc.Start()); service.WhenStopped(svc => svc.Stop()); }); config.RunAsLocalSystem(); config.SetDescription(ServiceDescription); config.SetDisplayName(ServiceDisplayName); config.SetServiceName(ServiceName); }); }
private void Run() { try { var repo = new SoldOutRepository(); // Create the search catalogue var searches = repo.GetAllSearchesWithSearchCriteria(); conditionResolver = new ConditionResolver(repo.GetConditions()); var finder = new EbayFinder() .Configure(c => { // Initialize service end-point configuration c.EndPointAddress = "http://svcs.ebay.com/services/search/FindingService/v1"; c.GlobalId = "EBAY-GB"; // set eBay developer account AppID here! c.ApplicationId = ConfigurationManager.AppSettings["eBayApplicationId"]; }); foreach (var search in searches) { Console.WriteLine("Requesting completed items for '{0}' {1}", search.Name, (search.LastRun != null) ? "since " + search.LastRun.ToString() : string.Empty); // Create a request to get our completed items var response = finder.GetCompletedItems(search, 1); // Show output if (response.ack == AckValue.Success || response.ack == AckValue.Warning) { Console.WriteLine("Found " + response.searchResult.count + " new items"); // Set the last ran time search.LastRun = response.timestamp; if (response.searchResult.count > 0) { // Map returned items to our SoldItems model var newItems = eBayMapper.MapSearchItemsToSearchResults(response.searchResult.item, conditionResolver, search.ProductId); // Add them to the relevant search repo.AddSearchResults(search.SearchId, newItems); } repo.SaveAll(); } else { Console.WriteLine(string.Format("Request failed: {0}", response.ack.ToString())); response.errorMessage.ToList().ForEach(e => Console.WriteLine(e.message)); } } } catch (Exception ex) { Console.WriteLine("Error: " + ex); } }