Executing batch file in C# web service

前提是你 提交于 2019-12-25 03:59:14

问题


I am trying to execute a batch file in C# web service. 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.

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. 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.


回答1:


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...

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




回答2:


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

Download and run ProcMon to capture all registry and file access. With it running, hit your website in Debug mode and sucessfully execute your batch file through the web service. The in ProdMon, stop capturing (press CTRL-E to start and stop capture) and the press CTRL-F to search for "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. You can filter your results -- just right-click on the Process Name and chose "Include 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. Now hit your webservice...once it fails stop your trace (CTRL-C) and review it to see what exactly if failing. 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

Good luck and tell use what you find!




回答3:


As Yahia pointed up above, your issue is related to security permissions. When run via Debug within VS, your web service's regular permissions are elevated.

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). Now, naturally, this Windows Service will have extended permissions, so it could run any process at any time.

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. 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.



来源:https://stackoverflow.com/questions/7055469/executing-batch-file-in-c-sharp-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!