How are STA COM components handled when used in a WCF service hosted in IIS (7+)? -


from understand, when com component marked using sta used mta thread, calls supposed marshalled sta thread , executed dedicated thread. in case of windows client application, mean execute on ui thread (if marked sta), , callbacks com component me handled windows messages sent hidden window , processed on windows message loop.

what happens though if use sta com component in wcf service hosted in iis? worker process have windows message loop on sta thread? can fire own sta thread own message loop?

the com runtime looks after dispatching of calls methods on com object inside sta: right based on same os mechanism used dispatching windows messages, don't need worry making happen - com under hood.

what do need worry sta com objects going live in. if instantiate apartment-threaded com objects using com interop wcf service, need careful.

if thread on not sta thread, in-process com objects live in default host sta iis worker process. not want happen: com objects service operations end in same sta. clue in name - there 1 thread objects - , calls methods serialized waiting 1 , thread in apartment execute them. service not scale handle multiple concurrent clients.

you need make sure com objects instantiate service particular wcf request in own sta separate objects created other requests. there broadly 2 ways this:

  • spin own thread, specifying apartmentstate.sta in setapartmentstate() before start it, on instantiate com objects particular request. approach detailed scott seely in the link in kev's answer: ensures each service operation call invoked on new sta-initialised thread. harder more scalable solution along these lines implement pool of reusable sta-initialised threads.
  • host com objects in com+ application, live in separate dllhost process, com+ (through abstraction called the activity) can take care of putting objects different requests different stas.

i'm not sure mean when refer callbacks. perhaps mean com method calls on com interface implemented in managed code, via reference passed in com objects argument 1 of com objects' methods: if so, should work. perhaps mean else, in case perhaps amend question clarify.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -