Remote execute and RPC_E_WRONG_THREAD
I don't get it, you are using framework classes that are do***ented as being
non-tested non-supported but still you seem to be surprised when you have
issues.
It's important to know that when you run code as a SQL 'managed SP', you are
restricted to what the host (SQL Server) is allowing you to do, for the
moment SQL server does not allow some assemblies to be brought in it's
namespace (you know that because you had to configure the assembly with a
PERMISSION_SET = UNSAFE). The managed assemblies which cannot be used with
SQLCLR are do***ented as are the API's which cannot be used, all assemblies
which do use COM interop under the covers (like System.Management) aren't
supported, which is quite normal as they were not designed to be used from a
host that uses COM unfriendly fibers instead of (COM initialized) managed
threads.
What you can try is to run your code into your own managed thread (note that
creating threads isn't supported either) so that your RCW ends on a managed
thread instead of on a SQL server's thread/fiber.
Willy.
| Willy,
|
| That is not helpful.
| I use WMI in regular (C++) XPs for long time
| There is little value in Net if we can't use the half of assembiles.
| Do you know if MS has plans to test and start trusting it's own
libraries?
|
| Best regards, Anatol | |
| "Willy Denoyette [MVP]" <willy.denoyette@telenet.be> wrote |
| > You should not bring in the System.Management assembly in SQL server, this
| > assembly is not trusted and not tested in a SQL environment.
| >
| > Willy.
| >
| > "Anatoli Dontsov" <Anatoli@dontsov.com> wrote in message
| > news:uQ0yqagUGHA.1204@TK2MSFTNGP12.phx.gbl...
| > | Hi, All!
| > |
| > | I wrote an XP (extended stored procedure to run under MSSQL) to remote
| > | execute and trying virtually simultaneously start application (say
| > | Notepad.exe) on a dozen boxes.
| > | Or a number of application instances on the SQL box - doesn't matter).
| > | In average 3 of 10 calls failing with message:
| > |
| > | clr_wmi_execute: The application called an interface that was
| > marshalled
| > | for a different thread. (Exception from HRESULT: 0x8001010E
| > | (RPC_E_WRONG_THREAD))
| > |
| > | The error happening in line
| > |
| > | ManagementBaseObject inParams =
| > | pManagementClass.GetMethodParameters("Create");
| > |
| > | See full code below. Any ideas?
| > |
| > | Best regards, Anatoli
| > |
| > |
| > |
| > | using System;
| > | using System.Data;
| > | using System.Management;
| > | using Microsoft.SqlServer.Server;
| > | using System.Data.SqlTypes;
| > |
| > | public sealed partial class FrameworkAdminTools
| > | {
| > | [Microsoft.SqlServer.Server.SqlProcedure]
| > | public static void clr_wmi_execute(SqlString command, SqlString
| > workingdir,
| > | SqlString server, out SqlString NTErrorMessage)
| > | {
| > | NTErrorMessage ="clr_wmi_execute ...";
| > | try
| > | {
| > | ConnectionOptions pConnectionOptions = new ConnectionOptions();
| > | pConnectionOptions.Impersonation =
| > | System.Management.ImpersonationLevel.Impersonate;
| > | ManagementScope pManagementScope = new ManagementScope("\\\\" +
| > | server.ToString() + "\\root\\cimv2", pConnectionOptions);
| > | pManagementScope.Connect();
| > | ManagementPath pManagementPath = new ManagementPath("Win32_Process");
| > | ManagementClass pManagementClass = new ManagementClass
| > (pManagementScope,
| > | pManagementPath, new ObjectGetOptions(null, new TimeSpan(0,0,0,5),
| > true) );
| > | ManagementBaseObject inParams =
| > | pManagementClass.GetMethodParameters("Create");
| > | InvokeMethodOptions pInvokeMethodOptions = new InvokeMethodOptions();
| > | inParams["CommandLine"] = command.ToString();
| > | if (workingdir.ToString().Length > 0)
| > | inParams ["CurrentDirectory"] = workingdir.ToString();
| > | ManagementBaseObject pManagementBaseObject =
| > | pManagementClass.InvokeMethod( "Create", inParams,
| > pInvokeMethodOptions);
| > | object rv = pManagementBaseObject["returnvalue"];
| > | object prid = pManagementBaseObject["processid"];
| > | NTErrorMessage = "PID=" + prid.ToString() + "; rVal=" + rv.ToString();
| > | }
| > | catch (Exception e)
| > | {
| > | NTErrorMessage = "clr_wmi_execute: " + e.Message;
| > | }
| > | SqlContext.Pipe.Send(NTErrorMessage.ToString());
| > | }
| > | }
| > |
| > |
| > |
| > |
| > |
| > |
| > |
| >
| >
|
|
|