简体   繁体   English

在 C# web 服务中执行批处理文件

[英]Executing batch file in C# web service

I am trying to execute a batch file in C# web service.我正在尝试在 C# web 服务中执行批处理文件。 I am sure that the batch file has no problem.我确信批处理文件没有问题。 The problem I am facing is that the batch file will be executed only in debugging mode.我面临的问题是批处理文件将仅在调试模式下执行。 It won't produce error nor being executed when I connect with the client machine.当我与客户端机器连接时,它不会产生错误也不会被执行。 I am sure that there is nothing wrong with the connection since I can successfully send something from client to web service.我确信连接没有问题,因为我可以成功地从客户端向 web 服务发送内容。

Here is the code:这是代码:

        try
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "C:\\mybatch.bat";
            proc.StartInfo.UseShellExecute = true;
            proc.Start(); 

        }
        catch (Exception e)
        {
            return e.ToString();
        }

Visual Studio is running under administrator mode, but cmd.exe is running under (myUser) mode, but I have granted full access to every user. Visual Studio 在管理员模式下运行,但 cmd.exe 在(myUser)模式下运行,但我已授予每个用户完全访问权限。 I don't know if this is a problem or not.我不知道这是否是一个问题。 If not, should I add something to the code?如果没有,我应该在代码中添加一些东西吗? :( :(

Please tell me the possible causes of the problem and the way to solve.请告诉我问题的可能原因和解决方法。

A web service is not running in the "normal desktop"... it has different (=less) permissions/rights, for example they usually don't have access to shares... you can either run the web service with your user account (BIG security risk.) or should reconsider your design since running batch files from web services isn't usually something web services do... web 服务未在“普通桌面”中运行...它具有不同(=较少)的权限/权利,例如他们通常无权访问共享...您可以与您的用户一起运行 web 服务帐户(BIG 安全风险。)或者应该重新考虑您的设计,因为从 web 服务运行批处理文件通常不是 web 服务所做的事情......

IF you really need to do this try如果您真的需要这样做,请尝试

ProcessStartInfo processInfo = new ProcessStartInfo(@"C:\mybatch.bat");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
Process process = Process.Start(processInfo);
process.WaitForExit();
if(process.ExitCode==0){ // success}

see http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx请参阅http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

It's very frustrating when something works in Debug mode, but fails in Release, To sort out the runtime differences: I'd consider using the free ProcMon tool from SysInternals: http://technet.microsoft.com/en-us/sysinternals/bb896645当某些东西在调试模式下工作但在发布时失败时,这是非常令人沮丧的,为了解决运行时差异:我会考虑使用来自 SysInternals 的免费 ProcMon 工具: http://technet.microsoft.com/en-us/sysinternals/ bb896645

Download and run ProcMon to capture all registry and file access.下载并运行 ProcMon 以捕获所有注册表和文件访问。 With it running, hit your website in Debug mode and sucessfully execute your batch file through the web service.随着它的运行,在调试模式下访问您的网站并通过 web 服务成功执行您的批处理文件。 The in ProdMon, stop capturing (press CTRL-E to start and stop capture) and the press CTRL-F to search for "mybatch.bat".在 ProdMon 中,停止捕获(按 CTRL-E 开始和停止捕获)并按 CTRL-F 搜索“mybatch.bat”。

You should find multiple lines--pay attention to the associated process (likely "w3wp.exe"), and notice the Operation, Path and Result of each line.你应该找到多行——注意相关的进程(可能是“w3wp.exe”),并注意每一行的操作、路径和结果。 You can filter your results -- just right-click on the Process Name and chose "Include w3wp.exe".您可以过滤您的结果——只需右键单击进程名称并选择“包括 w3wp.exe”。

When you're ready to see the Release fail case, clear your trace (CTRL-X), put your webserver in Release mode, start your trace again.当您准备好查看发布失败案例时,清除您的跟踪 (CTRL-X),将您的网络服务器置于发布模式,再次启动您的跟踪。 Now hit your webservice...once it fails stop your trace (CTRL-C) and review it to see what exactly if failing.现在点击您的网络服务......一旦它失败停止您的跟踪(CTRL-C)并查看它以查看失败的确切情况。 Once you know that, you'll be able to focus on resolving that issue-the specific difference between debug and release.一旦你知道了这一点,你就可以专注于解决这个问题——调试和发布之间的具体区别。 It's likely the answer will be in the documentation that Yahia pointed to: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx答案很可能在 Yahia 指出的文档中: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

Good luck and tell use what you find!祝你好运,告诉使用你找到的东西!

As Yahia pointed up above, your issue is related to security permissions.正如 Yahia 上面指出的那样,您的问题与安全权限有关。 When run via Debug within VS, your web service's regular permissions are elevated.在 VS 中通过调试运行时,您的 web 服务的常规权限会被提升。

You can develop a lightweight Windows Service which will run on the web service host machine (you can place them on different machines, as long as they have access to some shared resources - see next).您可以开发一个轻量级的 Windows 服务,它将在 web 服务主机上运行(您可以将它们放置在不同的机器上,只要它们可以访问一些共享资源 - 见下文)。 Now, naturally, this Windows Service will have extended permissions, so it could run any process at any time.现在,很自然,这个 Windows 服务将具有扩展权限,因此它可以随时运行任何进程。

As in how to leverage communication between the two brother services, I reckon you have a few options.至于如何利用两个兄弟服务之间的通信,我认为您有几个选择。 First one that pops in my head right away is using files located on a shared location.第一个立即出现在我脑海中的是使用位于共享位置的文件。 When the WS receives a request, it writes down that request in a file on that share.当 WS 接收到请求时,它会将该请求记录在该共享上的文件中。 The Windows service either has a poll timer to check for new jobs in that location, or it can use a FileSystemWatcher to get notified by events. Windows 服务要么有一个轮询计时器来检查该位置的新作业,要么它可以使用FileSystemWatcher来获得事件通知。

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

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