Interop: Using COM in ASMX webservice or WCF service - C#
Recently, I am facing an issue I thought it would be easy to implement. I have a COM object already installed on my Windows 7 machine. I am trying to build a webservice or WCF service around this COM object. Unfortunately, no matter what I try seems useless.
I have both Visual Studio 2008 and 2010 installed. Using the VS IDE, I created a reference to the COM and as expected, VS automagically created the interop DLL needed to access the members of COM.
When I try this in a winforms application, everything works fine, but when used in a service my luck is gone.
The code is supersimple and it looks like this:
LCDirect_WebLogin.CoClass_n_ap_com com = (LCDirect_WebLogin.CoClass_n_ap_com)obj.Unwrap();
object value = null;
string name = "";
com.getsysteminfo(name, ref value);
if (value != null)
return value.ToString();
So, the same code in winform app works fine but under IIS it crashes.
A few extra info: Finally I need to deploy on a Windows 2003 Server. The COM DLL depends on PowerBuilder runtime. I copied the 开发者_运维技巧PB runtimes in my system32 and also in bin folder for webservice.
I don't think it's a dependency issue, but rather a security issue with calling unmanaged code.
I also tried running the webservice under a more proviledged account using the App Pool and using an administrator account to run under. Same issue.
Any ideas? Thanks.
Finally I solved my problem. The solution is to run your COM object in STA mode. I found the solution here: http://www.techrepublic.com/article/application-development-calling-a-com-object-from-a-web-service-in-net/5152421
In your windows app you are in the security context of the logged in user, you.
In the service, the default is that you are executing in the security context of the identity of the application pool. Probably NETWORK SERVICE user, that is not allowed to call COM.
For who might be interested on another ellegant solution: http://msdn.microsoft.com/en-us/magazine/cc163544.aspx
精彩评论