Select to view content in your preferred language

Error creating a map in a core host Application

505
2
03-14-2022 05:47 AM
AndreasRuloffs2
New Contributor II

Hi everybody,

I have to write an console application, that creates an lyrx-File of a wms-service.

I should be able to create a Layer using the Layer Factory, but it needs a map.

So I try to Create an empty Map first.

When I do, I get an Exception:

System.NullReferenceException
HResult = 0x80004003
Nachricht = Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
Quelle = ArcGIS.Desktop.Mapping
Stapelüberwachung:
at ArcGIS.Desktop.Mapping.MapFactory.<>c__DisplayClass5_0.<CreateMap>b__0()
at ArcGIS.Desktop.Internal.Mapping.Utilities.MakeBasicServiceCall[T](Func`1 serviceCall)
at ArcGIS.Desktop.Mapping.MapFactory.CreateMap(String name, MapType mapType, MapViewingMode defaultViewingMode, Basemap basemap)

Unfortunately I have no idea what my error is.

Thank you, for reading

Andreas Ruloffs

 

using ArcGIS.Core.CIM;
using ArcGIS.Core.Hosting;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ProCoreHost3
{
    class Program
    {
        //[STAThread] must be present on the Application entry point
        [STAThread]
        static void Main(string[] args)
        {

            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveProAssemblyPath);
            if (args.Length != 3)
            {
                Console.Out.WriteLine("usage: CreateWMSLyr <wmsurl> <visibleLayer seperated by commas> <outputfile name");
                Environment.Exit(1);
            }
            String wmsURL = args[0];
            String visibleLayerStr = args[1];
            String outFile = args[2];

            String[] visibleLayer = visibleLayerStr.Split(new char[] { ',' });

            MainAsync(wmsURL, visibleLayer, outFile).GetAwaiter().GetResult();
        }

        static async Task MainAsync(String wmsURL, String[] visibleLayer, String outFile)
        {
            //Call Host.Initialize before constructing any objects from ArcGIS.Core
            try
            {
                Host.Initialize();
            }
            catch (Exception e)
            {
                // Error (missing installation, no license, 64 bit mismatch, etc.)
                Console.WriteLine(string.Format("Initialization failed: {0}", e.Message));
                return;
            }

            Map map = MapFactory.Instance.CreateMap("test", MapType.Map, MapViewingMode.Map, Basemap.None);

            CIMInternetServerConnection serverConnection = new CIMInternetServerConnection
            {
                URL = wmsURL
            };
            CIMWMSServiceConnection connection = new CIMWMSServiceConnection { ServerConnection = serverConnection };

            Layer layer = LayerFactory.Instance.CreateLayer(connection, MapView.Active.Map);
            LayerDocument layerdocument = new LayerDocument(layer);

            layerdocument.Save(outFile);
        }
        static Assembly ResolveProAssemblyPath(object sender, ResolveEventArgs args)
        {
            string folderPath = @"C:\Program Files\ArcGIS\Pro\bin"; //Get this from registry
            string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
            if (!File.Exists(assemblyPath)) return null;
            Assembly assembly = Assembly.LoadFrom(assemblyPath);
            return assembly;
        }
    }
}
0 Kudos
2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Corehost supports access to GDB and Geometry only. CoreHost application cannot use many of the constructs provided by the ArcGIS.Desktop dlls

Aashis
by Esri Contributor
Esri Contributor

In addition to @GKmieliauskas , attempting to access any of the ArcGIS Pro UI elements or types in other assemblies in an ArcEngine-like fashion will crash the program.

Details: Corehost Conceptual doc

0 Kudos