Using an ActiveX in a Web service

Discussion in 'ASP .Net' started by Oriane, Jul 24, 2008.

  1. Oriane

    Oriane Guest

    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
    Oriane, Jul 24, 2008
    #1
    1. Advertising

  2. Oriane

    Munna Guest

    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
    Munna, Jul 24, 2008
    #2
    1. Advertising

  3. 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:
    .

    ==================================================
    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" <>
    >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
    >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
    >
    >
    Steven Cheng [MSFT], Jul 25, 2008
    #3
  4. Oriane

    Oriane Guest

    Hi Steven,
    "Steven Cheng [MSFT]" <> a écrit dans le message
    de news:...
    > 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
    Oriane, Jul 28, 2008
    #4
  5. 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:
    .

    ==================================================
    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" <>
    >References: <>

    <>
    >In-Reply-To: <>
    >Subject: Re: Using an ActiveX in a Web service
    >Date: Mon, 28 Jul 2008 08:59:49 +0200


    >
    >Hi Steven,
    >"Steven Cheng [MSFT]" <> a écrit dans le

    message
    >de news:...
    >> 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
    >
    >
    Steven Cheng [MSFT], Jul 29, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page