Error (Transaction outside of API context is not allowed) on IExternalEventHandler

MiguelGT17
Advocate
Advocate

Error (Transaction outside of API context is not allowed) on IExternalEventHandler

MiguelGT17
Advocate
Advocate

Hi everyone

I've implemented this IExternalEventHandler which was working pretty well for the last 3 months:

public class CSVExportHandler : IExternalEventHandler

public void Execute(UIApplication app)
switch (caseValue)
            {
                case caseHandler.docOpened:
                    docOpened(app);
                    break;
                case caseHandler.refreshData:
                    refreshData(app);
}

 private async void refreshData(UIApplication app)
        {
            UIDocument uidoc = app.ActiveUIDocument;
            //if(uidoc== null) return;
            Document doc = app.ActiveUIDocument.Document;
...

}



somehow it's throwing the error when implementing another transaction wrapped within the refreData() method. A hint to the issue is that it also appeared since I send a 2° request to an web API.



¿Any thoughts on this issue?

MiguelGT17_0-1699837826582.png

 

0 Likes
Reply
159 Views
1 Reply
Reply (1)

longt61
Advocate
Advocate

I believe that your asynchronous method is the root of the problem. As far as I know, Revit does not support asynchronous methods, and all of the transaction/ modification must happen within its API context, in the same thread. When you call the asynchronous method, the Document object has been passed to outside of the API context.

If you have no choice but to use asynchronous method, you should try to separate the async functionalities from the direct modification of Revit elements. Maybe you can create an external .exe file then start a new process and wait for the process to stop., or you can export data to external files.

Hope this helps.