How to Connect to a System Center Data Access Service by Using the Current Credentials

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

An Operations Manager software development kit (SDK) client application can connect to the System Center Data Access service by using the credentials of the current user.

noteNote
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 System Center Data Access Service.

Example

The following example demonstrates how to connect to a System Center Data Access service by using the credentials of the current user. The example also shows how to handle some common connection failures.

/// <summary>
/// Connects to an SDK Service by using the current user's credentials.
/// </summary>
using System;
using System.ServiceProcess;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;


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 ManagementGroup object by using the new keyword, as shown in the following example:

mg = new ManagementGroup(serverName);

is the same as:

mg = ManagementGroup.Connect(serverName);

See Also