Skip to content

An external event created outside an add-in callback runs its handler with no active add-in #134

Closed

Description

@Nice3point

A Toolkit external event runs its handler as the add-in that uses it, wherever the event is first created.

ExternalEventHandler creates the Revit ExternalEvent inside RevitContext.BeginApiContextScope(), which opens the call depth scope and no add-in data scope.
Revit records the active add-in when ExternalEvent.Create runs, and runs the handler as that add-in.
Outside an add-in callback, such as in a WPF event handler of a modeless window, in a dispatcher callback, or on a thread-pool thread, no add-in is active.
A generated [ExternalEvent] property creates its event on the first access, so the first caller decides the add-in of every later raise.

Steps to reproduce

  1. Create a schema with SetVendorId("Contoso"), public read access, and vendor write access.
  2. In a session whose add-in has the vendor Contoso, create an AsyncRequestExternalEvent<string> whose handler reads UIApplication.ActiveAddInId and writes an entity of the schema to a new document.
  3. Create the event in four places and raise it.

✔️ Expected Behavior

The handler runs as the add-in in every case, and the write succeeds.

❌ Actual Behavior

The event is created Raised from ActiveAddInId in the handler Write
Inside an add-in callback on the Revit thread The same callback The add-in Written
Inside an add-in callback on the Revit thread A thread-pool thread The add-in Written
On a thread-pool thread The same thread null InvalidOperationException: "Writing of Entities of this Schema is not allowed to the current add-in."
In a Dispatcher.InvokeAsync callback on the Revit thread The same callback null The same exception

Environment

Revit 2025, Nice3point.Revit.Toolkit 2025.2.3, Nice3point.TUnit.Revit 2025.1.8 with RevitClient(VendorId = "Contoso"), a RevitApiUiTest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛An unexpected issue that highlights incorrect behaviorcontext 🧭Revit API context, dialog and failure suppressionexternal commands 🚪ExternalCommand, ExternalApplication, and external events

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions