Debugging problems for Pro SDK in VS

2771
15
10-31-2016 03:48 AM
CollinKleiboer
New Contributor III


It seems I can't debug an ArcGIS Pro Addin in VS.
Every time it struggles and gives me a System.AggregateException.
It seems like the map is not being loaded well.
I've tried to do some changes in the firewall, but no results.
If I run the project directly, no problems. It happens only if I run it via debug in VS.
Any ideas?

"An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll
Additional information: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
"

0 Kudos
15 Replies
CharlesMacleod
Esri Regular Contributor

Sounds like a timing issue. That is, running on the VM is changing the sequence of execution in the Add-in. From the exception you are making an Async call that you are not awaiting. That async call is failing when you are running on the VM (but I have no idea why). If you use the debugger and look at the inner exceptions property it may give you the actual exception that faulted the task.  That said, if you do "await" the async call you can bracket it with a standard "try { } catch" block to trap the exception.

I will need to see your code to assist further.

0 Kudos
CollinKleiboer
New Contributor III

Hello Charles,

Thank you.

It looks like it has to do with the use of the MapView in the code.

I created a very simple test AddIn with only 1 button. After commenting the bold lines below, it works like it should.

The initial problem remains: the MapView is not loading well when debugging (white map), TOC loads only after focussing.

using System;
using ArcGIS.Desktop.Framework.Contracts;
//using ArcGIS.Desktop.Mapping;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;

namespace effe
{
internal class Button1 : Button
{
protected override void OnClick()
{
//testen
try
{
QueuedTask.Run(() =>
{
string test = "{ \"xmin\" : 1, \"ymin\" : 2,\"xmax\":3,\"ymax\":4,\"spatialReference\":{\"wkid\":4326}}";
Geometry geom = GeometryEngine.ImportFromJSON(JSONImportFlags.jsonImportDefaults, test);
//IDisposable x = MapView.Active.AddOverlay(geom);
MessageBox.Show("toegevoegd: " + geom.ToString() + " json: " + geom.ToJson());
}
);
} catch (Exception ex)
{
MessageBox.Show("FOUTJE: " + ex.ToString());
}


}
}
}

0 Kudos
CharlesMacleod
Esri Regular Contributor

I cannot reproduce your error. I opened a new project with the World Topo base map. Can you change your code as follow please: (this will allow you to catch the exception): 

protected async override void OnClick() {

     try {

       await QueuedTask.Run....

Also, is there a base map currently drawn in your project or a blank map display? Is the map 2D or 3D?

CollinKleiboer
New Contributor III

I tried with the base map and also with only a local featureclass (both 2D). But the results are the same.

See my response to Wolfgang below...

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Collin,

 I enhanced your 'simple button' add-in a bit using the code snippet below and was able to debug and run the add-in without problems.  I think the main issue was that the IDisposable object returned by AddOnOverlay has to be maintained since the added overlay is removed from the mapview when that object is disposed.  The code below uses the button click to add a polygon to the overlay and then removes the same polygon on the next button click.  I hope this snippet will work for you.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace ProAppModule2
{
    internal class Button1 : Button
    {
        private IList<IDisposable> _addOns = new List<IDisposable>();
        protected override async void OnClick()
        {
            //testen
            try
            {
                // clear any geometries on overlay
                if (_addOns.Count > 0)
                {
                    foreach (var addOn in _addOns) addOn.Dispose();
                    _addOns.Clear();
                    MessageBox.Show("Overlay clear");
                }
                else
                {
                    var symbol = OverlaySymbol;
                    await QueuedTask.Run(() =>
                    {
                        Envelope env = Geom.Result;
                        Polygon poly = PolygonBuilder.CreatePolygon(env);
                        _addOns.Add(MapView.Active.AddOverlay(poly, symbol.Result));
                        MessageBox.Show("toegevoegd: " + Geom + " json: " + env.ToJson() + env.GeometryType);
                        MapView.Active.ZoomTo(env);
                    });
                    MessageBox.Show("zoomed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("FOUTJE: " + ex.ToString());
            }
        }

        private Task<Envelope> Geom
        {
            get
            {
                return QueuedTask.Run(() =>
                {
                    var test =
                        "{ \"xmin\" : 1, \"ymin\" : 2,\"xmax\":3,\"ymax\":4,\"spatialReference\":{\"wkid\":4326}}";
                    return GeometryEngine.ImportFromJSON(JSONImportFlags.jsonImportDefaults, test) as Envelope;
                });
            }
        }

        private Task<CIMSymbolReference> OverlaySymbol
        {
            get
            {
                return QueuedTask.Run(() =>
                {
                    CIMStroke outline = SymbolFactory.ConstructStroke(ColorFactory.BlackRGB, 2.0, SimpleLineStyle.Solid);
                    CIMPolygonSymbol fillWithOutline = SymbolFactory.ConstructPolygonSymbol(ColorFactory.RedRGB,
                        SimpleFillStyle.Solid, outline);
                    return fillWithOutline.MakeSymbolReference();
                });
            }
        }
    }
}
CollinKleiboer
New Contributor III

Hello Wolfgang and Charles,

Thanks for responding.

Both your code work, but only if I create a new project during the debugging.

If I want to start debugging with a recent project, it fails to load the mapview.

The problem (in my opinion) is not in the code, but in my environment/settings.

Occasionally I was able to see some exception message:

System.InvalidOperationException: The map viewer is not initialized.
at ArcGIS.Desktop.Mapping.MapView.ValidateCanCallMapViewer(Boolean throwEx)
at ArcGIS.Desktop.Mapping.MapView.ZoomTo(Geometry geometry, Nullable`1 duration, Boolean maintainViewDirection)
at effe2.Button2.<>c__DisplayClass1_0.<OnClick>b__0()....

Are there known issues with a not initializing mapviewer (in debug mode) on a saved ArcGIS Pro project?

Maybe in combination with particular debug settings or other VS extensions (like WebEssentials)?

Best regards,

Collin Kleiboer

0 Kudos
YuanLiu
Occasional Contributor

I just tested it on latest Pro 1.4 but was not able to reproduce it, new Pro project or existing ones. And a quick search does not show any similar issue/bug reported before either. You probably already did it... but could you try to create a new Pro add-in project and see if it still happens? It would help us narrow down the issue to either the project or your environment. 

0 Kudos
CollinKleiboer
New Contributor III

Hi Yuan,

I removed all AddIn's, created a new (Pro) project/solution and added a button. Started a debug-session. Same result. Map starts loading (the 3 vertical lines), then suddenly stops. I get the System.AggregateException on a certain moment again.

Running tasks:

Not Flagged 5808 Awaiting Async: <Initialize>d__0

Not Flagged 5807 Blocked System.Threading.WaitHandle.InternalWaitOne Task: <WaitForViewState>b__2c

 

Regards,

Collin Kleiboer.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I it tried on Win 10 - Pro - anniversary edition - VS 2015 - update 3 and both Pro 1.3 (release) and 1.4 (7008).  The sample code from above works fine (I had to change the namespace to match my newly created add-in's namespace).  Not sure what versions Collin is using.

0 Kudos