// Reference: https://csharp.christiannagel.com/2016/07/27/diwithoptions/ (November 23, 2019)
        /// <summary>
        /// Registers WMIContext in dependency injection container
        /// </summary>
        /// <typeparam name="TContext">WMI Context type</typeparam>
        /// <param name="serviceCollection">Object to be extended</param>
        /// <param name="optionsAction">Action to configure connection options</param>
        /// <param name="contextLifetime">Lifetime of Context in dependency inject container</param>
        /// <param name="optionsLifetime">Lifetime of Context options in dependency inject container</param>
        /// <returns>Dependency injection collection</returns>
        public static IServiceCollection AddWmiContext <TContext>(
            this IServiceCollection serviceCollection,
            Action <WmiContextOptionsBuilder> optionsAction = null,
            ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
            ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
            where TContext : WmiContext
        {
            serviceCollection.TryAdd(new ServiceDescriptor(
                                         typeof(WmiContextOptions <TContext>),
                                         p =>
            {
                var builder = new WmiContextOptionsBuilder <TContext>(
                    new WmiContextOptions <TContext>());
                optionsAction?.Invoke(builder);
                return(builder.Options);
            },
                                         optionsLifetime));
            serviceCollection.Add(new ServiceDescriptor(
                                      typeof(WmiContextOptions),
                                      p => p.GetRequiredService <WmiContextOptions <TContext> >(),
                                      optionsLifetime));

            serviceCollection.TryAdd(new ServiceDescriptor(
                                         typeof(TContext), typeof(TContext), contextLifetime));
            return(serviceCollection);
        }
Exemple #2
0
 /// <summary>
 /// Add Connection driver to use DCom connection
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="computerName">Computer name or IP address to connect</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <param name="options">Preconfigured <c>ConnectionOptions</c> object</param>
 /// <returns>WmiContextOptionsBuilder (itself)</returns>
 public static WmiContextOptionsBuilder UseDCom(
     this WmiContextOptionsBuilder optionsBuilder,
     string computerName, string nameSpace, ConnectionOptions options)
 {
     optionsBuilder.Options.Connection = new DComConnection(computerName, nameSpace, options);
     return(optionsBuilder);
 }
Exemple #3
0
 /// <summary>
 /// Add Connection driver to use DCom connection
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="computerName">Computer name or IP address to connect</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <returns>WmiContextOptionsBuilder (itself)</returns>
 public static WmiContextOptionsBuilder UseDCom(
     this WmiContextOptionsBuilder optionsBuilder,
     string computerName = ".", string nameSpace = @"root\CIMv2")
 {
     optionsBuilder.Options.Connection = new DComConnection(computerName, nameSpace);
     return(optionsBuilder);
 }
Exemple #4
0
 /// <summary>
 /// Add Connection driver to use DCom connection
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="connection">Preconfigured <c>ManagementScope</c> object</param>
 /// <returns>WmiContextOptionsBuilder (itself)</returns>
 public static WmiContextOptionsBuilder UseDCom(
     this WmiContextOptionsBuilder optionsBuilder,
     ManagementScope connection)
 {
     optionsBuilder.Options.Connection = new DComConnection(connection);
     return(optionsBuilder);
 }
 /// <summary>
 /// Add Connection driver to use Windows Remote management
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="computerName">Computer name or IP address to connect</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <param name="credential">Preconfigured <c>CimCredential</c> object</param>
 /// <returns>WmiContextOptionsBuilder (itself)</returns>
 public static WmiContextOptionsBuilder UseCim(
     this WmiContextOptionsBuilder optionsBuilder,
     string computerName, string nameSpace, CimCredential credential)
 {
     optionsBuilder.Options.Connection = new CimConnection(computerName, nameSpace, credential);
     return(optionsBuilder);
 }
 /// <summary>
 /// Add Connection driver to use Windows Remote management
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="connection">Preconfigured <C>CimSession</C> object</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <returns>WmiContextOptionsBuilder (itself)</returns>
 public static WmiContextOptionsBuilder UseCim(
     this WmiContextOptionsBuilder optionsBuilder,
     CimSession connection, string nameSpace = @"root\CIMv2")
 {
     optionsBuilder.Options.Connection = new CimConnection(connection, nameSpace);
     return(optionsBuilder);
 }
 /// <summary>
 /// Add Connection driver to use Windows Remote management
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="computerName">Computer name or IP address to connect</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <param name="authenticationMechanism">Authentication Mechanism</param>
 /// <param name="domain">User domain</param>
 /// <param name="userName">User name</param>
 /// <param name="password">User password</param>
 /// <returns></returns>
 public static WmiContextOptionsBuilder UseCim(
     this WmiContextOptionsBuilder optionsBuilder,
     string computerName, string nameSpace, PasswordAuthenticationMechanism authenticationMechanism,
     string domain, string userName, string password)
 {
     optionsBuilder.Options.Connection = new CimConnection(
         computerName, nameSpace, authenticationMechanism, domain, userName, password);
     return(optionsBuilder);
 }
Exemple #8
0
 /// <summary>
 /// Add Connection driver to use DCom connection
 /// </summary>
 /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param>
 /// <param name="computerName">Computer name or IP address to connect</param>
 /// <param name="nameSpace">WMI Namespace to work with</param>
 /// <param name="impersonationLevel">Impersonation level</param>
 /// <param name="domain">User domain</param>
 /// <param name="userName">User name</param>
 /// <param name="password">User password</param>
 /// <returns></returns>
 public static WmiContextOptionsBuilder UseDCom(
     this WmiContextOptionsBuilder optionsBuilder,
     string computerName, string nameSpace,
     string domain, string userName, string password,
     ImpersonationLevel impersonationLevel = ImpersonationLevel.Impersonate)
 {
     optionsBuilder.Options.Connection = new DComConnection(
         computerName, nameSpace, domain, userName, password, impersonationLevel);
     return(optionsBuilder);
 }