using a COM in web application

  • Thread starter Thread starter Amitava Sengupta
  • Start date Start date
A

Amitava Sengupta

Is there any issue in using COM component in web applications. The
problem I'm facing are

1. Though the dll is using non-static member variables which are being
instantiated on each call, Whenever I'm calling the (.aspx) pages using
the dll more than once simultaneously, both results in garbage. When
called once it is perfect in result.
2. The memory is not being released in web application of the dll even
after finishing properly with result. Again it is perfect when using in
non-web application (say VB exe application). Actually, the dll is using
PowerPoint classes and opening a ppt file. And the PowerPoint is
releasing memory long after.
 
Hi,

are u releasing the COM object after using it by setting it to null (C#) or
nothing (VB). Because we cannot predict the behavior of the CLR GC, you must
explicitly de reference your heavy objects. I hope that would help.

Regards
Joyjit
 
Hi,

try Marshal.ReleaseComObject(object your comobject) if that works.

Regards
Joyjit
 
I just lived through this.

If there's any possible way to get around using ASP.NET to automate office,
then do it, Run screaming into the hells and never look back, chilld!!

Here's the thing. If you scour the microsoft site, they strongly recomment
that you don't do this. The COM objects dont end up geting released, even
if you tell them to through Marshall.ReleaseComObject. What happens is that
for every "thing" that you reference, you have to distroy the reference
explicitly with ReleaseComObject. For example, if you were using an excel
spreadsheet and all you wanted to do was open a workbook and change the
value of a single cell, you would have to create explicit variables for the
(here we go) Application, WordbooksCollection, ActiveSheet, ActiveSheetRange
and the Cell you're referring to. If you want to refer to 20 different
cells, then you have to create a reference to each cell explicitly and then
explicitly call ReleaseCOMObject on every one. Forget a single cell and
you're toast.

In the case of PowerPoint, you'd have to create a seperate variable for
every shape. presentation, slide and textbox you use. You can't reassign a
variable because when the reference is changed, you can't call
ReleaseCOMObject on it.

I ended up haveing to use Word and Excel for a project, under duress. As a
result, we had to resolve the issue with the following approach.

Only one instance of the application word can exist at any time
Only one instance of the application excel can exist at any time
Don't directly Create an instance of the applicationClass. Delegate that to
a worker function (e.g. GetWordInstance)
GetWordInstance performs the following actions:
Execute System.Diagnostics.Process.GetProcessesByName("MSWORD")
If no processes exist, then create a new Word.ApplicationClass and return
it
If a process does exist, wait 1/5 second and try again.
If a total of 20 seconds pass without being able to create a word
application then throw an exception

When you are finished with the Word Application, you have to call a
releaseWordInstance function which does the following
Execute System.Diagnostics.Process.GetProcessesByName("MSWORD")
For each Process taht is returned, kill it!

I hate that we had to do this, but it was the only soution that we could
come up with.
 
Thanks Joyjit for ur interest in the matter. But every thing to release
a COM object or Garbage collection was done. Actually I tried many (if
not all) possible ways to release. Had there be any problem, I think the
COM would not have been worked for non-web (say VB exe) application.
 
In addition to other's comments:

If the COM component is written in VB6, or the component has a
threading model of STA (single threaded apartment), then use
AspCompat="true" in your @ Page directive. This sets the ASPX page to
run in a compatible apartment and reduces some thread marshalling.
This would mostly be just for performance reasons, but can also change
the behavior of an application, if you are using impersonation for
instance.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top