Exemplo n.º 1
0
        /// <inheritdoc/>
        public void RegisterServiceClient <T>(IOpenStackServiceClientDefinition clientDefinition) where T : IOpenStackServiceClient
        {
            var servicetType = typeof(T);

            if (this.serviceClientDefinitions.ContainsKey(servicetType))
            {
                throw new InvalidOperationException(
                          string.Format(
                              "A service of type '{0}' has already been registered, and cannot be registered again.",
                              servicetType.Name));
            }

            this.serviceClientDefinitions.Add(servicetType, clientDefinition);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an instance of the given client type using the given factory function, credential, and cancellation token.
        /// </summary>
        /// <param name="clientDefinition">A object that can be used to validate and create the give client type.</param>
        /// <param name="credential">The credential to be used by the created client.</param>
        /// <param name="serviceName">The name of the service to be used by the created client.</param>
        /// <param name="cancellationToken">The cancellation token to be used by the created client.</param>
        /// <returns>An instance of the requested client.</returns>
        internal IOpenStackServiceClient CreateServiceClientInstance(IOpenStackServiceClientDefinition clientDefinition, ICredential credential, string serviceName, CancellationToken cancellationToken)
        {
            clientDefinition.AssertIsNotNull("clientDefinition", "Cannot create an OpenStack service with a null client definition.");

            IOpenStackServiceClient instance;

            try
            {
                instance = clientDefinition.Create(credential, serviceName, cancellationToken, this.ServiceLocator) as IOpenStackServiceClient;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Could not create a service of type '{0}'. See inner exception for details.", clientDefinition.Name), ex);
            }

            if (instance != null)
            {
                return(instance);
            }
            throw new InvalidOperationException(string.Format("Could not create a service of type '{0}'. The type does not derive from or cast to IOpenStackClient. ", clientDefinition.Name));
        }