Author Topic: Add In Exception  (Read 1239 times)

philchudley

  • EA User
  • **
  • Posts: 544
  • Karma: +9/-0
  • UML/EA Principal Consultant / Trainer
    • View Profile
Add In Exception
« on: July 08, 2008, 01:28:54 am »
Hi All

I am developing an addin in C#.Net to produce documentation using MS Word.

I have used the Sparx C# Add-in Framework, and Word launches just fine.

All compiles/builds/installs OK and all my Add-In menus are then upon opening a project.

The Add-in works fine for the first time, however, upon a subsequent invocation I receive a dialogue stating

RPC Server Unvailable

Any ideas?

Cheers
follow me on Twitter

@SparxEAGuru

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add In Exception
« Reply #1 on: July 08, 2008, 02:55:16 am »
Hi Phil,

I suspect - without proof - that you are not shutting Word down correctly. You might also be having a garbage collection problem.

In the former case make sure that you explicitly set all your Word objects to null after you've finished with them; this includes the Application object as well. Remember that Word (and other MS Office applications) are built around old-style COM technology, and must be handled correctly.

If that does not work take a look at the cautions in the EA help regarding add-ins (and automation projects in general). There are some old notes on making sure you correctly release all references to a project and its internal objects.

HTH, David
No, you can't have it!

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +0/-0
    • View Profile
Re: Add In Exception
« Reply #2 on: July 08, 2008, 04:17:59 am »
Phil,

to elaborate on David's answer, the sequence is this:

1. EA starts, parses the registry for add ins, and creates a COM class (i.e. the public class of your add in).
2. EA calls a couple of public methods in your class, giving you a COM reference to the current EA instance.
3. EA closes its main form and calls a method in your add in to tell you about EA closing.

Now when you start EA again, the previous EA process may still be running (though not visible as a application) and holding a reference to an instance of your add in class. This happens when you failed to release all references to EA.Repository. To really make sure that your class holds no reference to EA you even have to force garbage collection (EA help describes this).

It's a common problem of the .Net COM interop mechanism, and it can not only happen with EA but also with MS Word or any COM server. Maybe you can build in some exception handling code to find out which RPC server is unavailable (EA or Word).

Anyway, how do you start Word? Activator.CreateInstance? There's a couple of ways, I seem to recall, each with its pros and cons.

It's a pity we have to use COM interop, but then we're still better off than the java folks whose interop layer is much less comfortable.

Frank

philchudley

  • EA User
  • **
  • Posts: 544
  • Karma: +9/-0
  • UML/EA Principal Consultant / Trainer
    • View Profile
Re: Add In Exception
« Reply #3 on: July 09, 2008, 03:08:45 am »
Thanks guys! Yes the Word object was created as an attribute of a class, instantiated in a constructor, therefore not released until the Add-In shut down.

Silly mistake ... which I spotted not long after the post ...

All works fine now, next battle is with the Microsoft.Office.Interop.Word  ... yes I have Office 2007 and Visual Studio 2008

follow me on Twitter

@SparxEAGuru

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add In Exception
« Reply #4 on: July 09, 2008, 03:36:09 am »
Remain calm Phil,

That's the only advice I can offer. I've got the same environment and thus far I've been able to get things working fine.

One piece of advice though. Put your cleanup code in a set of Try-Catch constructs and get rid of the COM assets in the Finally blocks. Take no chances. Also remember that Word can exit rather rapidly on a Close event; by the time you try to do cleanup you won't be able to access some of the container objects - they are already sent to garbage collection, but they might not 'seem' to be set to null - so be very careful to clean up as you go.

David
No, you can't have it!