winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ServerConnection.cs (1265B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ServerConnection.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace WinGetMCPServer
      8 {
      9     using Microsoft.Management.Deployment;
     10 
     11     /// <summary>
     12     /// Maintains the connection to the COM server.
     13     /// </summary>
     14     internal static class ServerConnection
     15     {
     16         private static PackageManager? packageManager = null;
     17 
     18         public static PackageManager Instance
     19         {
     20             get
     21             {
     22                 if (packageManager == null)
     23                 {
     24                     packageManager = new PackageManager();
     25                 }
     26 
     27                 try
     28                 {
     29                     // Perform the simplest available call to check if the COM server is still active.
     30                     _ = packageManager.Version;
     31                 }
     32                 catch
     33                 {
     34                     packageManager = new PackageManager();
     35                 }
     36 
     37                 return packageManager;
     38             }
     39         }
     40     }
     41 }