<Fix for Unhandled exceptions while connecting to Dynamics 365 online>


Metadata Diagram, a console application comes with Dynamics 365 SDK is capable of creating entities ER diagram in Visio. You can find sample code and Readme instructions at below location:


By default, console creates ER diagram for all entities, but you can pass entity schema names as arguments to restrict it for fewer entities.

  • Right click on the MetadataDiagramConsole in the Solution Explorer and select Properties. Click on the Debug section of the properties dialog.  Enter: “opportunity account” into the Command Line Arguments textbox to have the application draw only the opportunity and account entities.

Connect to Dynamics 365 OL

While connecting to Dynamics 365 OL, you might face unhandled exceptions as below:

  • The server could not be contacted. LDAP server is unavailable
  • There was no endpoint listening
  • Request timeouts while retrieving metadata

You can fix this issue by simply modifying below red highlighted code in Main() method

                // Obtain the target organization’s Web address and client logon
                // credentials from the user.
                //ServerConnection serverConnect = new ServerConnection();
                //ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
                string strUrl = string.Empty;
                strUrl = “https://<orgname>.api.crm9.dynamics.com/XRMServices/2011/Organization.svc“;
                ClientCredentials credential = new ClientCredentials();
                credential.UserName.UserName = “*********.onmicrosoft.com”;
                credential.UserName.Password = “***************”;
                // Set security protocol to TLS 1.2 for version 9.0 of Customer Engagement Platform
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                // Connect to the Organization service.
  // The using statement assures that the service proxy will be properly   disposed.
using (_serviceProxy = new OrganizationServiceProxy(new Uri(strUrl), null, credential, null))
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.Timeout = TimeSpan.MaxValue;
                    // Load Visio and create a new document.
                    application = new VisioApi.Application();
application.Visible = false; // Not showing the UI increases       rendering speed
                    builder.VersionName = application.Version;
                    document = application.Documents.Add(String.Empty);
                    builder._application = application;
                    builder._document = document;

Note – You can define connection URL and credentials in app.config to make it configurable.