Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a process from an IIS hosted WCF service?

Tags:

c#

iis

wcf

hosting

I would like to run a process from an intranet client on the WCF service side. In my case a client asks a server to create a new process on the server's machine under the provided credentials. WCF service is hosted on IIS 7.5 and I start a process using this code

var processInfo = new ProcessStartInfo("C:\\Windows\\System32\\notepad.exe")
{
    UserName = "some user",
    Password = MakeSecureString("some password"),
    UseShellExecute = false,
    LoadUserProfile = true
};

Process process = Process.Start(processInfo);

This code works if I host WCF service as a self-hosted console application running under admin user and I see the notepad started under another user. It fails on IIS with no exception, but process is immediately terminated

process.HasExited = true;
process.ExitCode = -1073741502;

On IIS WCF application is running under the user with admin rights and has got full trust defined in web.config. I cannot use self hosted application as it doesn't support easy continuous delivery (like WebDeploy with IIS web farms).

Q: How can I start a process on a server side from WCF service hosted on IIS?

EDIT:
I stumbled upon this post, with similar issues and I tried all the methods there, including all possible variations for Process.Start and P/Invoke with CreateProcessWithLogonW and CreateProcessAsUser I also tried granting additional permissions to users. Non of this would work with the error messages identical to the ones the guy had posted.

like image 471
oleksii Avatar asked Aug 26 '11 19:08

oleksii


2 Answers

Oleksii, the point is that if you host the WCF service in a console application, there is a windows session (a user logged in and Windows Explorer loaded) for that user and the notepad is opened and shown for that user, so you see it in the UI.

when you host your WCF service in IIS, being a server, IIS requires and allows no user interaction and works also if no user is logged in; in that context there is no UI to host your notepad or other UI enabled applications, you could execute a process for elaboration or other batch jobs but not render a windows UI application, because Windows Explorer is not loaded for you and there is no place to render your process's UI.

like image 123
Davide Piras Avatar answered Sep 24 '22 03:09

Davide Piras


here is what I use to call GnuPGP to do encryption. How does your setup compare?

private int ExecuteCommand(string arguments, string passPhrase, int timeout)
        {
            Process processObject;
            ProcessStartInfo pInfo = new ProcessStartInfo(_executablePath, arguments);


            pInfo.CreateNoWindow = true;
            pInfo.UseShellExecute = false;

            pInfo.RedirectStandardInput = true;
            pInfo.RedirectStandardOutput = true;
            pInfo.RedirectStandardError = true;
            processObject = Process.Start(pInfo);

            if (!string.IsNullOrEmpty(passPhrase))
            {
                processObject.StandardInput.WriteLine(passPhrase);
                processObject.StandardInput.Flush();
            }

            string result = processObject.StandardOutput.ReadToEnd();
            string error = processObject.StandardError.ReadToEnd();

            if (!processObject.WaitForExit(timeout))
            {
                throw new TimeoutException("GnuPG operation timeout. Waited " + timeout + " milliseconds ");
            }

            int exitcode = processObject.ExitCode;

            Error = error;
            Output = result;

            return exitcode;

        }
like image 41
Dustin Davis Avatar answered Sep 22 '22 03:09

Dustin Davis