Why is MdiActiveDocument null in the beginning in AutoCAD 2015+? - autocad

I am working in AutoCAD 2014 using Visual Studio 2013.
With my code I access the MdiActiveDocument's database from the DocumentManager .
Using the database I start a transaction and use the GetObject method of the transaction to retrieve Entity objects.
Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
var obj = acTrans.GetObject(id, OpenMode.ForRead);
if (obj is Entity)
{
// do stuff
}
acTrans.Commit();
}
This works fine while I am in development and start AutoCAD from inside of Visual Studio. In development I set the "Start external program" switch in the Debug tab of the application properties so it starts AutoCAD for me and everything works great.
The issue I am having is that in production when the app is loaded by AutoCAD via registry settings (I use demand loading) the MdiActiveDocument is null so the code crashes. I have discovered there is a document in the Application.DocumentManager but when I assign the database from that document to acCurDb the TransactionManager throws an error with I try to use the StartTransaction method.
if (Application.DocumentManager.Count > 0)
{
foreach(Document doc in Application.DocumentManager)
{
acCurDb = doc.Database;
break;
}
}
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
}
Can someone help me understand why the MdiActiveDocument is null and/or direct me to the proper way to get a Transaction object in AutoCAD?

Beginning in 2015, AutoCAD may have a null active document on startup depending on user system variables. It's just another check you have to add before running your routine.

As mentioned by #david-wolfe, AutoCAD 2015 may start with no active document (just a dashboard). In this case the MdiActiveDocument can be null.
Now you're on AutoCAD 2014, so a different scenario may happen: if your app is loading with AutoCAD, you code may run before anything is really ready. How are you running the code? Is it a CommandMethod? If is a command, the user can only run it from a command, so it will be a active document. But if you run it from other method (like direct call from the Ribbon or from a palette), it may be null.

Related

MikroORM repository throwing Global Context error when debugging in Visual Studio Code

