public static App Init(Action <HostBuilderContext, IServiceCollection> nativeConfigureServices) { var systemDir = FileSystem.CacheDirectory; ResourcesHelpers.ExtractSaveResource("XamarinFormsMSExtensions.appsettings.json", systemDir); var fullConfig = Path.Combine(systemDir, "XamarinFormsMSExtensions.appsettings.json"); var host = new HostBuilder() .ConfigureHostConfiguration(c => { c.AddCommandLine(new string[] { $"ContentRoot={FileSystem.AppDataDirectory}" }); c.AddJsonFile(fullConfig); }) .ConfigureServices((c, x) => { nativeConfigureServices(c, x); ConfigureServices(c, x); }) .ConfigureLogging(l => l.AddConsole(o => { o.DisableColors = true; })) .Build(); App.ServiceProvider = host.Services; return(App.ServiceProvider.GetService <App>()); }
private static List <DictionaryName> GetDictionaryNames(Enum enumName) { var cultures = EnumExtensions.GetValues <Cultures>(); var attributes = enumName.GetAttributes <DictionaryAttribute>(); var names = new List <DictionaryName>(); foreach (Cultures culture in cultures) { foreach (var attribute in attributes) { names.Add(new DictionaryName(ResourcesHelpers.GetString <Dictionaries>(attribute.Mapping.ToString(), culture), culture.ToString().ToUpper())); } } return(names); }
private static async Task CreateResourcesAsync(HttpClient httpClient, IList <ResourceDescription> resourceDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Resources must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Resource> existingResources = await space.GetExistingChildResourcesAsync(httpClient); var resourceIds = new List <Guid>(); var resourcesToCreate = resourceDescriptions.Where(rd => !existingResources.Any(er => er.Type.Equals(rd.type, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (ResourceDescription resourceDescription in resourcesToCreate) { Resource resource = resourceDescription.ToDigitalTwins(spaceId); Guid createdId = await CreateResourceAsync(httpClient, resource); if (createdId != Guid.Empty) { resourceIds.Add(createdId); } else { Console.WriteLine($"Failed to create resource. Please try manually: {resourceDescription.type}"); } } if (resourceIds.Any()) { // wait until all the resources are created and ready to use in case downstream operations (like device creation) // are dependent on it. Console.WriteLine("Polling until all resources are no longer in the provisioning state."); IEnumerable <Task <bool> > statusVerificationTasks = resourceIds.Select(resourceId => ResourcesHelpers.WaitTillResourceCreationCompletedAsync(httpClient, resourceId)); await Task.WhenAll(statusVerificationTasks); } }
private static async Task RemoveAllExistingResourcesAsync(HttpClient httpClient, IReadOnlyCollection <Space> rootSpaces) { if (!rootSpaces.Any()) { return; } Console.WriteLine(); IReadOnlyCollection <Resource> resourcesToRemove = await rootSpaces.GetExistingResourcesUnderSpacesAsync(httpClient); Console.WriteLine($"Removing {resourcesToRemove.Count} resources."); var resourceIdsDeleting = new List <Guid>(); foreach (Resource resourceToRemove in resourcesToRemove) { bool success = await resourceToRemove.DeleteResourceAsync(httpClient, JsonSerializerSettings); if (success) { resourceIdsDeleting.Add(Guid.Parse(resourceToRemove.Id)); } else { Console.WriteLine($"Failed to remove Resource, please try manually. (Id: {resourceToRemove.Id})"); } } if (resourceIdsDeleting.Any()) { Console.WriteLine("Polling until all resources have been deleted."); var tokenSource = new CancellationTokenSource(); ICollection <Task> statusVerificationTasks = resourceIdsDeleting .Select(resourceId => ResourcesHelpers.WaitTillResourceDeletionCompletedAsync(httpClient, resourceId, tokenSource.Token)) .ToArray(); // Ensuring that it won't sit here forever. Task processingTasks = Task.WhenAny(Task.WhenAll(statusVerificationTasks), Task.Delay(TimeSpan.FromMinutes(10))); ConsoleSpinner spinner = new ConsoleSpinner(); while (!processingTasks.IsCompleted) { Console.CursorVisible = false; spinner.Turn(); await Task.Delay(250); } if (statusVerificationTasks.Any(t => t.Status != TaskStatus.RanToCompletion)) { // Timeout occurred, need to cancel all the other tasks tokenSource.Cancel(); Console.WriteLine("Timeout occurred. Unable to verify that all resources have been deleted."); } else { Console.WriteLine("Resource deletion complete."); } } Console.WriteLine(); }