Using an ActiveX in a Web service

O

Oriane

Hi there,

I need to use an ActiveX inside a Web service. My problem is that I need an
handle on this Com component, so I add a reference to my .Net project, and I
create an instance of class of this Com exe. But I then realize that each
time I call for a method of my web service, I create a new Com exe process,
which is not what I want.

Any idea to avoid this pitfall ?

Philippe
 
M

Munna

HI,

I don't know much of com... but still want to share the thought
can't you load the componet in application start in global.ascx
and the share the same component in all the request...

regards

Munna
 
S

Steven Cheng [MSFT]

Hi Philippe ,

For your scenario, since you're calling an out-of-process COM object, it
will launch new COM process for new created object. To avoid this, I agree
with Munna that you can use a global variable(such as a static class
member) to cache a single instance of the COM component instance(something
like a singleton wrapper class) and all your webservice methods will use
that one. There is several things you need to take care here:

1. The COM component might not be thread safe, so for such multi-threading
environment, you will need to lock it when accessing it in each thread.

2. If #1 will make your performance impacted, consider creating multiple
such global instances as a pool so that you can always reuse these pooled
instances.

3. for .NET webservice/web application environment, the thread is MTA
thread while most COM objects are STA, this will make an additional calling
gap between them. If the COM is really an STA one, you may need to adjust
your webservice thread's COM apartment model, see the following article:

#Running ASMX Web Services on STA Threads
http://msdn.microsoft.com/en-us/magazine/cc163544.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Oriane" <[email protected]>
Subject: Using an ActiveX in a Web service
Date: Thu, 24 Jul 2008 18:26:08 +0200
Hi there,

I need to use an ActiveX inside a Web service. My problem is that I need an
handle on this Com component, so I add a reference to my .Net project, and
I
 
O

Oriane

Hi Steven,
Steven Cheng said:
Hi Philippe ,

For your scenario, since you're calling an out-of-process COM object, it
will launch new COM process for new created object. To avoid this, I
agree
with Munna that you can use a global variable(such as a static class
member) to cache a single instance of the COM component instance(something
like a singleton wrapper class) and all your webservice methods will use
that one. There is several things you need to take care here:

1. The COM component might not be thread safe, so for such multi-threading
environment, you will need to lock it when accessing it in each thread.
I need to check...
2. If #1 will make your performance impacted, consider creating multiple
such global instances as a pool so that you can always reuse these pooled
instances.
Good point.
3. for .NET webservice/web application environment, the thread is MTA
thread while most COM objects are STA, this will make an additional
calling
gap between them. If the COM is really an STA one, you may need to adjust
your webservice thread's COM apartment model,[...]
Up to now I've got no problem.

So I 've added the a Global.asax file and I create the COM component in the
Application_Start as a static variable. I've also added these lines on the
Application_End method:

protected void Application_End(object sender, EventArgs e)
{
// Destroy any reference
System.Runtime.InteropServices.Marshal.ReleaseComObject(StibilCtl);
StibilCtl = null;
}

and I think that you and Muna (which I thank also here) are absolutely
right, since now I've got only one COM object created (perhaps I will need a
pool later as yous suggest). I also have to check if I need to lock the
accesses to this object.

Thanks again

Oriane
 
S

Steven Cheng [MSFT]

Thanks for your followup Oriane,

Sure, if you have any further questions on this later, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
In-Reply-To: <[email protected]>
Subject: Re: Using an ActiveX in a Web service
Date: Mon, 28 Jul 2008 08:59:49 +0200
Hi Steven,
"Steven Cheng [MSFT]" <[email protected]> a écrit dans le message
de news:[email protected]...
Hi Philippe ,

For your scenario, since you're calling an out-of-process COM object, it
will launch new COM process for new created object. To avoid this, I
agree
with Munna that you can use a global variable(such as a static class
member) to cache a single instance of the COM component instance(something
like a singleton wrapper class) and all your webservice methods will use
that one. There is several things you need to take care here:

1. The COM component might not be thread safe, so for such multi-threading
environment, you will need to lock it when accessing it in each thread.
I need to check...
2. If #1 will make your performance impacted, consider creating multiple
such global instances as a pool so that you can always reuse these pooled
instances.
Good point.
3. for .NET webservice/web application environment, the thread is MTA
thread while most COM objects are STA, this will make an additional
calling
gap between them. If the COM is really an STA one, you may need to adjust
your webservice thread's COM apartment model,[...]
Up to now I've got no problem.

So I 've added the a Global.asax file and I create the COM component in the
Application_Start as a static variable. I've also added these lines on the
Application_End method:

protected void Application_End(object sender, EventArgs e)
{
// Destroy any reference
System.Runtime.InteropServices.Marshal.ReleaseComObject(StibilCtl);
StibilCtl = null;
}

and I think that you and Muna (which I thank also here) are absolutely
right, since now I've got only one COM object created (perhaps I will need a
pool later as yous suggest). I also have to check if I need to lock the
accesses to this object.

Thanks again

Oriane
 

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

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top