I am trying to use MikroORM repositories, but I keep getting the Global Context validation error when I use them while debugging in Visual Studio Code. I am using the RequestContext middleware, just as the documentation and demo applications suggest. The error does not occur when I run the application in a terminal window instead of by debugging.
I am currently running Visual Studio Code version 1.68.1 on Windows 10, using Node version 18.2.0.
Error Recreation:
Install the sample application Express + MongoDB + JavaScript from the Mikro-orm Example integrations (https://github.com/mikro-orm/express-js-example-app).
Follow the instructions in the README.md to run the application.
Use Postman (or equivalent) to create an author.
3.1. POST http://localhost:3000/author\
3.2. Body (raw/JSON):
{
"name": "test person",
"email": "test#test.com",
"age": 42
}
Use Postman (or equivalent) to retrieve authors.
4.1. GET http://localhost:3000/author
Copy the id of the author you just created.
Use Postman (or equivalent) to update the author.
6.1. PUT http://localhost:3000/author/<id from step 5.>
6.2. Body (raw/JSON):
{
"name": "test 'success' person"
}
NOTE: This will succeed, proving that everything is configured correctly. I have tried this with both the repository's version of Mikro-orm as well as the current latest version (5.2.3).
Stop the application (Ctrl-C in terminal)
Attach the debugger
8.1. Open the file app\server.js
8.2. Open VS Code's Run and Debug panel.
8.3. Click the "Run and Debug" button.
Use Postman (or equivalent) to update the author.
9.1. PUT http://localhost:3000/author/<id from step 5.>
9.2. Body (raw/JSON):
{
"name": "test 'failure' person"
}
This time the update will fail with the message:
"Using global EntityManager instance methods for context specific
actions is disallowed. If you need to work with the global instance's
identity map, use allowGlobalContext configuration option or
fork() instead."
Within the sample application, the error occurs in author.controller.js, within the router.put('/:id', async (req, res) section, specifically on the line await DI.authorRepository.flush() (line 55). However, experimentation suggests that the error will occur the second time you make any repository call. For example, if you duplicate the findOneOrFail line like this:
const author = await DI.authorRepository.findOneOrFail(req.params.id);
const foo = await DI.authorRepository.findOneOrFail(req.params.id);
the error will occur on the second call to .findOneOrFail.
The error is being thrown in #mikro-orm/core/EntityManager.js, in the getContext method (line 737). When the second call to the repository occurs, getContext is called multiple times. The first time has the following call stack
EntityRepository.flush
MongoEntityRepository.em (get)
EntityManager.getContext
This call is successful.
However, the second call to getContext fails. It has this call stack:
EntityRepository.flush
EntityManager.flush
EntityManager.getUnitOfWork
EntityManager.getContext
The issue seems to be that between the two calls to getContext, em._id goes from not 1 to 1. It looks to me like the EntityManager with an _id of 1 is the global context we shouldn't be using. However, when you run the code without debugging, em._id is not 1 during any of these calls.
I have not been able to figure out why the context is not retrieving the correct entity manager when in debugging mode.

Can you intercept or overwrite an IPreviewHandler of docx files?

In my previous investigation I discovered that even though windows file explorer launches word.exe in an embedded state the Documents collection remains empty.
Excel and word opened in Windows Explorer preview panel have Workbooks.Count and Documents.Count equal to 0
A bit further research when I made the embedded applications visible they would load with out a document inside of it:
public static class WordAppExt
{
public static Word.Document GetActiveDoc(this Word.Application App)
{
try
{
App.Visible = true;
if (App.Documents.Count > 0)
{
return App.ActiveDocument;
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
}
SO I came to the conclusion that explorer only needs to launch word, but it does not have the documents hosted inside of word. which means windows explorer is likely a IOleContainer. If true this means any object embedded in the preview pane can be accessed via EnumObjects.
Unfortunately my code runs in a VSTO addin, I don't have access to the explorer com objects so I can't do a QI for IOleContainer. But I have a theory that there maybe another way of accessing the Document COM object, through the PreviewHandler. I think that the reason Word, Powerpoint, and Excel get launched is either to register or initialization of the previewHandler?
So HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PreviewHandlers has a list of guids associated with IPreviewHandler for different file types.
There are plenty of articles about how to create your own previewHandler for new file types:
Building Preview Handlers
How to Register a Preview Handler
PreviewHandler.cs
Hosting Preview Handlers in Windows Forms Applications
Preview Handlers Revisited
http://www.codeproject.com/Articles/19744/Using-Preview-Handlers-in-Windows-Vista
https://code.msdn.microsoft.com/CppShellExtPreviewHandler-58db53b8
But I want to replace Words Preview Handler with my own. If I can't replace it I'd be more than happy to intercept the existing Preview Handler.

How to debug a plugin in on-line version?

I've created a plugin and registered it using hte registration tool. I've also added a step that is supposed to handle a message of creation of an instance. Sadly, the intended behavior doesn't occur.
My guess is that something inside the plugin crashes but I have no idea on how to debug it. Setting up breakpoints is not going to work agains on-line version, I understand, so I'm not even trying.
For legal and technical reasons, I won't be able to lift over the solution to an on-premise installation, neither. Is guessing my only option?
For server-side (plugins) I'm using ITracingService. For client-side I log everything to console. The downside with the first is that you actually need to crash the execution to get to see anything. The downside with the latter is that plugins sometimes get executed without GUI being invoked at all.
When it comes to heavier projects, I simply set up a WCF web service that I call from the plugin and write to that. That way, on one screen, I'm executing the plugin while on the other, I'm getting a nice log file (or just put the sent information to on the screen).
You could, for instance, start with a very basic update of a field on the instance of your entity that's being created. When you have that working, you can always fall back to the last working version. If you don't even get that to work, it mean, probably, that you're setting up the plugin registration incorrectly.
A very efficient way would be to lift over the solution to an on-premise version where you have full control but I see in your question that it's not en option.
In case you could lift the solution to an on-premise version, here's a link on how to debug plugins.
Don't forget that you also have access to the ITracingService.
You can get a reference to it in your Execute method and then write to it every so often in your code to log variables or courses of action that you are attempting or have succeeded with. You can also use it to surface more valuable information when an exception occurs.
It's basically like writing to a console. Then, if anything causes the plug-in to crash at runtime then you can see everything that you've traced when you click Download Log File on the error shown to the user.
Beware though - unless your plug-in actually throws an exception (deliberate or otherwise) then you have no access to whatever was traced.
Example:
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(
typeof(IPluginExecutionContext));
// Get a reference to the tracing service.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Getting entity from InputParameters...");
// may fail for some messages, since "Target" is not present
var myEntity = (Entity)context.InputParameters["Target"];
tracingService.Trace("Got entity OK");
// some other logic here...
}
catch (FaultException<OrganizationServiceFault> ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = (FaultException<OrganizationServiceFault>)ex.InnerException;
_trace.Trace(ex.ToString());
}
throw new InvalidPluginExecutionException(
string.Format("An error occurred in your plugin: {0}", ex));
}
catch (Exception ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = ex.InnerException;
_trace.Trace(ex.ToString());
}
throw;
}
}

TransactionScope does not rollback inside wcf service method, does roll back if called directly

I am facing a problem that drives me crazy for couple of days now, hoping someone can help me.
Here it is ;
I'm using EF4 with oracle database, using dotConnect for oracle from devart as provider.
I have wcf service method which calls DeleteCabinet method below;
public void DeleteCabinet(string pRID)
{
using(TransactionScope tranScope = new TransactionScope())
{
DBUtils.DeleteCabinetAndShelves(pRecordId);
//throw exception to test record not deleted
throw new Exception("xxx something has happened test xxx");
tranScope.Complete();
}
}
DBUtils.DeleteCabinetAndShelves looks like below;
public void DeleteCabinetAndShelves(string pRecordId)
{
using(var context = new EdrmEntities())
{
var cabinet = context.Cabinets.Include("Shelves").Single(p => p.RID == pCabinetRID);
//mark all cabinet shelves for deletion
if (cabinet.Shelves != null)
{
foreach (var tempShelf in cabinet.Shelves.ToList())
{
context.DeleteObject(tempShelf);
}
}
//mark cabinet for deletion
context.DeleteObject(cabinet);
//save
context.SaveChanges();
}
}
when I call DeleteCabinet from within my test project, not a wcf call but direct method call, it works OK. It throws exception ,and transaction is rolled back. Thus no record is deleted from DB as expected
The problem is that when I call service method (which calls DeleteCabinet) from a client, the exception is thrown , but the record IS deleted from db. Transaction does not roll back !
seems like calling wcf method does not roll back transaction, but it seems crazy ( at least to me), does anybody know the reason why this might be happening ?
Thanks in advance
You tagged your post with the DevArt and DotConnect tags... I wonder if this is a bug in the DevArt providers rather than something inherent to WCF / Entity Framework / System.Transactions. You could test the theory by seeing if it happens with a ObjectContext that is using the built-in SQL Server Provider (or even Oracle's own EF provider that was recently released) and see if the issue still occurs. That is the only thing I can think of since the code seems 100% correct.
Thanks to #Rabid and #luksans constructive comments problem is solved, and it turned out it has nothing to do with wcf or devart's provider being buggy
Here's the thing ; wcf service (which did not rollback), and integration test (which did) are inside different projects, thus config files are different. They got out of sync sometime in past, and the difference is in Enlist=false part. So wcf project's connection string has Enlist=false while test project does not. That's how the illusion of WCF failing transaction was born.
Removing Enlist=false from wcf project's connection string fixed the issue.

Exception when deleting message from Azure queue?

I'm dipping my toes into Windows Azure, and I'm running into something that has to be simple, but I just can't see it.
I have this small test to play with Azure queues:
public void CanPublishSillyLittleMessageOnQueue()
{
var queueClient = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudQueueClient();
var testQueue = queueClient.GetQueueReference("testqueue1");
testQueue.CreateIfNotExist();
var message = new CloudQueueMessage("This is a test");
testQueue.AddMessage(message);
CloudQueueMessage received;
int sleepCount = 0;
while((received = testQueue.GetMessage()) == null)
{
++sleepCount;
Thread.Sleep(25);
}
testQueue.DeleteMessage(received);
Assert.Equal(message.AsString, received.AsString);
}
It sends the message just fine - I can see it in the SQL table. However, when it hits the "testQueue.DeleteMessage(received)" method, I get this:
TestCase 'AzureExploratory.PlayingWithQueues.CanPublishSillyLittleMessageOnQueue'
failed: System.ArgumentNullException : Value cannot be null.
Parameter name: str
at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.ExecuteAndWait()
at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImplWithRetry(Func`1 impl, RetryPolicy policy)
at Microsoft.WindowsAzure.StorageClient.CloudQueue.DeleteMessage(CloudQueueMessage message)
PlayingWithQueues.cs(75,0): at AzureExploratory.PlayingWithQueues.CanPublishSillyLittleMessageOnQueue()
which appears to be a failure somewhere down inside the guts of the Azure SDK.
I'm using VS 2010, .NET 4.0, the Azure SDK V1.2, 64-bit Win 7. The developer store service is running; I can see the messages go into the queue, I just can't delete them.
Anyone ever seen anything like this?
I figured out what's going on. The code in question was running in a xUnit test harness. Turns out that the xUnit runner doesn't set up an appdomain with a config file path by default. System.UriBuilder now hits the config file, so it blows up.
The workaround was to add an empty app.config to the test project. Now it works.
ARGH!

Resources