Having trouble passing variables to basic C# console app

I am trying to pass an ip, username and password in a simple test app that use the vmware api:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VMware.Vim;

namespace vmwaretest
{
    class Program
    {
        static void Main(string[] args)
        {
            VimClient c = new VimClient();
            ServiceContent sc = c.Connect("server01.dom1");
            UserSession us = c.Login("xxxxxx", "xxxxxxx");

            IList<VMware.Vim.EntityViewBase> vms = c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null);
            foreach (VMware.Vim.EntityViewBase tmp in vms)
            {
                VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp;
                Console.WriteLine((bool)(vm.Guest.GuestState.Equals("running") ? true : false));
                Console.WriteLine(new Uri(vm.Guest.IpAddress != null ? vm.Guest.IpAddress : "0.0.0.0"));
                Console.WriteLine((string)vm.Client.ServiceUrl);
                Console.WriteLine(vm.Guest.HostName != null ? (string)vm.Guest.HostName : "");
                Console.WriteLine("----------------");
            }
        }
    }
}

This fails with an error: ServiceContent sc = c.Connect("server01.dom1"); in VS2013

Here is the full exception detail:
System.UriFormatException was unhandled
  HResult=-2146233033
  Message=Invalid URI: The format of the URI could not be determined.
  Source=System
  StackTrace:
       at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Uri..ctor(String uriString)
       at VMware.Vim.VersionUtils.GetVIServerSupportedVersions(String serviceUrl)
       at VMware.Vim.VersionUtils.GetVIServerVersion(String serviceUrl)
       at VMware.Vim.VimClient.Connect()
       at VMware.Vim.VimClient.Connect(String serviceUrl)
       at vmwaretest.Program.Main(String[] args) in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\vmwaretest\vmwaretest\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Changing the server name to "http://server01.dom1/sdk"; does not work either.

Anyone have any ideas as to what I am doing wrong?

Comments

  • Never mind on the connection string. That does work with hard coding the variables in the connection method. I needed to add console.read(); for the last line of the main to see the program run which helped me figure out the problem. It would be nice to know how to use an encrypted session file to keep from hard coding these credentials in to the program however.
  • You can also use console. Readline(). And that should hold the output steady as well. Also make sure to use try and catch block if you are using threads. It'd be better that way especially if you have some network and the io related code. Most of the time it helps manage the variables this way. Glad it worked out for you.
Sign In or Register to comment.