In my project, I need to implement user registration through WPF which runs in Revit. When trying to send a query or get data from MSSQL, a critical error occurs and Revit closes. At the same time, the same code that works with MSSQL works flawlessly in applications that are not related to Revit in any way. Is there a way how to solve this problem?
Without a sample code is kinda hard to know exactly the problem. But most of the time if an Exception happens in a modeless WPF and is not resolved Revit crashes and closes.
One thing, you could do to test is create a IExternalCommand and run your MSSQL code in there, probably gonna work.
In the WPF use a try/catch to know what is happening, and remember Revit only allows access Revit method/class inside a Revit context (Same thread and safe place inside that thread).
Here is an example of the code
Revit API only works with NET Framework.
This DbContext what package is come from?
The database is connected using the Entity framework.
1. Here, a connection to the database is made and data is added to it from the User class, which becomes a database table.
2. Here the address of the connection to the database is written
3.This is a class with fields
4. On the click of the button located on the WPF form, we create an instance of the User class, write it down and send it to the database.
As mentioned above, this code works properly outside of Revit, but in Revit it stops working and a critical error occurs
You may find more information if you try handling corrupted state exceptions or first chance exceptions.
At the moment what you provided isn't much to go on. The majority of times this difference in behaviour is due to where add-in code base is vs standalone application. Revit add-in starts looking where the Revit executable is kept a standalone app will be starting to look where your assembly file is kept (likely where your referenced ones are also). Ordinarily if this is the issue you should only end up with a file not found exception/type load exception. However if one issue compounds/masks another you may not see that, you need to get more information.
You could even go as far as to p/invoke GetLastError it will give you an integer to lookup but again the timing is important i.e. when to call it: just after the error that causes it, just after the line that causes it. Also look in the Revit journal for clues.
Can't find what you're looking for? Ask the community or share your knowledge.