An Operations Manager SDK client application can connect to the SDK Service by using the credentials of the current user.

Note
The current user's account must be included in an Operations Manager user-role profile. For more information, see How to Connect an Operations Manager SDK Client to the SDK Service

Example

The following example demonstrates how to connect to an SDK Service by using the credentials of the current user. The example also shows how to handle some common connection failures.

Visual Basic  Copy Code
' Connects to an SDK Service by using the current user's credentials.
Imports System
Imports System.Collections.Generic
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.VisualBasic
Imports System.ServiceProcess
Imports System.Text

Namespace SDKSamples
	Class Program

		Public Overloads Shared Function Main(ByVal args() As String) As Integer
			Console.WriteLine("Attempting to connect to the SDK Service by using the current user's credentials.")

			Try
				Dim mg As ManagementGroup = ManagementGroup.Connect("localhost")
				If mg.IsConnected Then
					Console.WriteLine("Connection succeeded.")
				Else
					Throw New InvalidOperationException("Not connected to an SDK Service.")
				End If
			Catch sde As ServerDisconnectedException
				Console.WriteLine(ControlChars.Lf + "Connection failed. " & sde.Message)
				If Not (sde.InnerException Is Nothing) Then
					Console.WriteLine(sde.InnerException.Message)
				End If
				' Call custom method to prompt the user for credentials if needed.
			Catch snr As ServiceNotRunningException
				Console.WriteLine(snr.Message)

				' Make one attempt to start the service.
				Dim sc = New ServiceController("OMSDK", "localhost")

				Console.WriteLine("Attempting to start the SDK Service on localhost.")
				sc.Start()

				' Wait 20 seconds for it to enter the Running state.
				sc.WaitForStatus(ServiceControllerStatus.Running, New TimeSpan(0, 0, 20))

				If sc.Status = ServiceControllerStatus.Running Then
					Dim mg As New ManagementGroup("localhost")
					Console.WriteLine("Connection succeeded.")
				Else
					Throw New InvalidOperationException("Unable to restart and connect to the SDK Service.")
				End If
			End Try
		End Function 'Main
	End Class 'Program
End Namespace 'SDKSamples
C#  Copy Code
/// <summary>
/// Connects to an SDK Service by using the current user's credentials.
/// </summary>
using System;
using System.Collections.Generic;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using System.ServiceProcess;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Attempting to connect to the SDK Service by using the current user's credentials.");

			try
			{
				ManagementGroup mg = ManagementGroup.Connect("localhost");
				if (mg.IsConnected)
					Console.WriteLine("Connection succeeded.");
				else
					throw new InvalidOperationException("Not connected to an SDK Service.");
		}
			catch (ServerDisconnectedException sde)
			{
				Console.WriteLine("\nConnection failed. " + sde.Message);
				if (sde.InnerException != null)
					Console.WriteLine(sde.InnerException.Message);
			
				// Call custom method to prompt the user for credentials if needed.
			
		}
			catch (ServiceNotRunningException snr)
			{
				Console.WriteLine(snr.Message);

				// Make one attempt to start the service.
				System.ServiceProcess.ServiceController sc = new 
					ServiceController("MOMSDK", "localhost");

				Console.WriteLine("Attempting to start the SDK Service on " + "localhost" + ".");
				sc.Start();

				// Wait 20 seconds for it to enter the Running state.
				sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20));

				if (sc.Status == ServiceControllerStatus.Running)
				{
					ManagementGroup mg = new ManagementGroup("localhost");
					Console.WriteLine("Connection succeeded.");
			}
				else
				{
					throw new InvalidOperationException("Unable to restart and connect to the SDK Service.");
			}
		}
	}
}
}

Instead of explicitly calling the Connect method, you can also instantiate a new Microsoft.EnterpriseManagement.ManagementGroup object by using the new keyword, as shown in the following example.

C#  Copy Code
mg = new ManagementGroup(serverName);

Is the same as:

C#  Copy Code
mg = ManagementGroup.Connect(serverName);

See Also


Send comments about this topic to Microsoft.