从另一个.exe调用.exe以运行Web服务

本文关键字:exe Web 服务 运行 调用 另一个 | 更新日期: 2023-09-27 18:24:36

我尝试过用几种类型的示例从另一个带有参数的.exe文件中调用一个.exe文件
运行"web服务",但有时会出现"500–内部服务器错误异常"。

1. code in First .Exe(code for only for one event, i have 8 event like this to run in Button click)    
    dateTimePicker4.CustomFormat = "yyyy-MM-dd";   
            string frodate = dateTimePicker4.Value.Date.ToShortDateString();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = @"C:'WebserviceClient.exe";           // this is the second .EXE file
            startInfo.Arguments = "300000 supplier" + " " + frodate;   // thesse are the 3 parameters
            Process.Start(startInfo);
 2. My second .Exe file receive these parameters and call the Web-Service like below
WebRequest webRequest = WebRequest.Create("http://My Server Path/epos/getproduct.asmx"); // this is web service in another location 
            HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.Headers.Add("SOAPAction: http://tempuri.org/getCategory");
            httpRequest.ProtocolVersion = HttpVersion.Version11;
            httpRequest.Credentials = CredentialCache.DefaultCredentials;
            Stream requestStream = httpRequest.GetRequestStream();
            //Create Stream and Complete Request             
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
        StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'"");
        soapRequest.Append(" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'" ");
        soapRequest.Append("xmlns:soap='"http://schemas.xmlsoap.org/soap/envelope/'"><soap:Body>");
        soapRequest.Append("<getCategory xmlns='"http://tempuri.org/'">");
        soapRequest.Append("<inBranch>" + strParam + "</inBranch>");
        soapRequest.Append("<dir>" + strParamDir + "</dir>");
        soapRequest.Append("<modifyDateFrom>" + strModifyDateFrom + "</modifyDateFrom>");
        soapRequest.Append("<modifyDateTo>" + strModifyDateTo + "</modifyDateTo>");
        soapRequest.Append("</getCategory>");
        soapRequest.Append("</soap:Body></soap:Envelope>");
        streamWriter.Write(soapRequest.ToString());
        streamWriter.Close();
        //Get the Response    
        HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();  // here i am getting the ERROR
        StreamReader srd = new StreamReader(wr.GetResponseStream());
        string resulXmlFromWebService = srd.ReadToEnd();

//注意——我必须用不同的参数运行这个服务8次,当我从8个批处理文件一个接一个地调用第二个.EXE时,就没有问题了。

现在我正试图在按钮点击事件中从我的批处理文件的First.Exe instad一个接一个地运行此服务,然后当第一个事件完成第二个事件启动时,我得到了500错误。

我做错了什么,请给我一些建议。

从另一个.exe调用.exe以运行Web服务

替换此行:

        startInfo.FileName = @"C:'WebserviceClient.exe";

这行:

ThreadPool.QueueUserWorkItem(delegate { Process.Start("C:''WebserviceClient.exe"); });

另一种方法如下:

    public static void TestCommands()
    {
        var command = "WebserviceClient.exe";
        ExecuteCommand(command, 5000);
        var command = "WebserviceClient2.exe";
        ExecuteCommand(command, 5000);
    }
    public static int ExecuteCommand(string command, int timeout)
    {
        var processInfo = new ProcessStartInfo(command)
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            WorkingDirectory = @"C:''",
        };
        var process = Process.Start(processInfo);
        process.WaitForExit(timeout);
        var exitCode = process.ExitCode;
        process.Close();
        return exitCode;
    }