private bool TrySetConnectionStringAndTaskHubName()

in src/Cli/func/Common/DurableManager.cs [60:129]


        private bool TrySetConnectionStringAndTaskHubName()
        {
            // Set connection string key and task hub name to defaults
            _connectionStringKey = DefaultConnectionStringKey;
            _taskHubName = DefaultTaskHubName;

            try
            {
                if (File.Exists(ScriptConstants.HostMetadataFileName))
                {
                    // Attempt to retrieve Durable override settings from host.json
                    dynamic hostSettings = JObject.Parse(File.ReadAllText(ScriptConstants.HostMetadataFileName));
                    JObject durableTask = null;

                    string version = hostSettings["version"];
                    if (version?.Equals("2.0") == true)
                    {
                        // If the version is (explicitly) 2.0, prepend path to 'durableTask' with 'extensions'
                        durableTask = hostSettings?.extensions?.durableTask;
                    }
                    else
                    {
                        durableTask = hostSettings?.durableTask;
                    }

                    if (durableTask != null)
                    {
                        // Override connection string or task hub name if they exist in host.json
                        _connectionStringKey = durableTask.GetValue("AzureStorageConnectionStringName", StringComparison.OrdinalIgnoreCase)?.ToString()
                            ?? _connectionStringKey;
                        _taskHubName = durableTask.GetValue("HubName", StringComparison.OrdinalIgnoreCase)?.ToString()
                            ?? _taskHubName;

                        if (durableTask.TryGetValue("storageProvider", StringComparison.OrdinalIgnoreCase, out JToken storageProviderToken))
                        {
                            if (storageProviderToken is JObject storageProviderObject)
                            {
                                if (storageProviderObject.TryGetValue("type", out JToken typeValue) && typeValue.Type == JTokenType.String)
                                {
                                    if (Enum.TryParse(typeValue.Value<string>(), ignoreCase: true, out BackendType backendType))
                                    {
                                        BackendType = backendType;
                                    }
                                }

                                _partitionCount = storageProviderObject?.GetValue("partitionCount", StringComparison.OrdinalIgnoreCase)?.Value<int?>();
                            }
                            else
                            {
                                throw new CliException("The host.json file contains an invalid storageProvider schema in the durableTask section.");
                            }
                        }
                    }
                }
                else
                {
                    ColoredConsole.WriteLine(WarningColor($"Could not find local host metadata file '{ScriptConstants.HostMetadataFileName}'"));
                }
            }
            catch (Exception e)
            {
                // We can't throw here because that would result in an obscure error message about dependency injection in the command output.
                // Instead of throwing, we write a warning and return a false value, and another part of the code will throw an exception.
                ColoredConsole.WriteLine(WarningColor($"Exception thrown while attempting to parse task hub configuration from '{ScriptConstants.HostMetadataFileName}':"));
                ColoredConsole.WriteLine(WarningColor(e.Message));
                return false;
            }

            return true;
        }