Is it possible to configure a REST (WCF) service to run as STA instead of MTA?
是否可以将REST(WCF)服务配置为以STA而不是MTA运行?
This is approach is needed in order to run legacy COM objects. In order to configure ASMX web services to run as STA instead of MTA, there is a workaround available that uses an HTTPHandler to force the web service to run as STA.
这是运行传统COM对象所需的方法。为了将ASMX Web服务配置为以STA而不是MTA运行,有一种可用的解决方法,它使用HTTPHandler强制Web服务作为STA运行。
An article by Jeff Prosise, http://msdn.microsoft.com/en-us/magazine/cc163544.aspx, details the workaround and how to apply it. Applying the same HTTPHandler to a REST-based WCF service (using Create New ADO.NET Data Service in Visual Studio) produces an error at the point at which the WebServiceHandlerFactory is called (it's being passed the HTTPContext, URL, etc.).
Jeff Prosise撰写的一篇文章,http://msdn.microsoft.com/en-us/magazine/cc163544.aspx,详细介绍了解决方法以及如何应用它。将相同的HTTPHandler应用于基于REST的WCF服务(在Visual Studio中使用Create New ADO.NET Data Service)会在调用WebServiceHandlerFactory时生成错误(它正在传递HTTPContext,URL等)。
The handler works for ASMX web services, and I am able to add a WebMethod that returns the threading model as STA. However, even after setting ASPCompatibility (both in web.config
and at the class level), the custom HTTPHandler always produces an error at the same point when trying to use a REST service. I've not configured any endpoints, since I am using a basic REST service with a couple of "service operator" methods. The error is:
该处理程序适用于ASMX Web服务,我可以添加一个WebMethod,它将线程模型作为STA返回。但是,即使在设置ASPCompatibility(在web.config和类级别)之后,自定义HTTPHandler在尝试使用REST服务时也始终在同一点产生错误。我没有配置任何端点,因为我使用的是基本的REST服务和一些“服务运营商”方法。错误是:
Unable to cast object of type 'System.Web.Compilation.BuildResultCustomString' to type 'System.Web.Compilation.BuildResultCompiledType'.
无法将类型为“System.Web.Compilation.BuildResultCustomString”的对象强制转换为“System.Web.Compilation.BuildResultCompiledType”。
2 个解决方案
#1
Read Integrating with COM+ Applications and see if that gets you anywhere.
阅读与COM +应用程序集成,看看是否能让你随处可见。
#2
I'm coming from the opposite direction (trying to convert my STA COM to MTA), and I believe it is automatic, if you instantiate the COM within your service instance and your COM is a proper STA with the proper registry entry. This is the way it worked for me, and I created at least 5 wrappers for this COM.
我来自相反的方向(尝试将我的STA COM转换为MTA),并且我相信它是自动的,如果您在服务实例中实例化COM并且您的COM是具有适当注册表项的正确STA。这是它对我有用的方式,我为这个COM创建了至少5个包装器。
If you are creating threads manually, you may want to use SetApartmentState.
如果要手动创建线程,则可能需要使用SetApartmentState。
#1
Read Integrating with COM+ Applications and see if that gets you anywhere.
阅读与COM +应用程序集成,看看是否能让你随处可见。
#2
I'm coming from the opposite direction (trying to convert my STA COM to MTA), and I believe it is automatic, if you instantiate the COM within your service instance and your COM is a proper STA with the proper registry entry. This is the way it worked for me, and I created at least 5 wrappers for this COM.
我来自相反的方向(尝试将我的STA COM转换为MTA),并且我相信它是自动的,如果您在服务实例中实例化COM并且您的COM是具有适当注册表项的正确STA。这是它对我有用的方式,我为这个COM创建了至少5个包装器。
If you are creating threads manually, you may want to use SetApartmentState.
如果要手动创建线程,则可能需要使用SetApartmentState。