Select to view content in your preferred language

open aprx in standalone C# application

1148
8
11-13-2024 09:55 AM
CarlosPiccirillo2
Emerging Contributor

Hi everyone,

I have converted an ArcObjects SDK .NET application written in C# for creating PDF maps to an ArcGIS Pro add-in using the ArcGIS Pro SDK. Everything runs fine from within ArcGIS Pro but now I want to convert it to a standalone complied exe as the original is so I can run it from Windows Task Scheduler.

From my research, I understand that await QueuedTask.Run is only needed inside of Pro so I have been slowly removing these one function at a time in the order that each function runs. 

Things work until I get to the function below which is supposed to open an aprx that I use as a template (has layout that I manipulate, layers loaded, etc.) and return a Project object which is used later in the code to retrieve a Map and a Layout.

public static Project GetProject()
{
try
{
string aprxFile = PATH_Aprx_template + "exhibit_map.aprx";
 
// Open the project
//Project project = Project.OpenAsync(aprxFile).Result;
//Project project = Project.OpenAsync(aprxFile).GetAwaiter().GetResult();
Project project = Project.OpenAsync(aprxFile);
 
// Check if the project is null (failed to open)
if (project == null)
{
Console.WriteLine("Failed to open the project.");
return null;
}
return project;
}
catch (Exception ex)
{
Console.WriteLine($"Error in GetProject: {ex.Message}");
return null;
}
}
 
The project refuses to compile in Visual Studio with an error of "cannot implicitly convert type System.Threading.Tasks.Task<ArcGIS.Desktop.Core.Project> to ArcGIS.Desktop.Core.Project at line
 
Project project = Project.OpenAsync(aprxFile);
 
As you can see, I tried a couple of other things that are commented out but those didn't work either. 
 
I am using ArcGIS Pro 3.3 and Visual Studio 2022.
 
Can someone help me out?
Thanks,
Carlos
Tags (1)
0 Kudos
8 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Project.OpenAsync returns Task<Project>. So, to assign result of Project.OpenAsync to Project variable, you need to use await before Project.OpenAsync. Your method could look like code below:

public static async Task<Project> GetProject()
{
try
{
string aprxFile = PATH_Aprx_template + "exhibit_map.aprx";
 
// Open the project
Project project = await Project.OpenAsync(aprxFile);
 
// Check if the project is null (failed to open)
if (project == null)
{
Console.WriteLine("Failed to open the project.");
return null;
}
return project;
}
catch (Exception ex)
{
Console.WriteLine($"Error in GetProject: {ex.Message}");
return null;
}
}

 

0 Kudos
CarlosPiccirillo2
Emerging Contributor

Thanks for your help, I really appreciate it.  I will try this on Monday and post back.

0 Kudos
RadekMandovec
Occasional Contributor

Hi,

are you sure this line:

Project project = await Project.OpenAsync(aprxFile);

will work? Carlos already used something similar :

Project project = Project.OpenAsync(aprxFile).Result;

 without succes. I know with Result it would block until the task is completed, but in his case it seems to me he needs to wait for the result anyway.

0 Kudos
CarlosPiccirillo2
Emerging Contributor

Hi guys, thanks for the replies. Unfortunately, the suggestions did not work. Below is the original function that works fine in my add-in but now that I am converting the code to a console standalone application, the await QueuedTask.Run no longer applies. So I guess I need a different way to get a reference to the project.

public static async Task<Project> GetProject()
{
try
{
string aprxFile = PATH_Aprx_template + "exhibit_map.aprx";
 
// Open the project
Project project = await Project.OpenAsync(aprxFile);
 
// Check if the project is null (failed to open)
if (project == null)
{
Console.WriteLine("Failed to open the project.");
return null;
}
return project;
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show(ex.Message, "GetProject");
Console.WriteLine($"Error in GetProject: {ex.Message}");
return null;
}
}

 

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Sorry @CarlosPiccirillo2 .

Project class belongs to ArcGIS.Desktop.Core workspace which is not accessible from CoreHost application. ArcGIS.CoreHost and ArcGIS.Core assemblies are available from Corehost application only. More information here.

0 Kudos
CarlosPiccirillo2
Emerging Contributor

Thanks for the clarification. Do you know have I can get a reference to a Project (aprx) using ArcGIS.CoreHost or ArcGIS.Core?

0 Kudos
GKmieliauskas
Esri Regular Contributor

You could try to call python script with your workflow from CoreHost application or execute python script directly. We have similar situation with geoprocessing in CoreHost application. There is no way to call geoprocessing tool directly, but you can call it from python.

0 Kudos
CarlosPiccirillo2
Emerging Contributor

The entire application is quite complex which is why I was trying to avoid going the Python route but It's starting to look like I will have to re-write the entire application in Python which I was hoping to avoid. Only have a year of experience in Python but over 15 years in C# which is why I ported over the old ArcMap (ArcObjects) code using C#. 

0 Kudos