简体   繁体   English

如何从IIS托管的WCF服务启动进程?

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

I would like to run a process from an intranet client on the WCF service side. 我想从WCF服务端的Intranet客户端运行进程。 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 WCF服务托管在IIS 7.5上,我使用此代码启动进程

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. 如果我将WCF服务托管为在admin用户下运行的自托管控制台应用程序,并且我看到记事本在另一个用户下启动,则此代码有效。 It fails on IIS with no exception, but process is immediately terminated 它在IIS上失败,没有异常,但进程立即终止

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. 在IIS上,WCF应用程序在具有管理员权限的用户下运行,并且已在web.config中定义完全信任。 I cannot use self hosted application as it doesn't support easy continuous delivery (like WebDeploy with IIS web farms). 我不能使用自托管应用程序,因为它不支持简单的持续交付(例如带有IIS Web场的WebDeploy)。

Q: How can I start a process on a server side from WCF service hosted on IIS? 问:如何从IIS上托管的WCF服务启动服务器端的进程?

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. 我偶然发现了这篇文章 ,遇到了类似的问题,我尝试了所有的方法,包括Process.Start和P / Invoke与CreateProcessWithLogonW和CreateProcessAsUser的所有可能变体我还试图向用户授予其他权限。 Non of this would work with the error messages identical to the ones the guy had posted. 不适用于与该人发布的错误消息相同的错误消息。

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. Oleksii,重点是,如果您在控制台应用程序中托管WCF服务,则会为该用户创建一个Windows会话(已登录并加载Windows资源管理器),并为该用户打开并显示记事本,因此您可以看到它在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; 当您在IIS中托管WCF服务作为服务器时,IIS要求并且不允许用户交互,并且如果没有用户登录则也可以工作; 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. 在这种情况下,没有用于托管记事本或其他支持UI的应用程序的UI,您可以执行精心制作或其他批处理作业的流程,但不能渲染Windows UI应用程序,因为没有为您加载Windows资源管理器,也没有地方可以渲染流程的UI。

here is what I use to call GnuPGP to do encryption. 这是我用来调用GnuPGP进行加密的方法。 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;

        }

There's an apppool setting to make sure it loads the user profile. 有一个apppool设置,以确保它加载用户配置文件。

loadUserProfile Optional Boolean attribute.

Specifies whether IIS loads the user profile for the application pool identity. Setting    
this value to false causes IIS to revert to IIS 6.0 behavior. IIS 6.0 does not load the 
user profile for an application pool identity.

The default value is false.

That along with being a domain user as the identity with enough permissions might work?? 与作为具有足够权限的身份的域用户一起工作可能会起作用? I know that at a minimum the user will need a user profile. 我知道用户至少需要一个用户档案。

That said, it's a little bit of an odd architecture. 也就是说,这是一个奇怪的架构。 It seems like a better arch would be to have a persistent process like a windows service that the site communicates with but I'm not sure what your constraints are. 看起来更好的方法是拥有一个持久的进程,就像站点与之通信的Windows服务一样,但我不确定你的约束是什么。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM