Executing STA COM Objects from ASP.NET Web Services

by Johnny Nouel 25. julio 2009 01:03

A new customer for one of my web applications requested to make a change.  They wanted to execute a STA COM object to retrieve some values from another system in order to keep this application updated.  Having done that in the past I said 'ok'.

I remember the old AspCompat attribute of the Page directive could help.  All I needed was set the attribute to True and execute the page.  Then I remembered that I needed to provide the same functionality from a web service that many windows applications were already accessing.

There's no AspCompat attribute for web services so I had an issue that needed to be fixed quick.  I did a quick web search for the problem at hand and found a very detailed explanation made by Jeff Prosise in his article here at the MSDN Magazine web site.  His explanation and solution are flawless so I suggest you go there and read all that.

To summarize, the thing is that ASPX web pages and ASMX Web Services run on the same HTTP Pipeline which ASP.NET allocates a thread to each request.  The ASP.NET threads are MTA and for web pages the AspCompat attribute forces the web page to execute its code in STA mode which does not throw back an error if you try to call an STA COM object.  You can also, as an alternative, use the tlbimp.exe utility to convert the STA COM Object to an assembly allowing you to make the calls from ASP.NET without errors.  Be warned that this affects performance.

The proposed solution is pretty smart and simple.  Write an HttpHandler that derives from System.Web.UI.Page to manage the execution of ASMX files and add True to the already available AspCompat attribute.

Let's see the code for out AspCompat wrapper:

    1 Imports System

    2 Imports System.Web

    3 Imports System.Web.UI

    4 Imports System.Web.Services.Protocols

    5 Imports System.Web.SessionState

    6 

    7 Public Class AspCompatWebServiceHandler

    8     Inherits System.Web.UI.Page

    9     Implements IHttpAsyncHandler

   10     Implements IRequiresSessionState

   11     Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)

   12         Dim handler As IHttpHandler = New WebServiceHandlerFactory().GetHandler(Me.Context, _

   13             Me.Context.Request.HttpMethod, Me.Context.Request.FilePath, _

   14             Me.Context.Request.PhysicalPath)

   15         handler.ProcessRequest(Me.Context)

   16         Me.Context.ApplicationInstance.CompleteRequest()

   17     End Sub

   18 

   19     Public Function BeginProcessRequest(ByVal context As HttpContext, ByVal cb As AsyncCallback, _

   20         ByVal extraData As Object) As IAsyncResult Implements IHttpAsyncHandler.BeginProcessRequest

   21         Return Me.AspCompatBeginProcessRequest(context, cb, extraData)

   22     End Function

   23 

   24     Public Sub EndProcessRequest(ByVal result As IAsyncResult) _

   25         Implements IHttpAsyncHandler.EndProcessRequest

   26             Me.AspCompatEndProcessRequest(result)

   27     End Sub

   28 End Class

 

With this class created I then went and added it as an HttpHandler for ASMX files as shown below (I removed some code for brevity).

    <system.webServer>

        <handlers>

            <add name="AspCompatWebServiceHandler" verb="*" path="*.asmx" type="MyAssembly.AspCompatWebServiceHandler" preCondition="integratedMode"/>

        </handlers>

    </system.webServer>

 

As you can see the solution is pretty smart and simple.  This allows the execution of STA COM Objects from ASMX Web Services.

Please be informed that the the registration of the Handler was done in a Web Application Project and not a Web Site project.  The solution in Jeff Prosise's post is for Web Site Projects and differs only a little.  Here it is:

<configuration>

    <system.web>

        <httpHandlers>

            <add verb="*" path="*.asmx" type="AspCompatWebServiceHandler, __code" />

        </httpHandlers>

    </system.web>

</configuration>

Happy programming! Wink

Tags: , , ,

ASP.NET

Agregar Comentario




  Country flag
biuquote
  • Comentario
  • Vista Previa
Loading


Recent Posts

Sobre mi

Johnny Nouel Mi nombre es Johnny Nouel. Profesional de Tecnología.  Experto en SAP FI CO.  Especialista en SAP BW, BI y BO.  Desarrollador ASP.NET.  Amante de los Videojuegos y la Tecnología.  Aguilucho!

Contactame?